Class: Usher::Util::Generators::Generic

Inherits:
Object
  • Object
show all
Defined in:
lib/usher/util/generate.rb

Direct Known Subclasses

URL

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#usherObject

Returns the value of attribute usher.



7
8
9
# File 'lib/usher/util/generate.rb', line 7

def usher
  @usher
end

Instance Method Details

#generate(name, params) ⇒ Object



9
10
11
# File 'lib/usher/util/generate.rb', line 9

def generate(name, params)
  generate_path_for_base_params(@usher.named_routes[name].find_matching_path(params), params)
end

#generate_path_for_base_params(path, params) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/usher/util/generate.rb', line 13

def generate_path_for_base_params(path, params)
  raise UnrecognizedException.new unless path

  result = ''
  path.parts.each do |part|
    case part
    when Route::Variable::Glob
      value = (params && params.delete(part.name)) || part.default_value || raise(MissingParameterException.new)
      value.each_with_index do |current_value, index|
        part.valid!(current_value)
        result << current_value.to_s
        result << usher.delimiters.first if index != value.size - 1
      end
    when Route::Variable
      value = (params && params.delete(part.name)) || part.default_value || raise(MissingParameterException.new)
      part.valid!(value)
      result << value.to_s
    else
      result << part
    end
  end
  result
end