Module: DataMapper::Is::StateMachine::InstanceMethods

Defined in:
lib/dm-is-state_machine/is/state_machine.rb

Instance Method Summary collapse

Instance Method Details

#initialize(*args) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/dm-is-state_machine/is/state_machine.rb', line 100

def initialize(*args)
  super
  # ===== Run :enter hook if present =====
  return unless is_sm = model.instance_variable_get(:@is_state_machine)
  return unless machine = is_sm[:machine]
  return unless initial = machine.initial
  return unless initial_state = machine.find_state(initial)
  run_hook_if_present initial_state.options[:enter]
end

#run_hook_if_present(hook) ⇒ Object

hook may be either a Proc or symbol



111
112
113
114
115
116
117
118
# File 'lib/dm-is-state_machine/is/state_machine.rb', line 111

def run_hook_if_present(hook)
  return unless hook
  if hook.respond_to?(:call)
    hook.call(self)
  else
    __send__(hook)
  end
end

#transition!(event_name) ⇒ Object



120
121
122
123
124
125
126
# File 'lib/dm-is-state_machine/is/state_machine.rb', line 120

def transition!(event_name)
  machine = model.instance_variable_get(:@is_state_machine)[:machine]
  column = machine.column
  machine.current_state_name = __send__(column)
  machine.fire_event(event_name, self)
  __send__("#{column}=", machine.current_state_name)
end