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
Changes from 2 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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -390,6 +390,16 @@ Machine.successors
}
```

## Class constants
Each machine's state will turn into a constant:
stephannv marked this conversation as resolved.
Show resolved Hide resolved
```ruby
stephannv marked this conversation as resolved.
Show resolved Hide resolved
Machine.state(:some_state, initial: true)
Machine.state(:another_state)

Machine::SOME_STATE #=> "some_state"
Machine::ANOTHER_STATE # => "another_state"
stephannv marked this conversation as resolved.
Show resolved Hide resolved
```

## Instance methods

### `Machine#current_state`
2 changes: 2 additions & 0 deletions lib/statesman/exceptions.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions lib/statesman/machine.rb
Original file line number Diff line number Diff line change
@@ -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

@@ -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)
10 changes: 10 additions & 0 deletions spec/statesman/exceptions_spec.rb
Original file line number Diff line number Diff line change
@@ -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") }

18 changes: 16 additions & 2 deletions spec/statesman/machine_spec.rb
Original file line number Diff line number Diff line change
@@ -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
@@ -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