Class: Usher::Route::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/usher/route/path.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(route, parts) ⇒ Path

Returns a new instance of Path.



8
9
10
11
# File 'lib/usher/route/path.rb', line 8

def initialize(route, parts)
  self.route = route
  self.parts = parts
end

Instance Attribute Details

#partsObject

Returns the value of attribute parts.



6
7
8
# File 'lib/usher/route/path.rb', line 6

def parts
  @parts
end

#routeObject

Returns the value of attribute route.



5
6
7
# File 'lib/usher/route/path.rb', line 5

def route
  @route
end

Instance Method Details

#can_generate_from?(keys) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/usher/route/path.rb', line 45

def can_generate_from?(keys)
  (dynamic_required_keys - keys).size.zero?
end

#dynamic?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/usher/route/path.rb', line 41

def dynamic?
  @dynamic
end

#dynamic_indiciesObject



13
14
15
16
17
18
19
# File 'lib/usher/route/path.rb', line 13

def dynamic_indicies
  unless dynamic? && @dynamic_indicies
    @dynamic_indicies = []
    parts.each_index{|i| @dynamic_indicies << i if parts[i].is_a?(Variable)}
  end
  @dynamic_indicies
end

#dynamic_keysObject



33
34
35
# File 'lib/usher/route/path.rb', line 33

def dynamic_keys
  @dynamic_keys ||= dynamic_map.keys if dynamic?
end

#dynamic_mapObject



25
26
27
28
29
30
31
# File 'lib/usher/route/path.rb', line 25

def dynamic_map
  unless dynamic? && @dynamic_map
    @dynamic_map = {}
    dynamic_parts.each{|p| @dynamic_map[p.name] = p }
  end
  @dynamic_map
end

#dynamic_partsObject



21
22
23
# File 'lib/usher/route/path.rb', line 21

def dynamic_parts
  @dynamic_parts ||= parts.values_at(*dynamic_indicies) if dynamic?
end

#dynamic_required_keysObject



37
38
39
# File 'lib/usher/route/path.rb', line 37

def dynamic_required_keys
  @dynamic_required_keys ||= dynamic_parts.select{|dp| !dp.default_value}.map{|dp| dp.name} if dynamic?
end

#merge(other_path) ⇒ Object

Merges paths for use in generation



50
51
52
53
# File 'lib/usher/route/path.rb', line 50

def merge(other_path)
  new_parts = parts + other_path.parts
  Path.new(route, new_parts)
end