Module: HookR::Hooks::ClassMethods

Defined in:
lib/hookr.rb

Instance Method Summary collapse

Instance Method Details

#const_missing(const_name) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/hookr.rb', line 90

def const_missing(const_name)
  if const_name.to_s == "Listener"
    hooks = fetch_or_create_hooks
    listener_class ||= Class.new do
      hooks.each do |hook|
        define_method(hook.name) do |*args|
          # NOOP
        end
      end
    end
    const_set(const_name, listener_class)
  else
    super(const_name)
  end
end

#define_hook(name, *params) ⇒ Object

Define a new hook name. If params are supplied, they will become the hook’s named parameters.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/hookr.rb', line 73

def define_hook(name, *params)
  fetch_or_create_hooks << make_hook(name, nil, params)

  # We must use string evaluation in order to define a method that can
  # receive a block.
  instance_eval(<<-END)
    def #{name}(handle_or_method=nil, &block)
      add_callback(:#{name}, handle_or_method, &block)
    end
  END
  module_eval(<<-END)
    def #{name}(handle=nil, &block)
      add_external_callback(:#{name}, handle, block)
    end
  END
end

#extended(object) ⇒ Object



106
107
108
109
110
111
# File 'lib/hookr.rb', line 106

def extended(object)
  super(object)
  class << object
    include ::HookR::Hooks
  end
end

#hooksObject

Returns the hooks exposed by this class



67
68
69
# File 'lib/hookr.rb', line 67

def hooks
  result = fetch_or_create_hooks.dup.freeze
end