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
12
# File 'lib/usher/route/path.rb', line 8

def initialize(route, parts)
  self.route = route
  self.parts = parts
  build_generator
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

#==(other_path) ⇒ Object



14
15
16
# File 'lib/usher/route/path.rb', line 14

def ==(other_path)
  other_path.is_a?(Path) ? route == other_path.route && parts == other_path.parts : nil
end

#can_generate_from_keys?(keys) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
# File 'lib/usher/route/path.rb', line 54

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)


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

def can_generate_from_params?(params)
  if route.router.consider_destination_keys?
    (route.destination.to_a - params.to_a).size.zero?
  end
end

#convert_params_array(ary) ⇒ Object



18
19
20
# File 'lib/usher/route/path.rb', line 18

def convert_params_array(ary)
  ary.empty? ? ary : dynamic_keys.zip(ary)
end

#dynamic?Boolean

Returns:

  • (Boolean)


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

def dynamic?
  @dynamic
end

#dynamic_indiciesObject



22
23
24
25
26
27
28
# File 'lib/usher/route/path.rb', line 22

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



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

def dynamic_keys
  @dynamic_keys ||= dynamic_parts.map{|dp| dp.name} if dynamic?
end

#dynamic_mapObject



34
35
36
37
38
39
40
# File 'lib/usher/route/path.rb', line 34

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

#dynamic_partsObject



30
31
32
# File 'lib/usher/route/path.rb', line 30

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

#dynamic_required_keysObject



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

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

#generateObject



72
73
74
# File 'lib/usher/route/path.rb', line 72

def generate
  nil
end

#generate_from_hash(params = nil) ⇒ Object



76
77
78
# File 'lib/usher/route/path.rb', line 76

def generate_from_hash(params = nil)
  generate
end

#merge(other_path) ⇒ Object

Merges paths for use in generation



67
68
69
70
# File 'lib/usher/route/path.rb', line 67

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