Class: StateMachineEnum::StatesCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/state_machine_enum.rb

Overview

This keeps track of the states and rules we allow.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStatesCollector

Returns a new instance of StatesCollector.



24
25
26
27
28
29
30
31
# File 'lib/state_machine_enum.rb', line 24

def initialize
  @transitions = Set.new
  @states = Set.new
  @after_commit_hooks = {}
  @common_after_commit_hooks = []
  @after_attribute_write_hooks = {}
  @common_after_write_hooks = []
end

Instance Attribute Details

#after_attribute_write_hooksObject (readonly)

Returns the value of attribute after_attribute_write_hooks.



21
22
23
# File 'lib/state_machine_enum.rb', line 21

def after_attribute_write_hooks
  @after_attribute_write_hooks
end

#after_commit_hooksObject (readonly)

Returns the value of attribute after_commit_hooks.



21
22
23
# File 'lib/state_machine_enum.rb', line 21

def after_commit_hooks
  @after_commit_hooks
end

#common_after_commit_hooksObject (readonly)

Returns the value of attribute common_after_commit_hooks.



21
22
23
# File 'lib/state_machine_enum.rb', line 21

def common_after_commit_hooks
  @common_after_commit_hooks
end

#common_after_write_hooksObject (readonly)

Returns the value of attribute common_after_write_hooks.



21
22
23
# File 'lib/state_machine_enum.rb', line 21

def common_after_write_hooks
  @common_after_write_hooks
end

#statesObject (readonly)

Returns the value of attribute states.



21
22
23
# File 'lib/state_machine_enum.rb', line 21

def states
  @states
end

Instance Method Details

#_may_transition?(from, to) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/state_machine_enum.rb', line 67

def _may_transition?(from, to)
  @transitions.include?([from.to_s, to.to_s])
end

#_validate(model, attribute_name) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/state_machine_enum.rb', line 56

def _validate(model, attribute_name)
  return unless model.persisted?

  was = model.attribute_was(attribute_name)
  is = model[attribute_name]

  return if (was == is) || @transitions.include?([was, is])

  model.errors.add(attribute_name, "Invalid transition from #{was} to #{is}")
end

#after_any_committed_transition(&blk) ⇒ Object

A generic block that will run together with every committed transition.



52
53
54
# File 'lib/state_machine_enum.rb', line 52

def after_any_committed_transition(&blk)
  @common_after_commit_hooks << blk.to_proc
end

#after_committed_transition_to(target_state, &blk) ⇒ Object

Will run after the specified transition has comitted



46
47
48
49
# File 'lib/state_machine_enum.rb', line 46

def after_committed_transition_to(target_state, &blk)
  @after_commit_hooks[target_state.to_s] ||= []
  @after_commit_hooks[target_state.to_s] << blk.to_proc
end

#after_inline_transition_to(target_state, &blk) ⇒ Object

Runs after the attributes have changed, but before the state is saved



40
41
42
43
# File 'lib/state_machine_enum.rb', line 40

def after_inline_transition_to(target_state, &blk)
  @after_attribute_write_hooks[target_state.to_s] ||= []
  @after_attribute_write_hooks[target_state.to_s] << blk.to_proc
end

#permit_transition(from, to) ⇒ Object

Add a ‘rule’ that allows a transition in a single direction



34
35
36
37
# File 'lib/state_machine_enum.rb', line 34

def permit_transition(from, to)
  @states << from.to_s << to.to_s
  @transitions << [from.to_s, to.to_s]
end