Class: Octarine::Path
- Inherits:
-
Object
- Object
- Octarine::Path
- Extended by:
- Forwardable
- Defined in:
- lib/octarine/path.rb
Overview
Octarine::Path represents the path and query string portion of a url.
You are unlikely to need to create a Path instance yourself, insted you will usually obtain one from Request#path.
If you set a handler like so:
get "/users/:user_id/messages/:message_id" {|request| ... }
and a request is made like:
GET /users/1234/messages/4567?history=true
then ‘request.path` will behave as below:
path.user_id #=> "1234"
path[:user_id] #=> "1234"
path. #=> "5678"
path[:message_id] #=> "5678"
path["history"] #=> "true"
path.to_s #=> "/users/1234/messages/4567?history=true"
path.path #=> "/users/1234/messages/4567"
path.to_hash #=> {:user_id=>"1234", :message_id=>"5678", "history"=>"true"}
The following methods are available and behave as if the path was a hash: assoc, [], each, each_key, each_pair, each_value, empty?, fetch, has_key?, has_value?, key, key?, keys, rassoc, to_a, to_hash, value?, values, values_at and all Enumerable methods
The following methods are available and behave as if path was a string:
~, bytesize, length, size
Instance Method Summary collapse
-
#+(part) ⇒ Object
:call-seq: path + string -> new_path.
-
#==(other) ⇒ Object
:call-seq: path == other -> bool.
-
#===(other) ⇒ Object
:call-seq: path === other -> bool.
-
#initialize(template, path) ⇒ Path
constructor
:call-seq: Path.new(template, path_string) -> path.
-
#initialize_copy(source) ⇒ Object
:nodoc:.
-
#merge(other) ⇒ Object
:call-seq: path.merge(hash) -> new_path.
-
#to_s ⇒ Object
(also: #to_str)
:call-seq: path.to_s -> string.
-
#without(part) ⇒ Object
:call-seq: path.without(part) -> new_path.
Constructor Details
#initialize(template, path) ⇒ Path
:call-seq: Path.new(template, path_string) -> path
Create a new Path instance from a path template and a string of the path.
50 51 52 53 54 55 56 57 58 |
# File 'lib/octarine/path.rb', line 50 def initialize(template, path) @template = Octarine::PathTemplate.new(template) @params = @template.recognize(path) @params.each do |key, value| next unless Symbol === key self.class.class_eval {define_method(key) {@params[key]}} end end |
Instance Method Details
#+(part) ⇒ Object
97 98 99 |
# File 'lib/octarine/path.rb', line 97 def +(part) dup.tap {|cp| cp.template = @template + part} end |
#==(other) ⇒ Object
:call-seq: path == other -> bool
Returns true if other is equal to path, false otherwise.
114 115 116 |
# File 'lib/octarine/path.rb', line 114 def ==(other) self.class === other && to_s == other.to_s end |
#===(other) ⇒ Object
:call-seq: path === other -> bool
Returns true if other as a string is equal to path as a string, false otherwise.
123 124 125 |
# File 'lib/octarine/path.rb', line 123 def ===(other) to_s === other.to_s end |
#initialize_copy(source) ⇒ Object
:nodoc:
127 128 129 130 131 |
# File 'lib/octarine/path.rb', line 127 def initialize_copy(source) # :nodoc: super @template = @template.dup @params = @params.dup end |
#merge(other) ⇒ Object
85 86 87 |
# File 'lib/octarine/path.rb', line 85 def merge(other) dup.tap {|cp| cp.params.merge!(other)} end |
#to_s ⇒ Object Also known as: to_str
:call-seq: path.to_s -> string
Returns the path as a string.
105 106 107 |
# File 'lib/octarine/path.rb', line 105 def to_s @template.apply(@params) end |
#without(part) ⇒ Object
70 71 72 73 74 75 |
# File 'lib/octarine/path.rb', line 70 def without(part) dup.tap do |cp| cp.template = @template.without(part) cp.params.delete(part) end end |