Class: RailsStateMachine::StateMachine
- Inherits:
-
Object
- Object
- RailsStateMachine::StateMachine
- Defined in:
- lib/rails_state_machine/state_machine.rb
Constant Summary collapse
- StateAlreadyDefinedError =
Class.new(StandardError)
Instance Attribute Summary collapse
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#prefix_for_constant_definition ⇒ Object
readonly
Returns the value of attribute prefix_for_constant_definition.
-
#prefix_for_method_definition ⇒ Object
readonly
Returns the value of attribute prefix_for_method_definition.
-
#state_attribute ⇒ Object
readonly
Returns the value of attribute state_attribute.
Instance Method Summary collapse
- #configure(&block) ⇒ Object
- #event_names ⇒ Object
- #events ⇒ Object
- #find_event(name) ⇒ Object
- #has_state?(name) ⇒ Boolean
-
#initialize(model, state_attribute, prefix: '') ⇒ StateMachine
constructor
A new instance of StateMachine.
- #state_names ⇒ Object
- #states ⇒ Object
Constructor Details
#initialize(model, state_attribute, prefix: '') ⇒ StateMachine
Returns a new instance of StateMachine.
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/rails_state_machine/state_machine.rb', line 6 def initialize(model, state_attribute, prefix: '') @model = model @state_attribute = state_attribute @prefix = prefix @prefix_for_constant_definition = "#{@prefix.upcase}_" if @prefix.present? @prefix_for_method_definition = "#{@prefix.downcase}_" if @prefix.present? @states_by_name = {} @events_by_name = {} build_model_module end |
Instance Attribute Details
#model ⇒ Object (readonly)
Returns the value of attribute model.
17 18 19 |
# File 'lib/rails_state_machine/state_machine.rb', line 17 def model @model end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
17 18 19 |
# File 'lib/rails_state_machine/state_machine.rb', line 17 def prefix @prefix end |
#prefix_for_constant_definition ⇒ Object (readonly)
Returns the value of attribute prefix_for_constant_definition.
17 18 19 |
# File 'lib/rails_state_machine/state_machine.rb', line 17 def prefix_for_constant_definition @prefix_for_constant_definition end |
#prefix_for_method_definition ⇒ Object (readonly)
Returns the value of attribute prefix_for_method_definition.
17 18 19 |
# File 'lib/rails_state_machine/state_machine.rb', line 17 def prefix_for_method_definition @prefix_for_method_definition end |
#state_attribute ⇒ Object (readonly)
Returns the value of attribute state_attribute.
17 18 19 |
# File 'lib/rails_state_machine/state_machine.rb', line 17 def state_attribute @state_attribute end |
Instance Method Details
#configure(&block) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/rails_state_machine/state_machine.rb', line 19 def configure(&block) instance_eval(&block) check_if_states_already_defined define_state_methods define_state_constants register_initial_state define_event_methods define_attributes end |
#event_names ⇒ Object
44 45 46 |
# File 'lib/rails_state_machine/state_machine.rb', line 44 def event_names @events_by_name.keys end |
#events ⇒ Object
40 41 42 |
# File 'lib/rails_state_machine/state_machine.rb', line 40 def events @events_by_name.values end |
#find_event(name) ⇒ Object
48 49 50 |
# File 'lib/rails_state_machine/state_machine.rb', line 48 def find_event(name) @events_by_name[name.to_sym] end |
#has_state?(name) ⇒ Boolean
52 53 54 |
# File 'lib/rails_state_machine/state_machine.rb', line 52 def has_state?(name) @states_by_name.key?(name.to_sym) end |
#state_names ⇒ Object
36 37 38 |
# File 'lib/rails_state_machine/state_machine.rb', line 36 def state_names @states_by_name.keys end |
#states ⇒ Object
32 33 34 |
# File 'lib/rails_state_machine/state_machine.rb', line 32 def states @states_by_name.values end |