Class: UnifiedMatchers::UnifiedMatcher::Rule
- Defined in:
- lib/unified_matchers/unified_matcher.rb
Instance Attribute Summary collapse
-
#message ⇒ Object
readonly
Returns the value of attribute message.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #activable?(anObject) ⇒ Boolean
- #activable_rec(condition, anObject) ⇒ Object
- #activate(x) ⇒ Object
-
#initialize(matcher, name, *args, &block) ⇒ Rule
constructor
A new instance of Rule.
- #update_order(new, ref, offset, m) ⇒ Object
Constructor Details
#initialize(matcher, name, *args, &block) ⇒ Rule
Returns a new instance of Rule.
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 71 |
# File 'lib/unified_matchers/unified_matcher.rb', line 40 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
#message ⇒ Object (readonly)
Returns the value of attribute message.
38 39 40 |
# File 'lib/unified_matchers/unified_matcher.rb', line 38 def @message end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
38 39 40 |
# File 'lib/unified_matchers/unified_matcher.rb', line 38 def name @name end |
Instance Method Details
#activable?(anObject) ⇒ Boolean
109 110 111 |
# File 'lib/unified_matchers/unified_matcher.rb', line 109 def activable? anObject activable_rec @conditions, anObject end |
#activable_rec(condition, anObject) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/unified_matchers/unified_matcher.rb', line 113 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
134 135 136 137 138 139 140 |
# File 'lib/unified_matchers/unified_matcher.rb', line 134 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
95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/unified_matchers/unified_matcher.rb', line 95 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 |