Class: Wayfarer::Routing::PathRule

Inherits:
Rule
  • Object
show all
Defined in:
lib/wayfarer/routing/path_rule.rb

Instance Attribute Summary collapse

Attributes inherited from Rule

#child_rules, #target_action

Instance Method Summary collapse

Methods inherited from Rule

#build_child_rule_chain_from_options, #filetypes, #host, #if, #invoke, #matches?, #matching_rule_chain, #path, #protocol, #query, #uri

Constructor Details

#initialize(arg, opts = {}, &proc) ⇒ PathRule

Returns a new instance of PathRule.


11
12
13
14
15
16
17
18
19
# File 'lib/wayfarer/routing/path_rule.rb', line 11

def initialize(arg, opts = {}, &proc)
  @matcher = if arg.is_a? String
               Mustermann.new(arg, type: Wayfarer.config.mustermann_type)
             else
               arg
             end

  super(opts, &proc)
end

Instance Attribute Details

#matcherObject (readonly)

Returns the value of attribute matcher


9
10
11
# File 'lib/wayfarer/routing/path_rule.rb', line 9

def matcher
  @matcher
end

Instance Method Details

#params(uri) ⇒ Object


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/wayfarer/routing/path_rule.rb', line 21

def params(uri)
  return {} unless match!(uri)

  path = uri.path

  if @matcher.is_a? Mustermann
    @matcher.params(path)
  else
    captures = @matcher.match(full_path(uri)).captures

    captures.each.with_index.reduce({}) do |hash, (capture, i)|
      hash.merge(i.to_s => capture)
    end
  end
end