Class: CptHook::DSL::HookDefinitions

Inherits:
Object
  • Object
show all
Defined in:
lib/cpt_hook/dsl/hook_definitions.rb

Instance Method Summary collapse

Constructor Details

#initializeHookDefinitions

Returns a new instance of HookDefinitions.



5
6
7
# File 'lib/cpt_hook/dsl/hook_definitions.rb', line 5

def initialize
  @hooks = []
end

Instance Method Details

#after(method_to_hook, &block) ⇒ Object



21
22
23
# File 'lib/cpt_hook/dsl/hook_definitions.rb', line 21

def after(method_to_hook, &block)
  _hook_definition(method_to_hook, :after, &block)
end

#before(method_to_hook, &block) ⇒ Object



17
18
19
# File 'lib/cpt_hook/dsl/hook_definitions.rb', line 17

def before(method_to_hook, &block)
  _hook_definition(method_to_hook, :before, &block)
end

#clone(other) ⇒ Object



25
26
27
28
# File 'lib/cpt_hook/dsl/hook_definitions.rb', line 25

def clone(other)
  @hooks = other.hooks.map { |h| h.dup }
  self
end

#dupObject



30
31
32
# File 'lib/cpt_hook/dsl/hook_definitions.rb', line 30

def dup
  HookDefinitions.new.clone(self)
end

#hooked_methodsObject



13
14
15
# File 'lib/cpt_hook/dsl/hook_definitions.rb', line 13

def hooked_methods
  @hooks.map { |h| h.method }.uniq
end

#hooks(hook_type = nil) ⇒ Object



9
10
11
# File 'lib/cpt_hook/dsl/hook_definitions.rb', line 9

def hooks(hook_type = nil)
  hook_type.nil? ? @hooks : @hooks.select { |h| h.hook_type == hook_type }
end

#merge(other) ⇒ Object



34
35
36
# File 'lib/cpt_hook/dsl/hook_definitions.rb', line 34

def merge(other)
  dup.merge!(other)
end

#merge!(other) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cpt_hook/dsl/hook_definitions.rb', line 38

def merge!(other)
  other.hooks.each do |hook|
    my_hook = @hooks.find { |h| (h.method == hook.method) && (h.hook_type == hook.hook_type) }
    if my_hook
      my_hook.merge!(hook)
    else
      @hooks << hook
    end
  end
  self
end