Class: StrictMachine::Base

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *_args) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/strict_machine/base.rb', line 75

def method_missing(meth, *_args)
  if transition?(meth, current_state)
    trigger_transition(meth, mounted_on || self)
  else
    raise TransitionNotFoundError, meth
  end
end

Instance Attribute Details

#mounted_onObject

Returns the value of attribute mounted_on.



6
7
8
# File 'lib/strict_machine/base.rb', line 6

def mounted_on
  @mounted_on
end

#state_attrObject

Returns the value of attribute state_attr.



6
7
8
# File 'lib/strict_machine/base.rb', line 6

def state_attr
  @state_attr
end

Class Method Details

.statesObject



19
20
21
# File 'lib/strict_machine/base.rb', line 19

def self.states
  definition.states
end

.strict_machine(&block) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/strict_machine/base.rb', line 8

def self.strict_machine(&block)
  dc = DefinitionContext.new
  dc.instance_eval(&block)

  metaclass.instance_eval do
    define_method(:definition) { dc }
  end
end

Instance Method Details

#boot!Object



23
24
25
26
27
# File 'lib/strict_machine/base.rb', line 23

def boot!
  @state_attr = :status if @state_attr.nil?

  change_state(self.class.states.first.name)
end

#current_stateObject



29
30
31
# File 'lib/strict_machine/base.rb', line 29

def current_state
  instance_variable_get "@#{@state_attr}"
end

#definitionObject



33
34
35
# File 'lib/strict_machine/base.rb', line 33

def definition
  self.class.definition
end

#respond_to?(meth, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/strict_machine/base.rb', line 71

def respond_to?(meth, _include_private = false)
  transition?(meth, current_state)
end

#transition?(meth, state) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/strict_machine/base.rb', line 37

def transition?(meth, state)
  definition.transition?(meth, state)
end

#trigger_transition(trigger, stored = self) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/strict_machine/base.rb', line 41

def trigger_transition(trigger, stored = self)
  dt = Time.now

  is_bang = !trigger.to_s.index("!").nil?
  transition = from_state.get_transition(trigger, is_bang)

  if transition.guarded? && !is_bang
    raise GuardedTransitionError unless stored.public_send(
      transition.guard
    )
  end

  new_state = definition.get_state_by_name(transition.to)
  new_state.on_entry.each do |proc|
    stored.instance_exec(current_state, trigger.to_sym, &proc)
  end

  duration = Time.now - dt

  definition.transitions.each do |proc|
    stored.instance_exec(
      current_state, new_state.name, trigger.to_sym, duration, &proc
    )
  end

  change_state(new_state.name)
end