Class: Spectroscope::Hooks

Inherits:
Object
  • Object
show all
Defined in:
lib/spectroscope/hooks.rb

Overview

Context advice.

Defined Under Namespace

Classes: Hook

Instance Method Summary collapse

Constructor Details

#initializeHooks

New case instance.



10
11
12
13
# File 'lib/spectroscope/hooks.rb', line 10

def initialize
  @hooks_all  = {:before=>[], :after=>[]}
  @hooks_each = {:before=>[], :after=>[]}
end

Instance Method Details

#add(tense, range, *tags, &proc) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/spectroscope/hooks.rb', line 26

def add(tense, range, *tags, &proc)
  case range
  when :all
    raise ArgumentError, "tagged #{tense}-all advice is useless" unless tags.empty?
    @hooks_all[tense]  << Hook.new(tense, range, *tags, &proc)
  when :each
    @hooks_each[tense] << Hook.new(tense, range, *tags, &proc)
  else
    raise ArgumentError, "range must be :all or :each"
  end
end

#initialize_copy(original) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/spectroscope/hooks.rb', line 16

def initialize_copy(original)
  original.hooks_all do |tense, hooks|
    hooks_all[tense] = hooks.clone
  end
  original.hooks_each do |tense, hooks|
    hooks_each[tense] = hooks.clone
  end
end

#run(target, tense, range, scope) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/spectroscope/hooks.rb', line 39

def run(target, tense, range, scope)
  case range
  when :all
    hooks_all[tense].each do |hook|
      scope.instance_eval(&hook)
    end
  when :each
    hooks_each[tense].each do |hook|
      scope.instance_eval(&hook) if hook.match?(target)
    end
  else
    raise ArgumentError, "range must be :all or :each"
  end
end