Module: Theme::Events::Macros

Defined in:
lib/theme/events.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#_event_blocksObject

Returns the value of attribute _event_blocks.



48
49
50
# File 'lib/theme/events.rb', line 48

def _event_blocks
  @_event_blocks
end

#_for_listenersObject

Returns the value of attribute _for_listeners.



48
49
50
# File 'lib/theme/events.rb', line 48

def _for_listeners
  @_for_listeners
end

Instance Method Details

#on_event(name, options = {}, &block) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/theme/events.rb', line 50

def on_event(name, options = {}, &block)
  if id = options[:for]
    (@_for_listeners ||= []) << id
    name = :"#{id}_#{name}"
  end

  @_event_blocks ||= {}
  @_event_blocks[name] = options.fetch(:use) { block }

  mod = if const_defined?(:Events, false)
    const_get(:Events)
  else
    new_mod = Module.new do
      def self.to_s
        "Events(#{instance_methods(false).join(', ')})"
      end
    end
    const_set(:Events, new_mod)
  end

  include mod
end