Class: StrictMachine::DefinitionContext

Inherits:
Object
  • Object
show all
Defined in:
lib/strict_machine/definition_context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDefinitionContext

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_onObject

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

#statesObject (readonly)

Returns the value of attribute states.



5
6
7
# File 'lib/strict_machine/definition_context.rb', line 5

def states
  @states
end

#transitionsObject (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

Raises:



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

Returns:

  • (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