Class: AASM::InstanceBase
- Inherits:
-
Object
- Object
- AASM::InstanceBase
- Defined in:
- lib/aasm/instance_base.rb
Instance Attribute Summary collapse
-
#current_event ⇒ Object
Returns the value of attribute current_event.
-
#from_state ⇒ Object
Returns the value of attribute from_state.
-
#to_state ⇒ Object
Returns the value of attribute to_state.
Instance Method Summary collapse
- #current_state ⇒ Object
- #current_state=(state) ⇒ Object
- #determine_state_name(state) ⇒ Object
- #enter_initial_state ⇒ Object
- #events(options = {}) ⇒ Object
- #human_state ⇒ Object
-
#initialize(instance, name = :default) ⇒ InstanceBase
constructor
instance of the class including AASM, name of the state machine.
- #may_fire_event?(name, *args) ⇒ Boolean
- #set_current_state_with_persistence(state) ⇒ Object
- #state_object_for_name(name) ⇒ Object
- #states(options = {}) ⇒ Object
Constructor Details
#initialize(instance, name = :default) ⇒ InstanceBase
instance of the class including AASM, name of the state machine
6 7 8 9 |
# File 'lib/aasm/instance_base.rb', line 6 def initialize(instance, name=:default) # instance of the class including AASM, name of the state machine @instance = instance @name = name end |
Instance Attribute Details
#current_event ⇒ Object
Returns the value of attribute current_event.
4 5 6 |
# File 'lib/aasm/instance_base.rb', line 4 def current_event @current_event end |
#from_state ⇒ Object
Returns the value of attribute from_state.
4 5 6 |
# File 'lib/aasm/instance_base.rb', line 4 def from_state @from_state end |
#to_state ⇒ Object
Returns the value of attribute to_state.
4 5 6 |
# File 'lib/aasm/instance_base.rb', line 4 def to_state @to_state end |
Instance Method Details
#current_state ⇒ Object
11 12 13 |
# File 'lib/aasm/instance_base.rb', line 11 def current_state @instance.aasm_read_state(@name) end |
#current_state=(state) ⇒ Object
15 16 17 18 |
# File 'lib/aasm/instance_base.rb', line 15 def current_state=(state) @instance.aasm_write_state_without_persistence(state, @name) # @current_state = state end |
#determine_state_name(state) ⇒ Object
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/aasm/instance_base.rb', line 70 def determine_state_name(state) case state when Symbol, String state when Proc state.call(@instance) else raise NotImplementedError, "Unrecognized state-type given. Expected Symbol, String, or Proc." end end |
#enter_initial_state ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/aasm/instance_base.rb', line 20 def enter_initial_state state_name = determine_state_name(@instance.class.aasm(@name).initial_state) state_object = state_object_for_name(state_name) state_object.fire_callbacks(:before_enter, @instance) # state_object.fire_callbacks(:enter, @instance) self.current_state = state_name state_object.fire_callbacks(:after_enter, @instance) state_name end |
#events(options = {}) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/aasm/instance_base.rb', line 48 def events(={}) state = [:state] || current_state events = @instance.class.aasm(@name).events.select {|e| e.transitions_from_state?(state) } [:reject] = Array([:reject]) events.reject! { |e| [:reject].include?(e.name) } if [:permitted] # filters the results of events_for_current_state so that only those that # are really currently possible (given transition guards) are shown. events.select! { |e| @instance.send("may_#{e.name}?") } end events end |
#human_state ⇒ Object
32 33 34 |
# File 'lib/aasm/instance_base.rb', line 32 def human_state AASM::Localizer.new.human_state_name(@instance.class, state_object_for_name(current_state)) end |
#may_fire_event?(name, *args) ⇒ Boolean
81 82 83 84 85 86 87 |
# File 'lib/aasm/instance_base.rb', line 81 def may_fire_event?(name, *args) if event = @instance.class.aasm(@name).state_machine.events[name] !!event.may_fire?(@instance, *args) else false # unknown event end end |
#set_current_state_with_persistence(state) ⇒ Object
89 90 91 92 93 |
# File 'lib/aasm/instance_base.rb', line 89 def set_current_state_with_persistence(state) save_success = @instance.aasm_write_state(state, @name) self.current_state = state if save_success save_success end |
#state_object_for_name(name) ⇒ Object
64 65 66 67 68 |
# File 'lib/aasm/instance_base.rb', line 64 def state_object_for_name(name) obj = @instance.class.aasm(@name).states.find {|s| s.name == name} raise AASM::UndefinedState, "State :#{name} doesn't exist" if obj.nil? obj end |
#states(options = {}) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/aasm/instance_base.rb', line 36 def states(={}) if [:permitted] # ugliness level 1000 permitted_event_names = events(:permitted => true).map(&:name) transitions = @instance.class.aasm(@name).state_machine.events.values_at(*permitted_event_names).compact.map {|e| e.transitions_from_state(current_state) } tos = transitions.map {|t| t[0] ? t[0].to : nil}.flatten.compact.map(&:to_sym).uniq @instance.class.aasm(@name).states.select {|s| tos.include?(s.name.to_sym)} else @instance.class.aasm(@name).states end end |