Skip to content

Commit

Permalink
Change to explicit check for transition from state.
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmurach committed Jul 25, 2015
1 parent b116d68 commit 7d02725
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
10 changes: 3 additions & 7 deletions lib/finite_machine/events_chain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ def next_transition(name)
#
# @api private
def find_transition(name, from_state)
chain[name].find do |trans|
[ANY_STATE, from_state].include?(trans.from_state)
end
chain[name].find { |trans| trans.matches?(from_state) }
end

# Check if event has branching choice transitions or not
Expand All @@ -108,9 +106,7 @@ def find_transition(name, from_state)
#
# @api public
def choice_transition?(name, from_state)
chain[name].select do |trans|
[ANY_STATE, from_state].include?(trans.from_state)
end.size > 1
chain[name].select { |trans| trans.matches?(from_state) }.size > 1
end

# Examine transitions for event name that start in from state
Expand All @@ -128,7 +124,7 @@ def choice_transition?(name, from_state)
# @api public
def transition_from(name, from_state, *conditions, &block)
chain[name].find do |trans|
[ANY_STATE, from_state].include?(trans.from_state) &&
trans.matches?(from_state) &&
trans.check_conditions(*conditions, &block)
end
end
Expand Down
11 changes: 7 additions & 4 deletions lib/finite_machine/transition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,20 @@ def check_conditions(*args, &block)
# states[state] == state || (states[ANY_STATE] == state && from_state == state)
# end

# Check if machine current state matches any of the from states
# Check if this transition matches from state
#
# @param [Symbol] from
# the from state to match against
#
# @example
# transition.current? # => true
# transition.matches?(:green) # => true
#
# @return [Boolean]
# Return true if match is found, false otherwise.
#
# @api public
def current?
states.keys.any? { |state| state == machine.current || state == ANY_STATE }
def matches?(from)
states.keys.any? { |state| [ANY_STATE, from].include?(state) }
end

# Find this transition can move to
Expand Down

0 comments on commit 7d02725

Please sign in to comment.