Module: Theme::Events

Included in:
Component
Defined in:
lib/theme/events.rb

Defined Under Namespace

Modules: Macros

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(other) ⇒ Object



18
19
20
# File 'lib/theme/events.rb', line 18

def self.included(other)
  other.extend(Macros)
end

Instance Method Details

#add_listener(listener) ⇒ Object



22
23
24
# File 'lib/theme/events.rb', line 22

def add_listener(listener)
  (@listeners ||= []) << listener
end

#notify_listeners(event, *args) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/theme/events.rb', line 26

def notify_listeners(event, *args)
  id = self.class.instance_variable_get :@id
  # threads = []

  (@listeners || []).each do |listener|
    event_key  = :"for_#{id}_#{event}"
    event_keys = listener.class._event_blocks.keys

    if id && event_keys.include?(event_key)
      # threads << Thread.new do
      #   ThreadUtility.with_connection do
          listener.trigger(event_key, *args)
        # end
      # end
    end
  end
  #
  # threads.map(&:join)
end

#trigger(name, options = {}) ⇒ Object



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
# File 'lib/theme/events.rb', line 46

def trigger(name, options = {})
  options = Hashr.new(options) if options.is_a? Hash

  callback = false

  if respond_to? name
    callback = name
  elsif self.class._event_blocks
    callback = self.class._event_blocks[name]
  end

  if callback
    if callback.is_a? Proc
      callback.call options
    else
      if method(callback).parameters.length > 0
        send callback, options
      else
        send callback
      end
    end
  end

  notify_listeners(name, options)
end