class Disposable::Callback::Dispatch
Invokes callback for one event, e.g. on_add
(:relax!). Implements the binding between the Callback
API (on_change
) and the underlying layer (twin/AR/etc.).
Public Class Methods
new(twins)
click to toggle source
# File lib/disposable/callback.rb, line 105 def initialize(twins) @twins = Array(twins) # TODO: find that out with Collection. @invocations = [] end
Public Instance Methods
call(event, method, property_options, &block)
click to toggle source
# File lib/disposable/callback.rb, line 110 def call(event, method, property_options, &block) # FIXME: as long as we only support method, pass in here. send(event, property_options, &block) [event, method, @invocations] end
on_add(state=nil, &block)
click to toggle source
# File lib/disposable/callback.rb, line 115 def on_add(state=nil, &block) # how to call it once, for "all"? # @twins can only be Collection instance. @twins.added.each do |item| run!(item, &block) if ! state.is_a?(Symbol) run!(item, &block) if item.created? && state == :created # :created # DISCUSS: should we really keep that? end end
on_change(property_options={}, &block)
click to toggle source
# File lib/disposable/callback.rb, line 152 def on_change(property_options={}, &block) name = property_options[:property] @twins.each do |twin| if name run!(twin, &block) if twin.changed?(name) next end next unless twin.changed? run!(twin, &block) end end
on_create(*, &block)
click to toggle source
# File lib/disposable/callback.rb, line 145 def on_create(*, &block) @twins.each do |twin| next unless twin.created? run!(twin, &block) end end
on_delete(*, &block)
click to toggle source
# File lib/disposable/callback.rb, line 123 def on_delete(*, &block) # @twins can only be Collection instance. @twins.deleted.each do |item| run!(item, &block) end end
on_destroy(*, &block)
click to toggle source
# File lib/disposable/callback.rb, line 130 def on_destroy(*, &block) @twins.destroyed.each do |item| run!(item, &block) end end
on_update(*, &block)
click to toggle source
# File lib/disposable/callback.rb, line 136 def on_update(*, &block) @twins.each do |twin| next if twin.created? next unless twin.persisted? # only persisted can be updated. next unless twin.changed? run!(twin, &block) end end
Private Instance Methods
run!(twin) { |twin| ... }
click to toggle source
# File lib/disposable/callback.rb, line 167 def run!(twin, &block) yield(twin).tap do |res| @invocations << twin end end