Class: HttpRouter::Path
- Inherits:
-
Object
- Object
- HttpRouter::Path
- Defined in:
- lib/http_router/path.rb
Instance Attribute Summary collapse
-
#extension ⇒ Object
readonly
Returns the value of attribute extension.
-
#parts ⇒ Object
readonly
Returns the value of attribute parts.
-
#route ⇒ Object
Returns the value of attribute route.
Instance Method Summary collapse
- #===(other_path) ⇒ Object
- #compare_parts(p1, p2) ⇒ Object
- #generate_querystring(uri, params) ⇒ Object
-
#initialize(path, parts, extension) ⇒ Path
constructor
A new instance of Path.
- #matches_extension?(extension) ⇒ Boolean
- #url(args, options) ⇒ Object
- #variable_names ⇒ Object
- #variables ⇒ Object
Constructor Details
#initialize(path, parts, extension) ⇒ Path
Returns a new instance of Path.
6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/http_router/path.rb', line 6 def initialize(path, parts, extension) @path, @parts, @extension = path, parts, extension if duplicate_variable_names = variable_names.dup.uniq! raise AmbiguousVariableException.new("You have duplicate variable name present: #{duplicate_variable_names.join(', ')}") end eval_path = path.gsub(/[:\*]([a-zA-Z0-9_]+)/) {"\#{args.shift || (options && options.delete(:#{$1})) || raise(MissingParameterException.new(\"missing parameter #{$1}\"))}" } instance_eval " def raw_url(args,options) \"#{eval_path}\" end " end |
Instance Attribute Details
#extension ⇒ Object (readonly)
Returns the value of attribute extension.
4 5 6 |
# File 'lib/http_router/path.rb', line 4 def extension @extension end |
#parts ⇒ Object (readonly)
Returns the value of attribute parts.
4 5 6 |
# File 'lib/http_router/path.rb', line 4 def parts @parts end |
#route ⇒ Object
Returns the value of attribute route.
5 6 7 |
# File 'lib/http_router/path.rb', line 5 def route @route end |
Instance Method Details
#===(other_path) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/http_router/path.rb', line 20 def ===(other_path) return false if @parts.size != other_path.parts.size @parts.each_with_index {|p,i| return unless compare_parts(p, other_path.parts[i]) } compare_parts(@extension, other_path.extension) end |
#compare_parts(p1, p2) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/http_router/path.rb', line 28 def compare_parts(p1, p2) case p1 when Glob then p2.is_a?(Glob) when Variable then p2.is_a?(Variable) else p1 == p2 end end |
#generate_querystring(uri, params) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/http_router/path.rb', line 45 def generate_querystring(uri, params) if params && !params.empty? uri_size = uri.size params.each do |k,v| case v when Array v.each { |v_part| uri << '&' << CGI.escape(k.to_s) << '%5B%5D=' << CGI.escape(v_part.to_s) } else uri << '&' << CGI.escape(k.to_s) << '=' << CGI.escape(v.to_s) end end uri[uri_size] = ?? end end |
#matches_extension?(extension) ⇒ Boolean
72 73 74 |
# File 'lib/http_router/path.rb', line 72 def matches_extension?(extension) @extension.nil? || @extension === (extension) end |
#url(args, options) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/http_router/path.rb', line 37 def url(args, ) path = raw_url(args, ) raise TooManyParametersException.new unless args.empty? Rack::Utils.uri_escape!(path) generate_querystring(path, ) path end |
#variable_names ⇒ Object
68 69 70 |
# File 'lib/http_router/path.rb', line 68 def variable_names @variable_names ||= variables.map{|v| v.name} end |