Sha256: 94da61c3642614b038a41c82e0efee76717e77495f3640d9f45c1dceef83cb30
Contents?: true
Size: 1.51 KB
Versions: 3
Compression:
Stored size: 1.51 KB
Contents
module CanHasState class Trigger attr_accessor :state_machine attr_reader :from, :to, :type, :deferred, :perform def initialize(definition, from:, to:, type:, deferred: false, trigger:) @state_machine = definition @from = Array(from).map{|v| v&.to_s} @to = Array(to).map{|v| v&.to_s} @type = type @deferred = !!deferred @perform = Array(trigger) if @deferred && !state_machine.parent_context.respond_to?(:after_save) raise ArgumentError, 'use of deferred triggers requires support for #after_save callbacks' end @perform.each do |m| unless [Proc, String, Symbol].include?(m.class) raise ArgumentError, "Expecing Symbol or Proc for #{@type.inspect}, got #{m.class} : #{m}" end end end def matches?(from:, to:, deferred: false) matches_from?(from) && matches_to?(to) && matches_deferred?(deferred) end def call(record) perform.each do |m| case m when Proc if m.arity.zero? record.instance_eval(&m) else m.call record end when Symbol, String record.send m end end end private def matches_from?(state) from.include?('*') || from.include?(state) end def matches_to?(state) to.include?('*') || to.include?(state) end def matches_deferred?(defer) defer ? self.deferred : !self.deferred end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
can_has_state-1.0.0 | lib/can_has_state/trigger.rb |
can_has_state-0.9.0 | lib/can_has_state/trigger.rb |
can_has_state-0.8.0 | lib/can_has_state/trigger.rb |