module Needle::IncludeExclude

A module encapsulating the functionality of a service with include/exclude functionality. Such functionality involves a the ability to specify a pair of include and exclude arrays, each of which must be an array of method names that should be included or excluded from some kind of processing.

Constants

PATTERN

This is the regular expression for parsing elements in an include or exclude array.

Private Instance Methods

build_map( array ) click to toggle source

This is a utility function for converting an array of strings representing method name patterns, into an array of IncludeExcludePattern instances.

# File lib/needle/include-exclude.rb, line 46
def build_map( array )
  ( array || [] ).map do |pattern|
    unless pattern =~ PATTERN
      raise InterceptorConfigurationError,
        "invalid logging interceptor method pattern: #{pattern.inspect}"
    end

    name = $1
    comparitor = $2
    arity = ( $3 || -1 ).to_i

    comparitor ||= ">" if arity < 0
    comparitor ||= "="
      
    IncludeExcludePattern.new( Regexp.new( "^" + name + "$" ),
                               comparitor,
                               arity )
  end
end
match( context ) click to toggle source

Returns false if the given context object “matches” any of the exclude patterns without matching any of the include patterns. The context object must respond to the :sym and :args messages, where :sym is a symbol identifying the method being matched, and :args is an array of arguments that will be sent to that method.

# File lib/needle/include-exclude.rb, line 73
def match( context )
  match = true

  @excludes.each do |pattern|
    if match_pattern( context, pattern )
      match = false
      break
    end
  end

  unless match
    @includes.each do |pattern|
      if match_pattern( context, pattern )
        match = true
        break
      end
    end
  end

  return match
end
match_pattern( context, pattern ) click to toggle source

Returns true if the given context matches the given pattern, and false otherwise.

# File lib/needle/include-exclude.rb, line 98
def match_pattern( context, pattern )
  if context.sym.to_s =~ pattern.name
    case pattern.comparitor
      when "<"
        return context.args.length < pattern.arity
      when ">"
        return context.args.length > pattern.arity
      when "="
        return context.args.length == pattern.arity
    end
  end

  return false
end