Module: StateMachines::Machine::StateMethods

Included in:
StateMachines::Machine
Defined in:
lib/state_machines/machine/state_methods.rb

Instance Method Summary collapse

Instance Method Details

#dynamic_initial_state?Boolean

Whether a dynamic initial state is being used in the machine

Returns:

  • (Boolean)


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, options = {})
  state = initial_state(object)
  return unless state && (options[:force] || initialize_state?(object))

  value = state.value

  if (hash = options[: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, &)
  options = names.last.is_a?(Hash) ? names.pop : {}
  StateMachines::OptionsValidator.assert_valid_keys!(options, :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 options.any?

    states = add_states(names.first.values)
  else
    states = add_states(names)

    # Update the configuration for the state(s)
    states.each do |state|
      if options.include?(:value)
        state.value = options[:value]
        self.states.update(state)
      end

      state.human_name = options[:human_name] if options.include?(:human_name)
      state.cache = options[:cache] if options.include?(:cache)
      state.matcher = options[:if] if options.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