Module: Apollo::ClassMethods

Defined in:
lib/apollo.rb

Instance Method Summary collapse

Instance Method Details

#current_state_column(column_name = nil) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/apollo.rb', line 46

def current_state_column(column_name=nil)
  if column_name
    @current_state_column_name = column_name.to_sym
  else
    @current_state_column_name ||= :current_state
  end
  @current_state_column_name
end

#state_machine(&specification) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/apollo.rb', line 55

def state_machine(&specification)
  return @apollo_spec unless block_given?
  
  @apollo_spec = Specification.new(Hash.new, &specification)
  @apollo_spec.states.values.each do |state|
    state_name = state.name
    module_eval do
      define_method "#{state_name}?" do
        state_name == current_state.name
      end
    end

    state.events.values.each do |event|
      event_name = event.name
      module_eval do
        define_method "#{event_name}!".to_sym do |*args|
          process_event!(event_name, *args)
        end
      end
    end
  end
  
  @apollo_spec.state_sets.keys.each do |set_name|
    module_eval do
      define_method "#{set_name}?" do
        current_state.set_names.include?(set_name.to_sym)
      end
    end
  end
end