class Newark::Route::Constraint

Attributes

field[R]
matchers[R]

Public Class Methods

load(constraints) click to toggle source

Expects a hash of constraints

# File lib/newark/route.rb, line 66
def self.load(constraints)
  fail ArgumentError unless constraints.is_a?(Hash)
  constraints.map { |field, matcher| Constraint.new(field, matcher) }
end
new(field, match_or_matchers) click to toggle source
# File lib/newark/route.rb, line 71
def initialize(field, match_or_matchers)
  @field    = field
  @matchers = make_matchers_regexp(match_or_matchers)
end

Public Instance Methods

match?(request) click to toggle source
# File lib/newark/route.rb, line 76
def match?(request)
  if request.respond_to?(field)
    constrained = request.send(field)

    if matchers.is_a?(Hash) && constrained.is_a?(Hash)
      hash_match?(request, constrained, matchers)
    else
      constrained =~ matchers
    end
  end
end

Private Instance Methods

hash_match?(request, constrained, matchers) click to toggle source
# File lib/newark/route.rb, line 90
def hash_match?(request, constrained, matchers)
  matchers.all? { |key, matcher|
    constrained[key] =~ matcher
  }
end
make_matchers_regexp(match_or_matchers) click to toggle source
# File lib/newark/route.rb, line 96
def make_matchers_regexp(match_or_matchers)
  if match_or_matchers.is_a? Hash
    {}.tap do |matchers|
      match_or_matchers.each do |k, v|
        matchers[k] = matcher_to_regex(v)
      end
    end
  else
    matcher_to_regex(match_or_matchers)
  end
end
matcher_to_regex(matcher) click to toggle source
# File lib/newark/route.rb, line 108
def matcher_to_regex(matcher)
  return matcher if matcher.is_a? Regexp
  /^#{matcher.to_s}$/
end