Class: Progressive::State

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/progressive/state.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *args, &block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/progressive/state.rb', line 13

def method_missing(method_sym, *args, &block)
  if specification.state?(method_sym)
    to(method_sym, *args)
  elsif method_sym.to_s[-1] == '?'
    predicate = method_sym.to_s[0..-2]
    state == predicate
  else
    super
  end
end

Instance Method Details

#current_stateObject



39
40
41
# File 'lib/progressive/state.rb', line 39

def current_state
  specification.states[state.to_sym]
end

#specificationObject



9
10
11
# File 'lib/progressive/state.rb', line 9

def specification
  @specification ||= subject.specification
end

#to(state, options = {}) ⇒ Object

Public: Transition from the current state to a new state.

state - The event

Returns nothing.



29
30
31
32
33
34
35
36
37
# File 'lib/progressive/state.rb', line 29

def to(state, options = {})
  return false unless current_state.event?(state)

  subject.run_callbacks(:progress) do
    subject.run_callbacks(state) do
      update_attribute(:state, state)
    end
  end
end

#to_sObject



43
44
45
# File 'lib/progressive/state.rb', line 43

def to_s
  state
end