Class: Disposable::Callback::Group

Inherits:
Object
  • Object
show all
Extended by:
Declarative::Schema
Defined in:
lib/disposable/callback.rb

Overview

Order matters.

on_change :change!
collection :songs do
  on_add :notify_album!
  on_add :reset_song!

you can call collection :songs again, with :inherit. TODO: verify.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(twin) ⇒ Group

Returns a new instance of Group.



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

def initialize(twin)
  @twin = twin
  @invocations = []
end

Instance Attribute Details

#invocationsObject (readonly)

Returns the value of attribute invocations.



51
52
53
# File 'lib/disposable/callback.rb', line 51

def invocations
  @invocations
end

Class Method Details

.cloneObject



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

def self.clone
  Class.new(self)
end

.collection(name, options = {}, &block) ⇒ Object



25
26
27
# File 'lib/disposable/callback.rb', line 25

def self.collection(name, options={}, &block)
  property(name, options.merge(collection: true), &block)
end

.default_nested_classObject



17
18
19
# File 'lib/disposable/callback.rb', line 17

def self.default_nested_class
  Group
end

.hooksObject



53
54
55
# File 'lib/disposable/callback.rb', line 53

def self.hooks
  @hooks ||= []
end

.property(name, options = {}, &block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/disposable/callback.rb', line 29

def self.property(name, options={}, &block)
  # NOTE: while the API will stay the same, it's very likely i'm gonna use Declarative::Config here instead
  # of maintaining two stacks of callbacks.
  # it should have a Definition per callback where the representer_module will be a nested Group or a Callback.
  inherit = options[:inherit] # FIXME: this is deleted in ::property.

  super(name, options, &block).tap do |dfn|
    return if inherit
    hooks << ["property", dfn[:name]]
  end
end

.remove!(event, callback) ⇒ Object



41
42
43
# File 'lib/disposable/callback.rb', line 41

def self.remove!(event, callback)
  hooks.delete hooks.find { |cfg| cfg[0] == event && cfg[1] == callback }
end

Instance Method Details

#call(options = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/disposable/callback.rb', line 69

def call(options={})
  self.class.hooks.each do |event, method, property_options|
    if event == "property" # FIXME: make nicer.
      definition = self.class.definitions.get(method)
      twin = @twin.send(definition[:name]) # album.songs

      # recursively call nested group.
      @invocations += definition[:nested].new(twin).(options).invocations # Group.new(twin).()
      next
    end

    invocations << callback!(event, options, method, property_options)
  end

  self
end