Class: UnifiedMatchers::UnifiedMatcher::Rule

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(matcher, name, *args, &block) ⇒ Rule

Returns a new instance of Rule.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/unified_matchers/unified_matcher.rb', line 39

def initialize matcher, name, *args, &block
  unless name.is_a? Symbol
    raise ArgumentError, "The name must be a symbol, not: #{name}"
  end
  @name = name
  @conditions = {}
  @message = nil
  @pos = 0
  for arg in args.flatten do
    setup_arg arg, matcher
  end
  if @conditions.empty?
    @conditions[:respond_to] = @name
  end
  case @pos
  when 0
    update_order name, matcher.last_defined_rule, 1, matcher #after
  when 1
  else raise ArgumentError,
          "Don't define more than one position (:before or :after)"
  end
  if block.nil?
    meth = @conditions[:respond_to]
    @block = lambda { |x| x.send meth }
    @message ||= "%x.#{meth}"
  else
    @block = block
    @message ||= "%x.<rule #@name>"
  end
  matcher.last_defined_rule = name
  matcher.rules = {@name => self}
end

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



37
38
39
# File 'lib/unified_matchers/unified_matcher.rb', line 37

def message
  @message
end

#nameObject (readonly)

Returns the value of attribute name.



37
38
39
# File 'lib/unified_matchers/unified_matcher.rb', line 37

def name
  @name
end

Instance Method Details

#activable?(anObject) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/unified_matchers/unified_matcher.rb', line 108

def activable? anObject
  activable_rec @conditions, anObject
end

#activable_rec(condition, anObject) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/unified_matchers/unified_matcher.rb', line 112

def activable_rec condition, anObject
  case condition
  when Class then anObject.is_a? condition
  when Symbol then anObject.respond_to? condition
  when Hash
    if condition.size != 1
      raise ArgumentError, "Too much conditions, use :all or :any to" +
      "combine them. (rule: #{name})"
    end
    k,v = condition.to_a.first
    case k
    when :is_a then anObject.is_a? v
    when :respond_to then anObject.respond_to? v
    when :all then v.all? { |x| activable_rec x, anObject }
    when :any then v.any? { |x| activable_rec x, anObject }
    else raise ArgumentError, "Bad key: #{k}"
    end
  else raise ArgumentError, "Bad condition: #{condition}"
  end
end

#activate(x) ⇒ Object



133
134
135
136
137
138
139
# File 'lib/unified_matchers/unified_matcher.rb', line 133

def activate x
  case @block.arity
  when -1 then x.instance_eval(&@block)
  when  1 then @block[x]
  else raise ArgumentError, "Bad block arity: #{@block.arity}"
  end
end

#update_order(new, ref, offset, m) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/unified_matchers/unified_matcher.rb', line 94

def update_order new, ref, offset, m
  m.order = m.order - [new]
  i = m.order.index ref
  if i.nil?
    if m.last_defined_rule.nil?
      m.order = [new]
    else
      raise ArgumentError, "Undefined rule: #{ref}"
    end
  else
    m.order = m.order[0..i-1+offset] + [new] + m.order[i+offset..-1]
  end
end