Module: Gaskit::Hookable::ClassMethods

Defined in:
lib/gaskit/hookable.rb

Constant Summary collapse

VALID_HOOK_TYPES =
i[before after around].freeze

Instance Method Summary collapse

Instance Method Details

#after(proc = nil, &block) ⇒ Object



37
38
39
# File 'lib/gaskit/hookable.rb', line 37

def after(proc = nil, &block)
  use_hook(:after, proc, &block)
end

#around(proc = nil, &block) ⇒ Object



33
34
35
# File 'lib/gaskit/hookable.rb', line 33

def around(proc = nil, &block)
  use_hook(:around, proc, &block)
end

#before(proc = nil, &block) ⇒ Object



29
30
31
# File 'lib/gaskit/hookable.rb', line 29

def before(proc = nil, &block)
  use_hook(:before, proc, &block)
end

#hooks_enabled?Boolean

Returns:



41
42
43
# File 'lib/gaskit/hookable.rb', line 41

def hooks_enabled?
  @enabled || false
end

#inline_hooksObject



53
54
55
# File 'lib/gaskit/hookable.rb', line 53

def inline_hooks
  @inline_hooks ||= Concurrent::Hash.new { |h, k| h[k] = [] }
end

#registered_hooksObject



49
50
51
# File 'lib/gaskit/hookable.rb', line 49

def registered_hooks
  @registered_hooks ||= []
end

#run_all_hooks?Boolean

Returns:



45
46
47
# File 'lib/gaskit/hookable.rb', line 45

def run_all_hooks?
  @run_all_hooks || false
end

#use_hook(type, proc = nil, &block) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/gaskit/hookable.rb', line 21

def use_hook(type, proc = nil, &block)
  hook = block_given? ? block : proc
  type, hook = validate_hook!(type, hook)

  inline_hooks[type] << hook
  @enabled = true
end

#use_hooks(*tags, all: tags.empty?) ⇒ Object



14
15
16
17
18
19
# File 'lib/gaskit/hookable.rb', line 14

def use_hooks(*tags, all: tags.empty?)
  registered_hooks.concat(tags.map(&:to_sym)).uniq!

  @run_all_hooks = all
  @enabled = true
end