Class: Dao::Route

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

Defined Under Namespace

Classes: List

Constant Summary collapse

Default =
'/index'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Route

Returns a new instance of Route.



41
42
43
44
45
46
# File 'lib/dao/route.rb', line 41

def initialize(path)
  replace(path.to_s)
  @keys = Route.keys_for(self).freeze
  @pattern = Route.pattern_for(self).freeze
  freeze
end

Instance Attribute Details

#keysObject

Returns the value of attribute keys.



38
39
40
# File 'lib/dao/route.rb', line 38

def keys
  @keys
end

#patternObject

Returns the value of attribute pattern.



39
40
41
# File 'lib/dao/route.rb', line 39

def pattern
  @pattern
end

Class Method Details

.defaultObject



7
8
9
# File 'lib/dao/route.rb', line 7

def default
  Default
end

.keys_for(route) ⇒ Object



15
16
17
18
# File 'lib/dao/route.rb', line 15

def keys_for(route)
  route = Path.absolute_path_for(route.to_s)
  route.scan(%r{/:[^/]+}).map{|key| key.sub(%r{^/:}, '')}
end

.like?(route) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/dao/route.rb', line 11

def like?(route)
  route.to_s =~ %r{/:[^/]+}
end

.path_for(route, params = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/dao/route.rb', line 26

def path_for(route, params = {})
  path = Path.absolute_path_for(route.to_s)
  params = Map.new(params)
  params.each do |key, val|
    re = %r{/:#{ Regexp.escape(key.to_s) }(\Z|/)}
    repl = "/#{ val.to_s }\\1"
    path.gsub!(re, repl)
  end
  path
end

.pattern_for(route) ⇒ Object



20
21
22
23
24
# File 'lib/dao/route.rb', line 20

def pattern_for(route)
  route = Path.absolute_path_for(route.to_s)
  re = route.gsub(%r{/:[^/]+}, '/([^/]+)')
  /#{ re }/iux
end

Instance Method Details

#match(path) ⇒ Object



56
57
58
# File 'lib/dao/route.rb', line 56

def match(path)
  match = pattern.match(path).to_a
end

#params_for(path) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/dao/route.rb', line 60

def params_for(path)
  match = pattern.match(path).to_a

  unless match.empty?
    map = Map.new
    ignored = match.shift
    @keys.each_with_index do |key, index|
      map[key] = match[index]
    end
    map
  end
end

#pathObject



48
49
50
# File 'lib/dao/route.rb', line 48

def path
  self
end

#path_for(params) ⇒ Object



52
53
54
# File 'lib/dao/route.rb', line 52

def path_for(params)
  Route.path_for(self, params)
end