Class: HttpRouter::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/http_router/generator.rb

Defined Under Namespace

Classes: PathGenerator

Constant Summary collapse

SCHEME_PORTS =
{'http' => 80, 'https' => 443}

Instance Method Summary collapse

Constructor Details

#initialize(route, paths) ⇒ Generator

Returns a new instance of Generator.



53
54
55
56
57
58
59
60
# File 'lib/http_router/generator.rb', line 53

def initialize(route, paths)
  @route, @paths = route, paths
  @router = @route.router
  @route.generator = self
  @path_generators = @paths.map do |p|
    generator = PathGenerator.new(route, p.is_a?(String) ? p : route.path_for_generation, p.is_a?(Regexp) ? p : nil)
  end
end

Instance Method Details

#each_pathObject



70
71
72
73
74
75
# File 'lib/http_router/generator.rb', line 70

def each_path
  @path_generators.each {|p| yield p }
  @path_generators.sort! do |p1, p2|
    p2.param_names.size <=> p1.param_names.size
  end
end

#max_param_countObject



66
67
68
# File 'lib/http_router/generator.rb', line 66

def max_param_count
  @max_param_count ||= @path_generators.map{|p| p.param_names.size}.max
end

#param_namesObject



62
63
64
# File 'lib/http_router/generator.rb', line 62

def param_names
  @param_names ||= @path_generators.map{|path| path.param_names}.flatten.uniq
end

#path(*args) ⇒ Object



85
86
87
88
# File 'lib/http_router/generator.rb', line 85

def path(*args)
  result, extra_params = path_with_params(*args)
  append_querystring(result, extra_params)
end

#url(*args) ⇒ Object



77
78
79
# File 'lib/http_router/generator.rb', line 77

def url(*args)
  "#{scheme_port.first}#{url_ns(*args)}"
end

#url_ns(*args) ⇒ Object



81
82
83
# File 'lib/http_router/generator.rb', line 81

def url_ns(*args)
  "://#{@route.host || @router.default_host}#{scheme_port.last}#{path(*args)}"
end