Class: Parameterised::Paths::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/parameterised/paths/path.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Path

Path constructor

Parameters:

  • path (String)

    A potentially parameterised path



11
12
13
14
15
16
# File 'lib/parameterised/paths/path.rb', line 11

def initialize(path)
  @original_path = path
  @parameterised = false
  @params = []
  @path = parse_path(path)
end

Instance Method Details

#match(path) ⇒ PathMatch|nil

Attempts to match a path against our parameterised path

Parameters:

  • path (String)

    The path we want to attempt to match

Returns:

  • (PathMatch|nil)

    A PathMatch if we have encountered a match or nil when there if no match



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/parameterised/paths/path.rb', line 24

def match(path)
  path = normalise_path(path)

  unless @path.is_a?(Regexp)
    return @path == path ? PathMatch.new(@original_path) : nil
  end

  match = path.match(@path)

  return nil if match.nil?

  pos = 0
  param = {}
  match.captures.each do |capture|
    capture.sub!('/', '')
    param[@params[pos]] = capture
    pos += 1
  end

  PathMatch.new(@original_path, param)
end