Class: Addressable::Template::MatchData
- Inherits:
-
Object
- Object
- Addressable::Template::MatchData
- Defined in:
- lib/addressable/template.rb
Overview
This class represents the data that is extracted when a Template is matched against a URI.
Instance Attribute Summary collapse
-
#mapping ⇒ Hash
readonly
The mapping that resulted from the match.
-
#template ⇒ Addressable::Template
readonly
The Template used for the match.
-
#uri ⇒ Addressable::URI
readonly
The URI that the Template was matched against.
Instance Method Summary collapse
-
#[](key, len = nil) ⇒ Array, ...
Accesses captured values by name or by index.
-
#initialize(uri, template, mapping) ⇒ MatchData
constructor
Creates a new MatchData object.
-
#inspect ⇒ String
Returns a
String
representation of the MatchData’s state. -
#pre_match ⇒ String
(also: #post_match)
Dummy method for code expecting a ::MatchData instance.
-
#to_a ⇒ Array
Array with the matched URI as first element followed by the captured values.
-
#to_s ⇒ String
(also: #string)
The matched URI as String.
-
#values ⇒ Array
(also: #captures)
The list of values that were captured by the Template.
-
#values_at(*indexes) ⇒ Array
Returns multiple captured values at once.
-
#variables ⇒ Array
(also: #keys, #names)
The list of variables that were present in the Template.
Constructor Details
#initialize(uri, template, mapping) ⇒ MatchData
Creates a new MatchData object. MatchData objects should never be instantiated directly.
103 104 105 106 107 |
# File 'lib/addressable/template.rb', line 103 def initialize(uri, template, mapping) @uri = uri.dup.freeze @template = template @mapping = mapping.dup.freeze end |
Instance Attribute Details
#mapping ⇒ Hash (readonly)
Returns The mapping that resulted from the match. Note that this mapping does not include keys or values for variables that appear in the Template, but are not present in the URI.
125 126 127 |
# File 'lib/addressable/template.rb', line 125 def mapping @mapping end |
#template ⇒ Addressable::Template (readonly)
Returns The Template used for the match.
117 118 119 |
# File 'lib/addressable/template.rb', line 117 def template @template end |
#uri ⇒ Addressable::URI (readonly)
Returns The URI that the Template was matched against.
112 113 114 |
# File 'lib/addressable/template.rb', line 112 def uri @uri end |
Instance Method Details
#[](key, len = nil) ⇒ Array, ...
Accesses captured values by name or by index.
170 171 172 173 174 175 176 177 178 |
# File 'lib/addressable/template.rb', line 170 def [](key, len = nil) if len to_a[key, len] elsif String === key or Symbol === key mapping[key.to_s] else to_a[key] end end |
#inspect ⇒ String
Returns a String
representation of the MatchData’s state.
213 214 215 216 |
# File 'lib/addressable/template.rb', line 213 def inspect sprintf("#<%s:%#0x RESULT:%s>", self.class.to_s, self.object_id, self.mapping.inspect) end |
#pre_match ⇒ String Also known as: post_match
Dummy method for code expecting a ::MatchData instance
222 223 224 |
# File 'lib/addressable/template.rb', line 222 def pre_match "" end |
#to_a ⇒ Array
Returns Array with the matched URI as first element followed by the captured values.
184 185 186 |
# File 'lib/addressable/template.rb', line 184 def to_a [to_s, *values] end |
#to_s ⇒ String Also known as: string
Returns The matched URI as String.
191 192 193 |
# File 'lib/addressable/template.rb', line 191 def to_s uri.to_s end |
#values ⇒ Array Also known as: captures
Returns The list of values that were captured by the Template. Note that this list will include nils for any variables which were in the Template, but did not appear in the URI.
143 144 145 146 147 148 |
# File 'lib/addressable/template.rb', line 143 def values @values ||= self.variables.inject([]) do |accu, key| accu << self.mapping[key] accu end end |
#values_at(*indexes) ⇒ Array
Returns multiple captured values at once.
205 206 207 |
# File 'lib/addressable/template.rb', line 205 def values_at(*indexes) indexes.map { |i| self[i] } end |
#variables ⇒ Array Also known as: keys, names
Returns The list of variables that were present in the Template. Note that this list will include variables which do not appear in the mapping because they were not present in URI.
132 133 134 |
# File 'lib/addressable/template.rb', line 132 def variables self.template.variables end |