Class: Orbit::Routing::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/orbit/routing/path.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Path

Returns a new instance of Path.



6
7
8
9
# File 'lib/orbit/routing/path.rb', line 6

def initialize(path)
  @name = path
  @regex, @keys = compile
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/orbit/routing/path.rb', line 4

def name
  @name
end

#regexObject (readonly)

Returns the value of attribute regex.



4
5
6
# File 'lib/orbit/routing/path.rb', line 4

def regex
  @regex
end

Instance Method Details

#compileObject



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/orbit/routing/path.rb', line 22

def compile
  return compile_string if name.respond_to? :to_str
    
  if is_hash?
    [name, name.keys]
  elsif is_object?
    [name, name.names]
  elsif name.respond_to? :match
    [name, []]
  else
    raise TypeError, name
  end
end

#get_params(path) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/orbit/routing/path.rb', line 11

def get_params(path)
  matches = regex.match(path)
  {}.tap do |params|
    keys.each_with_index do |key, index|
      match_index = index + 1

      params[key.to_sym] = (matches[match_index]) if matches && matches.size > match_index
    end
  end
end