Class: Addressable::Template::MatchData

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(uri, template, mapping) ⇒ MatchData

Creates a new MatchData object. MatchData objects should never be instantiated directly.

Parameters:



105
106
107
108
109
# File 'lib/addressable/template.rb', line 105

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.

Returns:

  • (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.



127
128
129
# File 'lib/addressable/template.rb', line 127

def mapping
  @mapping
end

#template ⇒ Addressable::Template (readonly)

Returns The Template used for the match.

Returns:



119
120
121
# File 'lib/addressable/template.rb', line 119

def template
  @template
end

#uri ⇒ Addressable::URI (readonly)

Returns The URI that the Template was matched against.

Returns:



114
115
116
# File 'lib/addressable/template.rb', line 114

def uri
  @uri
end

Instance Method Details

#[](key, len = nil) ⇒ Array, ...

Accesses captured values by name or by index.

Parameters:

  • key (String, Symbol, Fixnum) —

    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.

  • len (#to_int, nil) (defaults to: nil) —

    If provided, an array of values will be returned with the given parameter used as length.

Returns:

  • (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.



172
173
174
175
176
177
178
179
180
# File 'lib/addressable/template.rb', line 172

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.

Returns:

  • (String) —

    The MatchData's state, as a String.



215
216
217
218
# File 'lib/addressable/template.rb', line 215

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

Returns:

  • (String) —

    An empty string.



224
225
226
# File 'lib/addressable/template.rb', line 224

def pre_match
  ""
end

#to_a ⇒ Array

Returns Array with the matched URI as first element followed by the captured values.

Returns:

  • (Array) —

    Array with the matched URI as first element followed by the captured values.



186
187
188
# File 'lib/addressable/template.rb', line 186

def to_a
  [to_s, *values]
end

#to_s ⇒ String Also known as: string

Returns The matched URI as String.

Returns:

  • (String) —

    The matched URI as String.



193
194
195
# File 'lib/addressable/template.rb', line 193

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.

Returns:

  • (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.



145
146
147
148
149
150
# File 'lib/addressable/template.rb', line 145

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.

Parameters:

  • *indexes (String, Symbol, Fixnum) —

    Indices of the captures to be returned

Returns:

  • (Array) —

    Values corresponding to given indices.

See Also:



207
208
209
# File 'lib/addressable/template.rb', line 207

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.

Returns:

  • (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.



134
135
136
# File 'lib/addressable/template.rb', line 134

def variables
  self.template.variables
end