Class: Newark::Route::Constraint

Inherits:
Object
  • Object
show all
Defined in:
lib/newark/route.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field, match_or_matchers) ⇒ Constraint

Returns a new instance of Constraint.



71
72
73
74
# File 'lib/newark/route.rb', line 71

def initialize(field, match_or_matchers)
  @field    = field
  @matchers = make_matchers_regexp(match_or_matchers)
end

Instance Attribute Details

#fieldObject (readonly)

Returns the value of attribute field.



63
64
65
# File 'lib/newark/route.rb', line 63

def field
  @field
end

#matchersObject (readonly)

Returns the value of attribute matchers.



63
64
65
# File 'lib/newark/route.rb', line 63

def matchers
  @matchers
end

Class Method Details

.load(constraints) ⇒ Object

Expects a hash of constraints



66
67
68
69
# 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

Instance Method Details

#match?(request) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
80
81
82
83
84
85
86
# 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