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
- #update_state!(new_state) ⇒ Object
Constructor Details
#initialize(model_name, name, column_name: :state, metadata: {}) ⇒ Machine
Returns a new instance of Machine.
8 9 10 11 12 13 |
# File 'lib/state_machina/machine.rb', line 8 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
53 54 55 |
# File 'lib/state_machina/machine.rb', line 53 def current_state states.find_by_name(current_state_name) end |
#current_state_name ⇒ Object
49 50 51 |
# File 'lib/state_machina/machine.rb', line 49 def current_state_name model.public_send(column_name) end |
#event(event_name, metadata: {}) {|StateMachina::Registry.register_event(event)| ... } ⇒ Object
21 22 23 24 25 |
# File 'lib/state_machina/machine.rb', line 21 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
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/state_machina/machine.rb', line 36 def events(possible: false, permitted: false, guard_context: model) events = StateMachina::Registry.find_events(model_name, name).values.map(&:dup) 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
15 16 17 18 19 |
# File 'lib/state_machina/machine.rb', line 15 def state(state_name, metadata: {}) state = StateMachina::State.new(model_name, name, state_name, metadata: ) StateMachina::Registry.register_state(state) end |
#states ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/state_machina/machine.rb', line 27 def states states = StateMachina::Registry.find_states(model_name, name).values.map(&:dup) states.each do |state| state.machine = self end StateMachina::StatesCollection.new(states) end |
#update_state(new_state) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/state_machina/machine.rb', line 57 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 |
#update_state!(new_state) ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/state_machina/machine.rb', line 65 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 |