Module: EdgeStateMachine::ClassMethods

Defined in:
lib/edge-state-machine.rb

Instance Method Summary collapse

Instance Method Details

#inherited(klass) ⇒ Object



16
17
18
19
# File 'lib/edge-state-machine.rb', line 16

def inherited(klass)
  super
  klass.state_machines = state_machines
end

#state_machine(name = :default, &block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/edge-state-machine.rb', line 29

def state_machine(name = :default, &block)
  machine = Machine.new(self, name, &block)
  state_machines[name] ||= machine

  machine.persisted_variable_name ||= :state

  machine.states.values.each do |state|
    state_name = state.name
    define_method "#{state_name}?" do
      state_name == current_state(name).name
    end
    add_scope(state, name) if machine.create_scopes?
  end

  machine.events.keys.each do |key|
    define_method "#{key}" do
      fire_event(machine.name, {:save => false}, key)
    end

    define_method "#{key}!" do
      fire_event(machine.name, {:save => true}, key)
    end
  end
end

#state_machinesObject



21
22
23
# File 'lib/edge-state-machine.rb', line 21

def state_machines
  @state_machines ||= {}
end

#state_machines=(value) ⇒ Object



25
26
27
# File 'lib/edge-state-machine.rb', line 25

def state_machines=(value)
  @state_machines = value ? value.dup : nil
end