Class: QED::Advice

Inherits:
Object
  • Object
show all
Defined in:
lib/qed/advice.rb

Overview

Advice

This class tracks advice defined by demonstrandum and applique. It is instantiated in Scope, so that the advice methods will have access to the same local binding as the scripts themselves.

There are two types of advice: *pattern matchers* and *event signals*.

Pattern Matchers (When)

Matchers are evaluated in Scope context, via #instance_exec, so that the advice methods will have access to the same scope as the demonstrandum themselves.

Event Signals (Before, After)

Event advice are triggered on symbolic targets which represent an event in the evaluation process, such as before any demo is run, or after all demo finish running.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAdvice

Returns a new instance of Advice.



34
35
36
37
# File 'lib/qed/advice.rb', line 34

def initialize
  @matchers = []
  @signals  = [{}]
end

Instance Attribute Details

#matchersObject (readonly)

Returns the value of attribute matchers.



28
29
30
# File 'lib/qed/advice.rb', line 28

def matchers
  @matchers
end

#signalsObject (readonly)

Returns the value of attribute signals.



31
32
33
# File 'lib/qed/advice.rb', line 31

def signals
  @signals
end

Instance Method Details

#add_event(type, &procedure) ⇒ Object



51
52
53
# File 'lib/qed/advice.rb', line 51

def add_event(type, &procedure)
  @signals.last[type.to_sym] = procedure
end

#add_match(patterns, &procedure) ⇒ Object



56
57
58
# File 'lib/qed/advice.rb', line 56

def add_match(patterns, &procedure)
  @matchers << [patterns, procedure]
end

#call(scope, type, *args) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/qed/advice.rb', line 40

def call(scope, type, *args)
  case type
  when :when
    call_matchers(scope, *args)
  else
    #@events.call(scope, type, *args)
    call_signals(scope, type, *args)
  end
end

#call_matchers(scope, section) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/qed/advice.rb', line 70

def call_matchers(scope, section)
  match = section.text
  args  = section.args

  @matchers.each do |(patterns, proc)|
    compare = match
    matched = true
    params  = []
    patterns.each do |pattern|
      case pattern
      when Regexp
        regex = pattern
      else
        regex = match_string_to_regexp(pattern)
      end
      if md = regex.match(compare)
        params.concat(md[1..-1])
        compare = md.post_match
      else
        matched = false
        break
      end
    end
    if matched
      params += args
      #proc.call(*params)
      scope.instance_exec(*params, &proc)
    end
  end
end

#call_signals(scope, type, *args) ⇒ Object

React to an event.



61
62
63
64
65
66
67
# File 'lib/qed/advice.rb', line 61

def call_signals(scope, type, *args)
  @signals.each do |set|
    proc = set[type.to_sym]
    #proc.call(*args) if proc
    scope.instance_exec(*args, &proc) if proc
  end
end

#signals_clear(type = nil) ⇒ Object

Clear advice.



112
113
114
115
116
117
118
# File 'lib/qed/advice.rb', line 112

def signals_clear(type=nil)
  if type
    @signals.each{ |set| set.delete(type.to_sym) }
  else
    @signals = [{}]
  end
end

#signals_resetObject

Clear last set of advice.



102
103
104
# File 'lib/qed/advice.rb', line 102

def signals_reset
  @signals.pop
end

#signals_setupObject



107
108
109
# File 'lib/qed/advice.rb', line 107

def signals_setup
  @signals.push({})
end