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 = {}, *args) ⇒ Object
- #fire(event_name, *args, &block) ⇒ Object
- #fire!(event_name, *args, &block) ⇒ 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
- #permitted_transitions ⇒ Object
- #set_current_state_with_persistence(state) ⇒ Object
- #state_object_for_name(name) ⇒ Object
- #states(options = {}, *args) ⇒ Object
Constructor Details
#initialize(instance, name = :default) ⇒ InstanceBase
instance of the class including AASM, name of the state machine
5 6 7 8 |
# File 'lib/aasm/instance_base.rb', line 5 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.
3 4 5 |
# File 'lib/aasm/instance_base.rb', line 3 def current_event @current_event end |
#from_state ⇒ Object
Returns the value of attribute from_state.
3 4 5 |
# File 'lib/aasm/instance_base.rb', line 3 def from_state @from_state end |
#to_state ⇒ Object
Returns the value of attribute to_state.
3 4 5 |
# File 'lib/aasm/instance_base.rb', line 3 def to_state @to_state end |
Instance Method Details
#current_state ⇒ Object
10 11 12 |
# File 'lib/aasm/instance_base.rb', line 10 def current_state @instance.aasm_read_state(@name) end |
#current_state=(state) ⇒ Object
14 15 16 |
# File 'lib/aasm/instance_base.rb', line 14 def current_state=(state) @instance.aasm_write_state_without_persistence(state, @name) end |
#determine_state_name(state) ⇒ Object
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/aasm/instance_base.rb', line 97 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
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/aasm/instance_base.rb', line 18 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) self.current_state = state_name state_object.fire_callbacks(:after_enter, @instance) state_name end |
#events(options = {}, *args) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/aasm/instance_base.rb', line 60 def events(={}, *args) 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 .has_key?(:permitted) # filters the results of events_for_current_state so that only those that # are really currently possible (given transition guards) are shown. if [:permitted] events.select! { |e| @instance.send("may_#{e.name}?", *args) } else events.select! { |e| !@instance.send("may_#{e.name}?", *args) } end end events end |
#fire(event_name, *args, &block) ⇒ Object
116 117 118 119 120 |
# File 'lib/aasm/instance_base.rb', line 116 def fire(event_name, *args, &block) event_exists?(event_name) @instance.send(event_name, *args, &block) end |
#fire!(event_name, *args, &block) ⇒ Object
122 123 124 125 126 |
# File 'lib/aasm/instance_base.rb', line 122 def fire!(event_name, *args, &block) event_exists?(event_name, true) bang_event_name = "#{event_name}!".to_sym @instance.send(bang_event_name, *args, &block) end |
#human_state ⇒ Object
29 30 31 |
# File 'lib/aasm/instance_base.rb', line 29 def human_state state_object_for_name(current_state).display_name end |
#may_fire_event?(name, *args) ⇒ Boolean
108 109 110 111 112 113 114 |
# File 'lib/aasm/instance_base.rb', line 108 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 |
#permitted_transitions ⇒ Object
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/aasm/instance_base.rb', line 80 def permitted_transitions events(permitted: true).flat_map do |event| available_transitions = event.transitions_from_state(current_state) allowed_transitions = available_transitions.select { |t| t.allowed?(@instance) } allowed_transitions.map do |transition| { event: event.name, state: transition.to } end end end |
#set_current_state_with_persistence(state) ⇒ Object
128 129 130 131 132 |
# File 'lib/aasm/instance_base.rb', line 128 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
91 92 93 94 95 |
# File 'lib/aasm/instance_base.rb', line 91 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 = {}, *args) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/aasm/instance_base.rb', line 33 def states(={}, *args) if .has_key?(:permitted) selected_events = events({:permitted => [:permitted]}, *args) # An array of arrays. Each inner array represents the transitions that # transition from the current state for an event event_transitions = selected_events.map {|e| e.transitions_from_state(current_state) } # An array of :to transition states to_state_names = event_transitions.map do |transitions| return nil if transitions.empty? # Return the :to state of the first transition that is allowed (or not) or nil if [:permitted] transition = transitions.find { |t| t.allowed?(@instance, *args) } else transition = transitions.find { |t| !t.allowed?(@instance, *args) } end transition ? transition.to : nil end.flatten.compact.uniq # Select states that are in to_state_names @instance.class.aasm(@name).states.select {|s| to_state_names.include?(s.name)} else @instance.class.aasm(@name).states end end |