Class: QED::Evaluator
Overview
Demonstrandum Evaluator
Instance Method Summary collapse
-
#advise!(signal, *args) ⇒ Object
Dispatch event to observers and advice.
- #call_matchers(section) ⇒ Object
-
#call_signals(type, *args) ⇒ Object
React to an event.
- #error!(step, exception) ⇒ Object
- #evaluate(step) ⇒ Object
-
#evaluate_code(step) ⇒ Object
Evaluate a demo step.
- #evaluate_data(step) ⇒ Object
- #evaluate_desc(step) ⇒ Object
- #evaluate_head(step) ⇒ Object
-
#evaluate_links(step) ⇒ Object
TODO: Not sure how to handle loading links in –comment runner mode.
- #fail!(step, exception) ⇒ Object
- #import!(file) ⇒ Object
-
#initialize(script, *observers) ⇒ Evaluator
constructor
A new instance of Evaluator.
-
#match_string_to_regexp(str) ⇒ Object
Convert matching string into a regular expression.
- #pass!(step) ⇒ Object
- #run ⇒ Object
-
#run_steps ⇒ Object
process.
Constructor Details
#initialize(script, *observers) ⇒ Evaluator
Returns a new instance of Evaluator.
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/qed/evaluator.rb', line 9 def initialize(script, *observers) @script = script @steps = script.steps #@file = script.file #@scope = script.scope #@binding = script.binding #@advice = script.advice @observers = observers end |
Instance Method Details
#advise!(signal, *args) ⇒ Object
Dispatch event to observers and advice.
167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/qed/evaluator.rb', line 167 def advise!(signal, *args) @observers.each{ |o| o.update(signal, *args) } #@script.advise(signal, *args) case signal when :when call_matchers(*args) else call_signals(signal, *args) end end |
#call_matchers(section) ⇒ Object
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/qed/evaluator.rb', line 223 def call_matchers(section) match = section.text args = section.arguments @script.applique.each do |a| matchers = a.__matchers__ 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) @script.scope.instance_exec(*params, &proc) end end end end |
#call_signals(type, *args) ⇒ Object
React to an event.
TODO: Should events short circuit on finding first match? In other words, should there be only one of each type of signal ragardless of how many applique layers?
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/qed/evaluator.rb', line 189 def call_signals(type, *args) @script.applique.each do |a| signals = a.__signals__ proc = signals[type.to_sym] #signals.each do |set| #proc = set[type.to_sym] #proc.call(*args) if proc @script.scope.instance_exec(*args, &proc) if proc #end end #@script.applique.each do |a| # signals = a.__signals__ # proc = signals[type.to_sym] # if proc # @script.scope.instance_exec(*args, &proc) # break # end #end #meth = "qed_#{type}" #if @script.scope.respond_to?(meth) # meth = @script.scope.method(meth) # if meth.arity == 0 # meth.call # else # meth.call(*args) # end #end #@script.scope.__send__(meth, *args) end |
#error!(step, exception) ⇒ Object
154 155 156 157 |
# File 'lib/qed/evaluator.rb', line 154 def error!(step, exception) advise!(:error, step, exception) #raise exception end |
#evaluate(step) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/qed/evaluator.rb', line 37 def evaluate(step) type = step.type advise!(:before_step, step) #, @script.file) advise!("before_#{type}".to_sym, step) #, @script.file) case type when :head evaluate_head(step) when :desc evaluate_desc(step) when :data evaluate_data(step) when :code evaluate_code(step) else raise "fatal: unknown #{type}" end advise!("after_#{type}".to_sym, step) #, @script.file) advise!(:after_step, step) #, @script.file) end |
#evaluate_code(step) ⇒ Object
Evaluate a demo step.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/qed/evaluator.rb', line 104 def evaluate_code(step) begin advise!(:code, step) @script.evaluate(step.code, step.lineno) rescue SystemExit pass!(step) # TODO: skip!(step) #rescue Assertion => exception # fail!(step, exception) rescue Exception => exception if exception.assertion? fail!(step, exception) else error!(step, exception) end else pass!(step) end end |
#evaluate_data(step) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/qed/evaluator.rb', line 84 def evaluate_data(step) #advise!(:data, step) begin advise!(:data, step) rescue SystemExit pass!(step) #rescue Assertion => exception # fail!(step, exception) rescue Exception => exception if exception.assertion? fail!(step, exception) else error!(step, exception) end else pass!(step) end end |
#evaluate_desc(step) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/qed/evaluator.rb', line 63 def evaluate_desc(step) evaluate_links(step) begin advise!(:desc, step) advise!(:when, step) # triggers matchers rescue SystemExit pass!(step) #rescue Assertion => exception # fail!(step, exception) rescue Exception => exception if exception.assertion? fail!(step, exception) else error!(step, exception) end else pass!(step) end end |
#evaluate_head(step) ⇒ Object
58 59 60 |
# File 'lib/qed/evaluator.rb', line 58 def evaluate_head(step) advise!(:head, step) end |
#evaluate_links(step) ⇒ Object
TODO: Not sure how to handle loading links in –comment runner mode. TODO: Do not think Scope should be reuseud by imported demo.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/qed/evaluator.rb', line 125 def evaluate_links(step) step.text.scan(/\[qed:\/\/(.*?)\]/) do |match| file = $1 # relative to demo script if File.exist?(File.join(@script.directory,file)) file = File.join(@script.directory,file) end # ruby or another demo case File.extname(file) when '.rb' import!(file) else Demo.new(file, :scope=>@script.scope).run end end end |
#fail!(step, exception) ⇒ Object
148 149 150 151 |
# File 'lib/qed/evaluator.rb', line 148 def fail!(step, exception) advise!(:fail, step, exception) #raise exception end |
#import!(file) ⇒ Object
160 161 162 163 164 |
# File 'lib/qed/evaluator.rb', line 160 def import!(file) advise!(:unload) # should this also occur just befor after_demo ? eval(File.read(file), @script.binding, file) advise!(:load, file) end |
#match_string_to_regexp(str) ⇒ Object
Convert matching string into a regular expression. If the string contains double parenthesis, such as ((.*?)), then the text within them is treated as in regular expression and kept verbatium.
TODO: Better way to isolate regexp. Maybe ?:(.*?) or /(.*?)/.
TODO: Now that we can use multi-patterns, do we still need this?
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/qed/evaluator.rb', line 264 def match_string_to_regexp(str) str = str.split(/(\(\(.*?\)\))(?!\))/).map{ |x| x =~ /\A\(\((.*)\)\)\Z/ ? $1 : Regexp.escape(x) }.join str = str.gsub(/\\\s+/, '\s+') Regexp.new(str, Regexp::IGNORECASE) #rexps = [] #str = str.gsub(/\(\((.*?)\)\)/) do |m| # rexps << '(' + $1 + ')' # "\0" #end #str = Regexp.escape(str) #rexps.each do |r| # str = str.sub("\0", r) #end #str = str.gsub(/(\\\ )+/, '\s+') #Regexp.new(str, Regexp::IGNORECASE) end |
#pass!(step) ⇒ Object
143 144 145 |
# File 'lib/qed/evaluator.rb', line 143 def pass!(step) advise!(:pass, step) end |
#run ⇒ Object
22 23 24 25 26 27 |
# File 'lib/qed/evaluator.rb', line 22 def run advise!(:before_demo, @script) advise!(:demo, @script) run_steps advise!(:after_demo, @script) end |
#run_steps ⇒ Object
process
30 31 32 33 34 |
# File 'lib/qed/evaluator.rb', line 30 def run_steps #process @steps.each do |step| evaluate(step) end end |