Class: Addressable::Template::MatchData

### Home Classes Methods

### In Files

- lib/addressable/template.rb

### Parent

Object

### Methods

- ::new
- #[]
- #captures
- #inspect
- #keys
- #names
- #post_match
- #pre_match
- #string
- #to_a
- #to_s
- #values
- #values_at
- #variables

### Class/Module Index

Quicksearch

- Addressable
- Addressable::IDNA
- Addressable::IDNA::PunycodeBadInput
- Addressable::IDNA::PunycodeBigOutput
- Addressable::IDNA::PunycodeOverflow
- Addressable::Template
- Addressable::Template::InvalidTemplateOperatorError
- Addressable::Template::InvalidTemplateValueError
- Addressable::Template::MatchData
- Addressable::Template::TemplateOperatorAbortedError
- Addressable::VERSION

No matching classes.

# Addressable::Template::MatchData

This class represents the data that is extracted when a Template is matched against a URI.

### Attributes

mapping[R]

@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.

template[R]

@return [Addressable::Template]

    The Template used for the match.

uri[R]

@return [Addressable::URI]

    The URI that the Template was matched against.

### Public Class Methods

new(uri, template, mapping) click to toggle source

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 101
    def initialize(uri, template, mapping)
      @uri = uri.dup.freeze
      @template = template
      @mapping = mapping.dup.freeze
    end

### Public Instance Methods

[](key, len = nil) click to toggle source

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 returend 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 168
    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

captures() click to toggle source
Alias for: values
inspect() click to toggle source

Returns a `String` representation of the MatchData's state.

@return [String] The MatchData's state, as a `String`.

    # File lib/addressable/template.rb, line 211
    def inspect
      sprintf("#<%s:%#0x RESULT:%s>",
        self.class.to_s, self.object_id, self.mapping.inspect)
    end

keys() click to toggle source
Alias for: variables
names() click to toggle source
Alias for: variables
post_match() click to toggle source
Alias for: pre_match
pre_match() click to toggle source

Dummy method for code expecting a ::MatchData instance

@return [String] An empty string.

    # File lib/addressable/template.rb, line 220
    def pre_match
      ""
    end

Also aliased as: post_match
string() click to toggle source
Alias for: to_s
to_a() click to toggle source

@return [Array]

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

    # File lib/addressable/template.rb, line 182
    def to_a
      [to_s, *values]
    end

to_s() click to toggle source

@return [String]

    The matched URI as String.

    # File lib/addressable/template.rb, line 189
    def to_s
      uri.to_s
    end

Also aliased as: string
values() click to toggle source

@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 141
    def values
      @values ||= self.variables.inject([]) do |accu, key|
        accu << self.mapping[key]
        accu
      end
    end

Also aliased as: captures
values_at(*indexes) click to toggle source

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 203
    def values_at(*indexes)
      indexes.map { |i| self[i] }
    end

variables() click to toggle source

@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 130
    def variables
      self.template.variables
    end

Also aliased as: keys, names

[Validate]

Generated with the Darkfish Rdoc Generator 2.