Skip to content

Commit

Permalink
Define state constants
Browse files Browse the repository at this point in the history
  • Loading branch information
stephannv committed Aug 2, 2023
1 parent 3c70691 commit bd8e15a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,16 @@ Machine.successors
}
```

## Class constants
Each machine's state will turn into a constant:
```ruby
Machine.state(:some_state, initial: true)
Machine.state(:another_state)

Machine::SOME_STATE #=> "some_state"
Machine::ANOTHER_STATE # => "another_state"
```

## Instance methods

#### `Machine#current_state`
Expand Down
2 changes: 2 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
const_set(name.upcase, name)

states << name
end

Expand Down
4 changes: 4 additions & 0 deletions spec/statesman/machine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

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) }

Expand Down

0 comments on commit bd8e15a

Please sign in to comment.