class Addressable::Template::MatchData
This class represents the data that is extracted when a Template
is matched against a URI
.
Attributes
@return [Hash]
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.
@return [Addressable::Template]
The Template used for the match.
@return [Addressable::URI]
The URI that the Template was matched against.
Public Class Methods
Creates a new MatchData
object. MatchData
objects should never be instantiated directly.
@param [Addressable::URI] uri
The URI that the template was matched against.
# File lib/addressable/template.rb, line 103 def initialize(uri, template, mapping) @uri = uri.dup.freeze @template = template @mapping = mapping.dup.freeze end
Public Instance Methods
Accesses captured values by name or by index.
@param [String, Symbol, Fixnum] key
Capture index or name. Note that when accessing by with index of 0, the full URI will be returned. The intention is to mimic the ::MatchData#[] behavior.
@param [#to_int, nil] len
If provided, an array of values will be returned with the given parameter used as length.
@return [Array, String, nil]
The captured value corresponding to the index or name. If the value was not provided or the key is unknown, nil will be returned. If the second parameter is provided, an array of that length will be returned instead.
# 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
Returns a String
representation of the MatchData’s state.
@return [String] The MatchData’s state, as a String
.
# 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
Dummy method for code expecting a ::MatchData instance
@return [String] An empty string.
# File lib/addressable/template.rb, line 222 def pre_match "" end
@return [Array]
Array with the matched URI as first element followed by the captured values.
# File lib/addressable/template.rb, line 184 def to_a [to_s, *values] end
@return [String]
The matched URI as String.
# File lib/addressable/template.rb, line 191 def to_s uri.to_s end
@return [Array]
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.
# File lib/addressable/template.rb, line 143 def values @values ||= self.variables.inject([]) do |accu, key| accu << self.mapping[key] accu end end
Returns multiple captured values at once.
@param [String, Symbol, Fixnum] *indexes
Indices of the captures to be returned
@return [Array]
Values corresponding to given indices.
@see Addressable::Template::MatchData#[]
# File lib/addressable/template.rb, line 205 def values_at(*indexes) indexes.map { |i| self[i] } end
@return [Array]
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.
# File lib/addressable/template.rb, line 132 def variables self.template.variables end