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?(keys) ⇒ Boolean

Returns:

  • (Boolean)


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

def can_generate_from_keys?(keys)
  if dynamic?
    (dynamic_required_keys - keys).size.zero? ? keys : nil
  end
end

#can_generate_from_params?(params) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
# File 'lib/usher/route/path.rb', line 51

def can_generate_from_params?(params)
  if route.router.consider_destination_keys?
    route.destination.to_a - params.to_a
    (route.destination.to_a - params.to_a).size.zero?
  end
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



59
60
61
62
# File 'lib/usher/route/path.rb', line 59

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