Module: Hook

Defined in:
lib/mixers/hook.rb

Overview

TODO: hooks should be an inheritor

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.append_features(base) ⇒ Object



7
8
9
# File 'lib/mixers/hook.rb', line 7

def self.append_features(base)
  base.extend self
end

Instance Method Details

#hook(name) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mixers/hook.rb', line 15

def hook(name)
  name = name.to_sym

  (class << self; self; end).class_eval %{
    def #{name}(meth=nil, &blk)
      hooks[:#{name}] << (meth || blk)
    end
  }

  module_eval %{
    def #{name}(*args)
      self.class.hooks[:#{name}].each do |blk|
        if Proc === blk
          instance_exec(:#{name}, *args, &blk)
        else
          __send__(blk, :#{name}, *args)
        end
      end
    end
  }
end

#hooksObject



11
12
13
# File 'lib/mixers/hook.rb', line 11

def hooks
  @hooks ||= Hash.new{ |h,k| h[k] = [] }
end