Module: StateMachines::Machine::StateMethods
- Included in:
- StateMachines::Machine
- Defined in:
- lib/state_machines/machine/state_methods.rb
Instance Method Summary collapse
-
#dynamic_initial_state? ⇒ Boolean
Whether a dynamic initial state is being used in the machine.
-
#initial_state(object) ⇒ Object
Gets the initial state of the machine for the given object.
-
#initialize_state(object, options = {}) ⇒ Object
Initializes the state on the given object.
-
#read(object, attribute, ivar = false) ⇒ Object
Gets the current value stored in the given object’s attribute.
-
#state(*names) ⇒ Object
(also: #other_states)
Customizes the definition of one or more states in the machine.
-
#write(object, attribute, value, ivar = false) ⇒ Object
Sets a new value in the given object’s attribute.
Instance Method Details
#dynamic_initial_state? ⇒ Boolean
Whether a dynamic initial state is being used in the machine
14 15 16 |
# File 'lib/state_machines/machine/state_methods.rb', line 14 def dynamic_initial_state? instance_variable_defined?(:@initial_state) && @initial_state.is_a?(Proc) end |
#initial_state(object) ⇒ Object
Gets the initial state of the machine for the given object. If a dynamic initial state was configured for this machine, then the object will be passed into the lambda block to help determine the actual state.
9 10 11 |
# File 'lib/state_machines/machine/state_methods.rb', line 9 def initial_state(object) states.fetch(dynamic_initial_state? ? evaluate_method(object, @initial_state) : @initial_state) if instance_variable_defined?(:@initial_state) end |
#initialize_state(object, options = {}) ⇒ Object
Initializes the state on the given object. Initial values are only set if the machine’s attribute hasn’t been previously initialized.
Configuration options:
-
:force
- Whether to initialize the state regardless of its current value -
:to
- A hash to set the initial value in instead of writing directly to the object
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/state_machines/machine/state_methods.rb', line 26 def initialize_state(object, = {}) state = initial_state(object) return unless state && ([:force] || initialize_state?(object)) value = state.value if (hash = [:to]) hash[attribute.to_s] = value else write(object, :state, value) end end |
#read(object, attribute, ivar = false) ⇒ Object
Gets the current value stored in the given object’s attribute.
76 77 78 79 80 81 82 83 |
# File 'lib/state_machines/machine/state_methods.rb', line 76 def read(object, attribute, ivar = false) attribute = self.attribute(attribute) if ivar object.instance_variable_defined?(:"@#{attribute}") ? object.instance_variable_get("@#{attribute}") : nil else object.send(attribute) end end |
#state(*names) ⇒ Object Also known as: other_states
Customizes the definition of one or more states in the machine.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/state_machines/machine/state_methods.rb', line 40 def state(*names, &) = names.last.is_a?(Hash) ? names.pop : {} StateMachines::OptionsValidator.assert_valid_keys!(, :value, :cache, :if, :human_name) # Store the context so that it can be used for / matched against any state # that gets added @states.context(names, &) if block_given? if names.first.is_a?(Matcher) # Add any states referenced in the matcher. When matchers are used, # states are not allowed to be configured. raise ArgumentError, "Cannot configure states when using matchers (using #{options.inspect})" if .any? states = add_states(names.first.values) else states = add_states(names) # Update the configuration for the state(s) states.each do |state| if .include?(:value) state.value = [:value] self.states.update(state) end state.human_name = [:human_name] if .include?(:human_name) state.cache = [:cache] if .include?(:cache) state.matcher = [:if] if .include?(:if) end end states.length == 1 ? states.first : states end |
#write(object, attribute, value, ivar = false) ⇒ Object
Sets a new value in the given object’s attribute.
86 87 88 89 |
# File 'lib/state_machines/machine/state_methods.rb', line 86 def write(object, attribute, value, ivar = false) attribute = self.attribute(attribute) ivar ? object.instance_variable_set(:"@#{attribute}", value) : object.send("#{attribute}=", value) end |