Class: Reins::Routes::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/reins/routes/rule.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(verb:, pattern:, dest:, name: nil, constraints: {}) ⇒ Rule

Returns a new instance of Rule.



6
7
8
9
10
11
12
13
# File 'lib/reins/routes/rule.rb', line 6

def initialize(verb:, pattern:, dest:, name: nil, constraints: {})
  @verb = verb
  @pattern = pattern
  @dest = dest
  @name = name
  @constraints = constraints
  @vars, @regexp = compile(pattern)
end

Instance Attribute Details

#constraintsObject (readonly)

Returns the value of attribute constraints.



4
5
6
# File 'lib/reins/routes/rule.rb', line 4

def constraints
  @constraints
end

#destObject (readonly)

Returns the value of attribute dest.



4
5
6
# File 'lib/reins/routes/rule.rb', line 4

def dest
  @dest
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/reins/routes/rule.rb', line 4

def name
  @name
end

#patternObject (readonly)

Returns the value of attribute pattern.



4
5
6
# File 'lib/reins/routes/rule.rb', line 4

def pattern
  @pattern
end

#regexpObject (readonly)

Returns the value of attribute regexp.



4
5
6
# File 'lib/reins/routes/rule.rb', line 4

def regexp
  @regexp
end

#varsObject (readonly)

Returns the value of attribute vars.



4
5
6
# File 'lib/reins/routes/rule.rb', line 4

def vars
  @vars
end

#verbObject (readonly)

Returns the value of attribute verb.



4
5
6
# File 'lib/reins/routes/rule.rb', line 4

def verb
  @verb
end

Instance Method Details

#match(request_verb, path) ⇒ Object

Returns extracted params Hash on a full match, nil otherwise.



16
17
18
19
20
# File 'lib/reins/routes/rule.rb', line 16

def match(request_verb, path)
  return nil unless verb_matches?(request_verb)

  match_path(path)
end

#matches_path?(path) ⇒ Boolean

True if the path (and constraints) match — verb-agnostic. Used to distinguish 404 from 405.

Returns:

  • (Boolean)


24
25
26
# File 'lib/reins/routes/rule.rb', line 24

def matches_path?(path)
  !match_path(path).nil?
end

#verb_matches?(request_verb) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/reins/routes/rule.rb', line 28

def verb_matches?(request_verb)
  @verb == :all || @verb == request_verb
end

#verbs_for_allow_headerObject



32
33
34
# File 'lib/reins/routes/rule.rb', line 32

def verbs_for_allow_header
  @verb == :all ? %w[GET POST PUT PATCH DELETE] : [@verb.to_s.upcase]
end