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



3
4
5
# File 'lib/theme/events.rb', line 3

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

Instance Method Details

#add_listener(listener) ⇒ Object



7
8
9
# File 'lib/theme/events.rb', line 7

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

#notify_listeners(event, *args) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/theme/events.rb', line 11

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

  (@listeners || []).each do |listener|
    if id
      listener.trigger(:"#{id}_#{event}", *args)
    else
      listener.trigger(event, *args)
    end
  end
end

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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/theme/events.rb', line 23

def trigger(name, options = {})
  if self.class._event_blocks && callback = self.class._event_blocks[name]
    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