Module: Hooks::ClassMethods

Defined in:
lib/rbind/core/hooks.rb

Instance Method Summary collapse

Instance Method Details

#__initializeObject



15
16
17
18
19
20
# File 'lib/rbind/core/hooks.rb', line 15

def __initialize
    @hook_names ||= Set.new
    @hook_callbacks ||= Hash.new do |h,k|
        h[k] = Array.new
    end
end

#callback?(name) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/rbind/core/hooks.rb', line 41

def callback?(name)
    !callbacks_for_hook(name).empty?
end

#callbacks_for_hook(name) ⇒ Object

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
# File 'lib/rbind/core/hooks.rb', line 22

def callbacks_for_hook(name)
    name = name.to_sym
    raise ArgumentError,"hook #{name} is not known" unless hook?(name)
    if @hook_callbacks.has_key?(name)
        @hook_callbacks[name]
    else
        []
    end
end

#define_hook(name) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/rbind/core/hooks.rb', line 5

def define_hook(name)
    name = name.to_sym
    @hook_names << name
    instance_eval %Q{
        def #{name}(&block)
            @hook_callbacks[:#{name}] << block
        end
        }
end

#hook?(name) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
# File 'lib/rbind/core/hooks.rb', line 32

def hook?(name)
    name = name.to_sym
    if @hook_names.include?(name)
        true
    else
        false
    end
end

#run_hook(name, *args) ⇒ Object



45
46
47
48
49
50
# File 'lib/rbind/core/hooks.rb', line 45

def run_hook(name,*args)
    callbacks = callbacks_for_hook(name)
    callbacks.map do |c|
        c.call(*args)
    end
end