Class UnifiedMatchers::UnifiedMatcher::Rule
In: lib/unified_matchers/unified_matcher.rb
Parent: Object

Methods

Attributes

message  [R] 
name  [R] 

Public Class methods

[Source]

# 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

Public Instance methods

[Source]

# File lib/unified_matchers/unified_matcher.rb, line 109
      def activable? anObject
        activable_rec @conditions, anObject
      end

[Source]

# 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

[Source]

# 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

[Source]

# 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

[Validate]