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.


102
103
104
105
106
# File 'lib/addressable/template.rb', line 102

def initialize(uri, template, mapping)
  @uri = uri.dup.freeze
  @template = template
  @mapping = mapping.dup.freeze
end

Instance Attribute Details

#mappingHash (readonly)


124
125
126
# File 'lib/addressable/template.rb', line 124

def mapping
  @mapping
end

#templateAddressable::Template (readonly)


116
117
118
# File 'lib/addressable/template.rb', line 116

def template
  @template
end

#uriAddressable::URI (readonly)


111
112
113
# File 'lib/addressable/template.rb', line 111

def uri
  @uri
end

Instance Method Details

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

Accesses captured values by name or by index.


169
170
171
172
173
174
175
176
177
# File 'lib/addressable/template.rb', line 169

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

#inspectString

Returns a String representation of the MatchData's state.


212
213
214
215
# File 'lib/addressable/template.rb', line 212

def inspect
  sprintf("#<%s:%#0x RESULT:%s>",
    self.class.to_s, self.object_id, self.mapping.inspect)
end

#pre_matchString Also known as: post_match

Dummy method for code expecting a ::MatchData instance


221
222
223
# File 'lib/addressable/template.rb', line 221

def pre_match
  ""
end

#to_aArray


183
184
185
# File 'lib/addressable/template.rb', line 183

def to_a
  [to_s, *values]
end

#to_sString Also known as: string


190
191
192
# File 'lib/addressable/template.rb', line 190

def to_s
  uri.to_s
end

#valuesArray Also known as: captures


142
143
144
145
146
147
# File 'lib/addressable/template.rb', line 142

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.

See Also:


204
205
206
# File 'lib/addressable/template.rb', line 204

def values_at(*indexes)
  indexes.map { |i| self[i] }
end

#variablesArray Also known as: keys, names


131
132
133
# File 'lib/addressable/template.rb', line 131

def variables
  self.template.variables
end