Skip to content

Commit

Permalink
Merge pull request #37 from nathanl/add-triggerable-events-method
Browse files Browse the repository at this point in the history
Add triggerable_events to list events available from current state
  • Loading branch information
soveran committed Dec 16, 2016
2 parents 9eeba9a + 8cb535c commit db9b8b3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -92,7 +92,13 @@ transitions will give you unexpected results.
Finally, you can list possible events or states:

``` ruby
# All possible events
machine.events #=> [:confirm, :ignore, :reset]

# All events triggerable from the current state
machine.triggerable_events #=> [:confirm, :ignore]

# All possible states
machine.states #=> [:new, :confirmed, :ignored]
```

Expand Down
4 changes: 4 additions & 0 deletions lib/micromachine.rb
Expand Up @@ -37,6 +37,10 @@ def events
transitions_for.keys
end

def triggerable_events
events.select { |event| trigger?(event) }
end

def states
transitions_for.values.map(&:to_a).flatten.uniq
end
Expand Down
4 changes: 4 additions & 0 deletions test/introspection.rb
Expand Up @@ -14,6 +14,10 @@
assert_equal [:confirm, :ignore, :reset], machine.events
end

test "list the available events for the current state" do |machine|
assert_equal [:confirm, :ignore], machine.triggerable_events
end

test "returns an array with the defined states" do |machine|
assert_equal [:pending, :confirmed, :ignored], machine.states
end

0 comments on commit db9b8b3

Please sign in to comment.