Class: UnifiedMatchers::UnifiedMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/unified_matchers/unified_matcher.rb

Defined Under Namespace

Classes: Rule

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.delete_final(aClass) ⇒ Object



168
169
170
# File 'lib/unified_matchers/unified_matcher.rb', line 168

def delete_final aClass
  self.ref_triggers = {aClass => nil}
end

.delete_rule(aRuleName) ⇒ Object



162
163
164
165
166
# File 'lib/unified_matchers/unified_matcher.rb', line 162

def delete_rule aRuleName
  self.last_defined_rule = nil if last_defined_rule == aRuleName
  self.order = order - [aRuleName]
  self.rules = {aRuleName => nil}
end

.final(ref_class, my_class, options = nil, &block) ⇒ Object

Raises:

  • (ArgumentError)


152
153
154
155
156
157
158
159
160
# File 'lib/unified_matchers/unified_matcher.rb', line 152

def final ref_class, my_class, options=nil, &block
  raise ArgumentError, 'Need a block' if block.nil?
  raise ArgumentError, "Need a class, not: #{ref_class}" unless ref_class.is_a? Class
  raise ArgumentError, "Need a class, not: #{my_class}" unless my_class.is_a? Class
  options = {} if options.nil?
  raise ArgumentError, "Need a Hash, not: #{options.inspect}" unless options.is_a? Hash
  options[:message] ||= "#{self}.final(#{ref_class} = %ref, #{my_class} = %my)"
  self.ref_triggers = { ref_class => [my_class, options, block] }
end

.redefine_rule(name, *args, &block) ⇒ Object



181
182
183
# File 'lib/unified_matchers/unified_matcher.rb', line 181

def redefine_rule name, *args, &block
  replace_rule name, name, *args, &block
end

.replace_rule(old_name, new_name, *args, &block) ⇒ Object



172
173
174
175
176
177
178
179
# File 'lib/unified_matchers/unified_matcher.rb', line 172

def replace_rule old_name, new_name, *args, &block
  #FIXME verify that there is no before or after
  i = order.index old_name
  delete_rule old_name
  Rule.new(self, new_name, *args, &block)
  self.order = order - [new_name]
  self.order = order[0..i-1] + [new_name] + order[i..-1]
end

.rule(*args, &block) ⇒ Object



148
149
150
# File 'lib/unified_matchers/unified_matcher.rb', line 148

def rule *args, &block
  Rule.new(self, *args, &block)
end

Instance Method Details

#[](ref, my, name = nil) ⇒ Object

class << self



187
188
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
221
222
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
# File 'lib/unified_matchers/unified_matcher.rb', line 187

def [] ( ref, my, name=nil )
  my_target_class, options, ref_trigger = nil, nil
  message_ref = [ref]
  rule_i = 0
  rule = rules[order[rule_i]]
  first = true
  done = []
  until (rule.nil? and not first) or ref_trigger do
    first = false
    ref_class = ref.class
    ref_class.ancestors.each do |aModule|
      if ref_triggers.has_key? aModule
        my_target_class, options, ref_trigger = ref_triggers[aModule]
        break
      end
    end
    if ref_trigger.nil?
      if rule.activable? ref
        check_done rule, done
        message_ref << rule.message
        ref = rule.activate ref
        rule_i = 0
      else
        rule_i += 1
      end
      rule = rules[order[rule_i]]
    end
  end
  if ref_trigger.nil?
    raise ArgumentError, "No final rule with this class #{ref.class}"
  end
  message_my = [my]
  rule_i = 0
  rule = rules[order[rule_i]]
  done.clear
  until my.class <= my_target_class or rule.nil? do
    if rule.activable? my
      check_done rule, done
      message_my << rule.message
      my = rule.activate my
      rule_i = 0
    else
      rule_i += 1
    end
    rule = rules[order[rule_i]]
  end
  unless my.class <= my_target_class
    raise ArgumentError, "No activable rule for #{my}"
  end
  if ref_trigger.arity == 3
    result = ref_trigger[ref, my, self]
  else
    result = ref_trigger[ref, my]
  end
  case result
  when MatcherResults then return result
  else
    result = [result] unless result.is_a? Array
    m = MatcherResult.new(name, ref, my, message_ref, message_my,
                          options[:message], *result)
    return MatcherResults.new(m)
  end
end

#check_done(rule, done) ⇒ Object

def []



251
252
253
254
255
256
257
# File 'lib/unified_matchers/unified_matcher.rb', line 251

def check_done rule, done
  if done.include? rule
    raise RuntimeError, "Possibly infinite rule activation" +
      " (#{rule.name}). Perhaps you should use :before or :after."
  end
  done << rule
end