Module: Stateful::ActiveRecord::ClassMethods
- Defined in:
- lib/Stateful/ActiveRecord/ClassMethods.rb
Class Method Summary collapse
Instance Method Summary collapse
- #define_event_method(transition) ⇒ Object
- #define_final_stateQ_method ⇒ Object
- #define_initial_stateQ_method ⇒ Object
- #define_next_state_method ⇒ Object
- #define_stateful_column_name_getter_method ⇒ Object
- #define_stateful_column_name_setter_method ⇒ Object
- #define_status_predicate_method(state_name) ⇒ Object
- #define_transitions_method ⇒ Object
- #stateful_column_name ⇒ Object
-
#stateful_column_name=(stateful_column_name) ⇒ Object
class << self.
Class Method Details
.extended(klass) ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/Stateful/ActiveRecord/ClassMethods.rb', line 10 def extended(klass) klass.define_stateful_column_name_setter_method klass.define_stateful_column_name_getter_method klass.define_next_state_method klass.define_transitions_method klass.define_initial_stateQ_method klass.define_final_stateQ_method end |
Instance Method Details
#define_event_method(transition) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/Stateful/ActiveRecord/ClassMethods.rb', line 29 def define_event_method(transition) stateful_column_name = self.instance_variable_get(:@stateful_column_name) define_method(transition.event_name) do next_state_name = self.send(stateful_column_name).next_state_name(transition.event_name) next_state = self.class.stateful_states.find(next_state_name) self.send("#{stateful_column_name}=", next_state) end end |
#define_final_stateQ_method ⇒ Object
91 92 93 94 95 96 |
# File 'lib/Stateful/ActiveRecord/ClassMethods.rb', line 91 def define_final_stateQ_method stateful_column_name = self.instance_variable_get(:@stateful_column_name) define_method(:final_state?) do final_states.include?(self.send(stateful_column_name)) end end |
#define_initial_stateQ_method ⇒ Object
84 85 86 87 88 89 |
# File 'lib/Stateful/ActiveRecord/ClassMethods.rb', line 84 def define_initial_stateQ_method stateful_column_name = self.instance_variable_get(:@stateful_column_name) define_method(:initial_state?) do self.send(stateful_column_name) == initial_state end end |
#define_next_state_method ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/Stateful/ActiveRecord/ClassMethods.rb', line 69 def define_next_state_method stateful_column_name = self.instance_variable_get(:@stateful_column_name) define_method(:next_state) do |event_name| next_state_name = self.send(stateful_column_name).next_state_name(event_name) all_states.find(next_state_name) end end |
#define_stateful_column_name_getter_method ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/Stateful/ActiveRecord/ClassMethods.rb', line 55 def define_stateful_column_name_getter_method stateful_column_name = self.instance_variable_get(:@stateful_column_name) define_method(stateful_column_name) do instance_variable_set("@#{stateful_column_name}", self.class.stateful_states.find(read_attribute(stateful_column_name))) if state = instance_variable_get("@#{stateful_column_name}") state else initial_state = self.class.stateful_states.initial_state self.send("#{stateful_column_name}=", initial_state.name) initial_state end end end |
#define_stateful_column_name_setter_method ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/Stateful/ActiveRecord/ClassMethods.rb', line 45 def define_stateful_column_name_setter_method stateful_column_name = self.instance_variable_get(:@stateful_column_name) define_method("#{stateful_column_name}=") do |state| instance_variable_set("@#{stateful_column_name}", self.class.stateful_states.find(state)) write_attribute(stateful_column_name, instance_variable_get("@#{stateful_column_name}").name) self.save instance_variable_get("@#{stateful_column_name}") end end |
#define_status_predicate_method(state_name) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/Stateful/ActiveRecord/ClassMethods.rb', line 38 def define_status_predicate_method(state_name) stateful_column_name = self.instance_variable_get(:@stateful_column_name) define_method("#{state_name}?") do self.send(stateful_column_name).name == state_name end end |
#define_transitions_method ⇒ Object
77 78 79 80 81 82 |
# File 'lib/Stateful/ActiveRecord/ClassMethods.rb', line 77 def define_transitions_method stateful_column_name = self.instance_variable_get(:@stateful_column_name) define_method(:transitions) do self.send(stateful_column_name).transitions end end |
#stateful_column_name ⇒ Object
25 26 27 |
# File 'lib/Stateful/ActiveRecord/ClassMethods.rb', line 25 def stateful_column_name @stateful_column_name end |
#stateful_column_name=(stateful_column_name) ⇒ Object
class << self
21 22 23 |
# File 'lib/Stateful/ActiveRecord/ClassMethods.rb', line 21 def stateful_column_name=(stateful_column_name) @stateful_column_name = stateful_column_name end |