Class: QED::Patterns
- Inherits:
-
Object
- Object
- QED::Patterns
- Defined in:
- lib/qed/advice/patterns.rb
Overview
Pattern Advice (When)
This class encapsulates “When” advice on plain text.
Matches are evaluated in Scope context, via #instance_exec, so that the advice methods will have access to the same scope as the demonstrandum themselves.
Instance Attribute Summary collapse
-
#when ⇒ Object
readonly
Returns the value of attribute when.
Instance Method Summary collapse
- #add(patterns, &procedure) ⇒ Object
- #call(scope, section) ⇒ Object
-
#initialize ⇒ Patterns
constructor
A new instance of Patterns.
Constructor Details
#initialize ⇒ Patterns
Returns a new instance of Patterns.
15 16 17 |
# File 'lib/qed/advice/patterns.rb', line 15 def initialize @when = [] end |
Instance Attribute Details
#when ⇒ Object (readonly)
Returns the value of attribute when.
13 14 15 |
# File 'lib/qed/advice/patterns.rb', line 13 def when @when end |
Instance Method Details
#add(patterns, &procedure) ⇒ Object
20 21 22 |
# File 'lib/qed/advice/patterns.rb', line 20 def add(patterns, &procedure) @when << [patterns, procedure] end |
#call(scope, section) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/qed/advice/patterns.rb', line 25 def call(scope, section) match = section.text args = section.args @when.each do |(patterns, proc)| compare = match matched = true params = [] patterns.each do |pattern| case pattern when Regexp regex = pattern else regex = when_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 |