Class: StateMachina::Machine
- Inherits:
-
Object
- Object
- StateMachina::Machine
- Defined in:
- lib/state_machina/machine.rb
Instance Attribute Summary collapse
-
#column_name ⇒ Object
readonly
Returns the value of attribute column_name.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#model ⇒ Object
Returns the value of attribute model.
-
#model_name ⇒ Object
readonly
Returns the value of attribute model_name.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #current_state ⇒ Object
- #current_state_name ⇒ Object
- #event(event_name, metadata: {}) {|StateMachina::Registry.register_event(event)| ... } ⇒ Object
- #events(possible: false, permitted: false, guard_context: model) ⇒ Object
-
#initialize(model_name, name, column_name: :state, metadata: {}) ⇒ Machine
constructor
A new instance of Machine.
- #state(state_name, metadata: {}) ⇒ Object
- #states ⇒ Object
- #update_state(new_state) ⇒ Object
Constructor Details
#initialize(model_name, name, column_name: :state, metadata: {}) ⇒ Machine
Returns a new instance of Machine.
6 7 8 9 10 11 |
# File 'lib/state_machina/machine.rb', line 6 def initialize(model_name, name, column_name: :state, metadata: {}) @model_name = model_name @name = name.to_s @column_name = column_name @metadata = end |
Instance Attribute Details
#column_name ⇒ Object (readonly)
Returns the value of attribute column_name.
3 4 5 |
# File 'lib/state_machina/machine.rb', line 3 def column_name @column_name end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
3 4 5 |
# File 'lib/state_machina/machine.rb', line 3 def @metadata end |
#model ⇒ Object
Returns the value of attribute model.
4 5 6 |
# File 'lib/state_machina/machine.rb', line 4 def model @model end |
#model_name ⇒ Object (readonly)
Returns the value of attribute model_name.
3 4 5 |
# File 'lib/state_machina/machine.rb', line 3 def model_name @model_name end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/state_machina/machine.rb', line 3 def name @name end |
Instance Method Details
#current_state ⇒ Object
51 52 53 |
# File 'lib/state_machina/machine.rb', line 51 def current_state states.find_by_name(current_state_name) end |
#current_state_name ⇒ Object
47 48 49 |
# File 'lib/state_machina/machine.rb', line 47 def current_state_name model.public_send(column_name) end |
#event(event_name, metadata: {}) {|StateMachina::Registry.register_event(event)| ... } ⇒ Object
19 20 21 22 23 |
# File 'lib/state_machina/machine.rb', line 19 def event(event_name, metadata: {}) event = StateMachina::Event.new(model_name, name, event_name, metadata: ) yield(StateMachina::Registry.register_event(event)) end |
#events(possible: false, permitted: false, guard_context: model) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/state_machina/machine.rb', line 34 def events(possible: false, permitted: false, guard_context: model) events = StateMachina::Registry.find_events(model_name, name).values events.each do |event| event.machine = self event.guard_context = guard_context end events = select_possible_events(events) if possible || permitted events = select_permitted_events(events) if permitted StateMachina::EventsCollection.new(events) end |
#state(state_name, metadata: {}) ⇒ Object
13 14 15 16 17 |
# File 'lib/state_machina/machine.rb', line 13 def state(state_name, metadata: {}) state = StateMachina::State.new(model_name, name, state_name, metadata: ) StateMachina::Registry.register_state(state) end |
#states ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/state_machina/machine.rb', line 25 def states states = StateMachina::Registry.find_states(model_name, name).values states.each do |state| state.machine = self end StateMachina::StatesCollection.new(states) end |
#update_state(new_state) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/state_machina/machine.rb', line 55 def update_state(new_state) if model.respond_to?(:update) model.update(column_name => new_state) else model.public_send("#{column_name}=", new_state) end end |