class HttpRouter::Generator

Constants

SCHEME_PORTS

Public Class Methods

new(route, paths) click to toggle source
# 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

Public Instance Methods

each_path() { |p| ... } click to toggle source
# 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_count() click to toggle source
# 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_names() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/http_router/generator.rb, line 77
def url(*args)
  "#{scheme_port.first}#{url_ns(*args)}"
end
url_ns(*args) click to toggle source
# File lib/http_router/generator.rb, line 81
def url_ns(*args)
  "://#{@route.host || @router.default_host}#{scheme_port.last}#{path(*args)}"
end

Private Instance Methods

append_querystring(uri, params) click to toggle source
# File lib/http_router/generator.rb, line 141
def append_querystring(uri, params)
  if params && !params.empty?
    uri_size = uri.size
    params.each{ |k,v|  append_querystring_value(uri, k, v) }
    uri[uri_size] = ??
  end
  uri
end
append_querystring_value(uri, key, value) click to toggle source
# File lib/http_router/generator.rb, line 133
def append_querystring_value(uri, key, value)
  case value
  when Array then value.each{ |v|    append_querystring_value(uri, "#{key}[]",     v) }
  when Hash  then value.each{ |k, v| append_querystring_value(uri, "#{key}[#{k}]", v) }
  else            uri << "&#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}"
  end
end
matching_path(params, other_hash = nil) click to toggle source
# File lib/http_router/generator.rb, line 119
def matching_path(params, other_hash = nil)
  return @path_generators.first if @path_generators.size == 1
  case params
  when Array, nil
    @path_generators.find do |path|
      significant_key_count = params ? params.size : 0
      significant_key_count += (path.param_names & other_hash.keys).size if other_hash
      significant_key_count >= path.param_names.size
    end
  when Hash
    @path_generators.find { |path| (params && !params.empty? && (path.param_names & params.keys).size == path.param_names.size) || path.param_names.empty? }
  end
end
path_args_processing(args) { |args, options| ... } click to toggle source
# File lib/http_router/generator.rb, line 110
def path_args_processing(args)
  options = args.last.is_a?(Hash) ? args.pop : nil
  options = options.nil? ? @route.default_values.dup : @route.default_values.merge(options) if @route.default_values
  options.delete_if{ |k,v| v.nil? } if options
  result, params = yield args, options
  mount_point = @router.url_mount && (options ? @router.url_mount.url(options) : @router.url_mount.url)
  mount_point ? [File.join(mount_point, result), params] : [result, params]
end
path_with_params(*a) click to toggle source
# File lib/http_router/generator.rb, line 100
def path_with_params(*a)
  path_args_processing(a) do |args, options|
    path = args.empty? ? matching_path(options) : matching_path(args, options)
    path &&= path.generate(args, options)
    raise TooManyParametersException unless args.empty?
    raise InvalidRouteException.new("Error generating #{@route.path_for_generation}") unless path
    path ? [path, options] : nil
  end
end
scheme_port() click to toggle source
# File lib/http_router/generator.rb, line 91
def scheme_port
  @scheme_port ||= begin
    scheme = @route.scheme || @router.default_scheme
    port = @router.default_port
    port_part = SCHEME_PORTS.key?(scheme) && SCHEME_PORTS[scheme] == port ? '' : ":#{port}"
    [scheme, port_part]
  end
end