Class: Progressive::State
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Progressive::State
- Defined in:
- lib/progressive/state.rb
Instance Method Summary collapse
- #current_state ⇒ Object
- #method_missing(method_sym, *args, &block) ⇒ Object
- #specification ⇒ Object
-
#to(state, options = {}) ⇒ Object
Public: Transition from the current state to a new state.
- #to_s ⇒ Object
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_state ⇒ Object
39 40 41 |
# File 'lib/progressive/state.rb', line 39 def current_state specification.states[state.to_sym] end |
#specification ⇒ Object
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, = {}) 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_s ⇒ Object
43 44 45 |
# File 'lib/progressive/state.rb', line 43 def to_s state end |