Class: CanHasState::Definition
- Inherits:
-
Object
- Object
- CanHasState::Definition
- Defined in:
- lib/can_has_state/definition.rb
Instance Attribute Summary collapse
-
#column ⇒ Object
readonly
Returns the value of attribute column.
-
#initial_state ⇒ Object
readonly
Returns the value of attribute initial_state.
-
#parent_context ⇒ Object
Returns the value of attribute parent_context.
-
#states ⇒ Object
readonly
Returns the value of attribute states.
-
#triggers ⇒ Object
readonly
Returns the value of attribute triggers.
Instance Method Summary collapse
- #allow?(record, to) ⇒ Boolean
- #extend_machine(&block) ⇒ Object
-
#initialize(column_name, model_class, &block) ⇒ Definition
constructor
A new instance of Definition.
- #initialize_dup(orig) ⇒ Object
- #known?(to) ⇒ Boolean
- #message(to) ⇒ Object
- #on(pairs) ⇒ Object
- #state(state_name, *args) ⇒ Object
-
#triggers_for(from:, to:, **conditions) ⇒ Object
conditions - :deferred.
Constructor Details
#initialize(column_name, model_class, &block) ⇒ Definition
Returns a new instance of Definition.
7 8 9 10 11 12 13 14 |
# File 'lib/can_has_state/definition.rb', line 7 def initialize(column_name, model_class, &block) @parent_context = model_class @column = column_name.to_sym @states = {} @triggers = [] instance_eval(&block) @initial_state ||= @states.keys.first end |
Instance Attribute Details
#column ⇒ Object (readonly)
Returns the value of attribute column.
5 6 7 |
# File 'lib/can_has_state/definition.rb', line 5 def column @column end |
#initial_state ⇒ Object (readonly)
Returns the value of attribute initial_state.
5 6 7 |
# File 'lib/can_has_state/definition.rb', line 5 def initial_state @initial_state end |
#parent_context ⇒ Object
Returns the value of attribute parent_context.
4 5 6 |
# File 'lib/can_has_state/definition.rb', line 4 def parent_context @parent_context end |
#states ⇒ Object (readonly)
Returns the value of attribute states.
5 6 7 |
# File 'lib/can_has_state/definition.rb', line 5 def states @states end |
#triggers ⇒ Object (readonly)
Returns the value of attribute triggers.
5 6 7 |
# File 'lib/can_has_state/definition.rb', line 5 def triggers @triggers end |
Instance Method Details
#allow?(record, to) ⇒ Boolean
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/can_has_state/definition.rb', line 90 def allow?(record, to) to = to&.to_s return false unless known?(to) states[to][:requirements].all? do |g| case g when Proc if g.arity.zero? record.instance_eval(&g) else g.call record end when Symbol, String record.send g else raise ArgumentError, "Expecing Symbol or Proc for :require, got #{g.class} : #{g}" end end end |
#extend_machine(&block) ⇒ Object
27 28 29 |
# File 'lib/can_has_state/definition.rb', line 27 def extend_machine(&block) instance_eval(&block) end |
#initialize_dup(orig) ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/can_has_state/definition.rb', line 16 def initialize_dup(orig) @states = @states.deep_dup @triggers = @triggers.map do |t| t = t.dup t.state_machine = self t end super end |
#known?(to) ⇒ Boolean
85 86 87 88 |
# File 'lib/can_has_state/definition.rb', line 85 def known?(to) to = to&.to_s states.keys.include? to end |
#message(to) ⇒ Object
109 110 111 112 |
# File 'lib/can_has_state/definition.rb', line 109 def (to) to = to&.to_s states[to][:message] end |
#on(pairs) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/can_has_state/definition.rb', line 75 def on(pairs) trigger = pairs.delete :trigger deferred = pairs.delete :deferred pairs.each do |from, to| @triggers << Trigger.new(self, from: from, to: to, trigger: trigger, type: :trigger, deferred: deferred) end end |
#state(state_name, *args) ⇒ Object
32 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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/can_has_state/definition.rb', line 32 def state(state_name, *args) = args. state_name = state_name.to_s if args.include? :initial @initial_state = state_name end requirements = [] = :invalid_transition .each do |key, val| case key when :from from_vals = Array(val).compact.map(&:to_s) from_vals << nil # for new records requirements << lambda do |r| val_was = r.send("#{column}_was")&.to_s from_vals.include? val_was end when :require requirements += Array(val) when :message = val when :timestamp @triggers << Trigger.new(self, from: '*', to: state_name, trigger: lambda{|r| r.send("#{val}=", Time.now.utc)}, type: :timestamp) when :on_enter @triggers << Trigger.new(self, from: '*', to: state_name, trigger: val, type: :on_enter) when :on_enter_deferred @triggers << Trigger.new(self, from: '*', to: state_name, trigger: val, type: :on_enter, deferred: true) when :on_exit @triggers << Trigger.new(self, from: state_name, to: '*', trigger: val, type: :on_exit) when :on_exit_deferred @triggers << Trigger.new(self, from: state_name, to: '*', trigger: val, type: :on_exit, deferred: true) else raise ArgumentError, "Unknown argument #{key.inspect}" end end @states[state_name] = {requirements: requirements, message: } end |
#triggers_for(from:, to:, **conditions) ⇒ Object
conditions - :deferred
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/can_has_state/definition.rb', line 116 def triggers_for(from:, to:, **conditions) from = from&.to_s to = to&.to_s # Rails.logger.debug "Checking triggers for transition #{from.inspect} to #{to.inspect} (#{conditions.inspect})" @triggers.select do |trigger| trigger.matches? from: from, to: to, **conditions # end.each do |trigger| # Rails.logger.debug " Matched trigger: #{trigger.from.inspect} -- #{trigger.to.inspect}" end end |