Class: HttpRouter::Node::Path

Inherits:
HttpRouter::Node show all
Defined in:
lib/http_router/node/path.rb

Instance Attribute Summary collapse

Attributes inherited from HttpRouter::Node

#router

Instance Method Summary collapse

Methods inherited from HttpRouter::Node

#add_destination, #add_free_match, #add_glob, #add_glob_regexp, #add_host, #add_lookup, #add_match, #add_request_method, #add_scheme, #add_spanning_match, #add_user_agent, #add_variable, #depth, #inspect, #inspect_matchers_body

Constructor Details

#initialize(router, parent, route, path, param_names = []) ⇒ Path

Returns a new instance of Path.



6
7
8
9
10
11
12
13
# File 'lib/http_router/node/path.rb', line 6

def initialize(router, parent, route, path, param_names = [])
  @route, @path, @param_names, @dynamic = route, path, param_names, !param_names.empty?
  @route.add_path(self)

  raise AmbiguousVariableException, "You have duplicate variable names present: #{duplicates.join(', ')}" if param_names.uniq.size != param_names.size
  super router, parent
  router.uncompile
end

Instance Attribute Details

#dynamicObject (readonly) Also known as: dynamic?

Returns the value of attribute dynamic.



4
5
6
# File 'lib/http_router/node/path.rb', line 4

def dynamic
  @dynamic
end

#param_namesObject (readonly)

Returns the value of attribute param_names.



4
5
6
# File 'lib/http_router/node/path.rb', line 4

def param_names
  @param_names
end

#pathObject (readonly)

Returns the value of attribute path.



4
5
6
# File 'lib/http_router/node/path.rb', line 4

def path
  @path
end

#routeObject (readonly)

Returns the value of attribute route.



4
5
6
# File 'lib/http_router/node/path.rb', line 4

def route
  @route
end

Instance Method Details

#duplicatesObject



51
52
53
# File 'lib/http_router/node/path.rb', line 51

def duplicates
  param_names.group_by { |e| e }.select { |k, v| v.size > 1 }.map(&:first)
end

#hashify_params(params) ⇒ Object



15
16
17
# File 'lib/http_router/node/path.rb', line 15

def hashify_params(params)
  @dynamic && params ? Hash[param_names.zip(params)] : {}
end

#inspect_labelObject



47
48
49
# File 'lib/http_router/node/path.rb', line 47

def inspect_label
  "Path: #{path.inspect} for route #{route.name || 'unnamed route'} to #{route.dest.inspect}"
end

#to_codeObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/http_router/node/path.rb', line 19

def to_code
  path_ivar = inject_root_ivar(self)
  "#{"if !callback && request.path.size == 1 && request.path.first == '' && (request.rack_request.head? || request.rack_request.get?) && request.rack_request.path_info[-1] == ?/
    response = ::Rack::Response.new
    response.redirect(request.rack_request.path_info[0, request.rack_request.path_info.size - 1], 302)
    return response.finish
  end" if router.redirect_trailing_slash?}

  #{"if request.#{router.ignore_trailing_slash? ? 'path_finished?' : 'path.empty?'}" unless route.match_partially}
    if callback
      request.called = true
      callback.call(Response.new(request, #{path_ivar}))
    else
      env = request.rack_request.dup.env
      env['router.request'] = request
      env['router.params'] ||= {}
      #{"env['router.params'].merge!(Hash[#{param_names.inspect}.zip(request.params)])" if dynamic?}
      @router.rewrite#{"_partial" if route.match_partially}_path_info(env, request)
      response = @router.process_destination_path(#{path_ivar}, env)
      return response unless router.pass_on_response(response)
    end
  #{"end" unless route.match_partially}"
end

#usable?(other) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/http_router/node/path.rb', line 43

def usable?(other)
  other == self
end