Class: QED::Advice

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

Overview

Advice

This class tracks advice defined by demonstrandum and applique. Advice are evaluated in Scope, so that they 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 when they match a description.

Event Signals (Before, After)

Event advice are triggered on symbolic targets which represent an event in the evaluation process, such as before an example is run, or after a demo finishes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAdvice

Returns a new instance of Advice.



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

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

Instance Attribute Details

#matchersObject (readonly)

Returns the value of attribute matchers.



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

def matchers
  @matchers
end

#signalsObject (readonly)

Returns the value of attribute signals.



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

def signals
  @signals
end

Instance Method Details

#add_event(type, &procedure) ⇒ Object



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

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

#add_match(patterns, &procedure) ⇒ Object



62
63
64
# File 'lib/qed/advice.rb', line 62

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

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



47
48
49
50
51
52
53
54
# File 'lib/qed/advice.rb', line 47

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

#call_matchers(scope, section) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/qed/advice.rb', line 76

def call_matchers(scope, section)
  match = section.text
  args  = section.arguments
  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.



67
68
69
70
71
72
73
# File 'lib/qed/advice.rb', line 67

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.



117
118
119
120
121
122
123
# File 'lib/qed/advice.rb', line 117

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.



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

def signals_reset
  @signals.pop
end

#signals_setupObject



112
113
114
# File 'lib/qed/advice.rb', line 112

def signals_setup
  @signals.push({})
end