Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define state constants #515

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,24 @@ Machine.successors
}
```

## Class constants
Adding a state to a state machine will automatically create a constant for the value, for example:
```ruby
stephannv marked this conversation as resolved.
Show resolved Hide resolved
class OrderStateMachine
include Statesman::Machine

state :pending, initial: true
state :checking_out
state :cancelled

# Constants created as a side effect of adding state
transition from: PENDING, to: [CHECKING_OUT, CANCELLED]
end

OrderStateMachine::PENDING #=> "pending"
OrderStateMachine::CHECKING_OUT # => "checking_out"
```

## Instance methods

### `Machine#current_state`
Expand Down
2 changes: 2 additions & 0 deletions lib/statesman/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class TransitionConflictError < StandardError; end

class MissingTransitionAssociation < StandardError; end

class StateConstantConflictError < StandardError; end

class TransitionFailedError < StandardError
def initialize(from, to)
@from = from
Expand Down
12 changes: 12 additions & 0 deletions lib/statesman/machine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ def state(name, options = { initial: false })
validate_initial_state(name)
@initial_state = name
end
define_state_constant(name)

states << name
end

Expand Down Expand Up @@ -163,6 +165,16 @@ def validate_from_and_to_state(from, to)

private

def define_state_constant(state_name)
constant_name = state_name.upcase

if const_defined?(constant_name)
raise StateConstantConflictError, "Name conflict: '#{self.class.name}::#{constant_name}' is already defined"
else
const_set(constant_name, state_name)
end
end

def add_callback(callback_type: nil, callback_class: nil,
from: nil, to: nil, &block)
validate_callback_type_and_class(callback_type, callback_class)
Expand Down
10 changes: 10 additions & 0 deletions spec/statesman/exceptions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@
end
end

describe "StateConstantConflictError" do
subject(:error) { Statesman::StateConstantConflictError.new }

its(:message) { is_expected.to eq("Statesman::StateConstantConflictError") }

its "string matches its message" do
expect(error.to_s).to eq(error.message)
end
end

describe "TransitionFailedError" do
subject(:error) { Statesman::TransitionFailedError.new("from", "to") }

Expand Down
18 changes: 16 additions & 2 deletions spec/statesman/machine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@

specify { expect(machine.states).to eq(%w[x y]) }

specify { expect(machine::X).to eq "x" }

specify { expect(machine::Y).to eq "y" }

context "initial" do
before { machine.state(:x, initial: true) }
before { machine.state(:z, initial: true) }

specify { expect(machine.initial_state).to eq("x") }
specify { expect(machine.initial_state).to eq("z") }

context "when an initial state is already defined" do
it "raises an error" do
Expand All @@ -23,6 +27,16 @@
end
end
end

context "when state name constant is already defined" do
it "warns about name conflict" do
machine.const_set(:SOME_CONST, "some const")

expect { machine.state(:some_const) }.to raise_error(
Statesman::StateConstantConflictError, "Name conflict: 'Class::SOME_CONST' is already defined"
)
end
end
end

describe ".remove_state" do
Expand Down