Class: StrictMachine::DefinitionContext
- Defined in:
- lib/strict_machine/definition_context.rb
Instance Attribute Summary collapse
-
#mounted_on ⇒ Object
Returns the value of attribute mounted_on.
-
#states ⇒ Object
readonly
Returns the value of attribute states.
-
#transitions ⇒ Object
readonly
Returns the value of attribute transitions.
Instance Method Summary collapse
- #get_state_by_name(name) ⇒ Object
-
#initialize ⇒ DefinitionContext
constructor
A new instance of DefinitionContext.
- #on(hash) ⇒ Object
- #on_entry(&block) ⇒ Object
- #on_transition(&block) ⇒ Object
- #state(name, &block) ⇒ Object
- #transition?(name, state) ⇒ Boolean
Constructor Details
#initialize ⇒ DefinitionContext
Returns a new instance of DefinitionContext.
8 9 10 11 |
# File 'lib/strict_machine/definition_context.rb', line 8 def initialize @states = [] @transitions = [] end |
Instance Attribute Details
#mounted_on ⇒ Object
Returns the value of attribute mounted_on.
6 7 8 |
# File 'lib/strict_machine/definition_context.rb', line 6 def mounted_on @mounted_on end |
#states ⇒ Object (readonly)
Returns the value of attribute states.
5 6 7 |
# File 'lib/strict_machine/definition_context.rb', line 5 def states @states end |
#transitions ⇒ Object (readonly)
Returns the value of attribute transitions.
5 6 7 |
# File 'lib/strict_machine/definition_context.rb', line 5 def transitions @transitions end |
Instance Method Details
#get_state_by_name(name) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/strict_machine/definition_context.rb', line 28 def get_state_by_name(name) @states.each do |this_state| return this_state if this_state.name == name end raise StateNotFoundError, name end |
#on(hash) ⇒ Object
43 44 45 |
# File 'lib/strict_machine/definition_context.rb', line 43 def on(hash) @states.last.add_transition hash end |
#on_entry(&block) ⇒ Object
47 48 49 |
# File 'lib/strict_machine/definition_context.rb', line 47 def on_entry(&block) @states.last.add_on_entry(block) end |
#on_transition(&block) ⇒ Object
51 52 53 |
# File 'lib/strict_machine/definition_context.rb', line 51 def on_transition(&block) @transitions << block end |
#state(name, &block) ⇒ Object
38 39 40 41 |
# File 'lib/strict_machine/definition_context.rb', line 38 def state(name, &block) @states << State.new(name) instance_eval(&block) if block_given? end |
#transition?(name, state) ⇒ Boolean
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/strict_machine/definition_context.rb', line 13 def transition?(name, state) is_bang = (name[-1] == "!") name = is_bang ? name[0..-2] : name @states.each do |this_state| next unless this_state.name.to_sym == state.to_sym this_state.transition_definitions.each do |this_transition| return true if this_transition.name == name.to_sym end end false end |