Class: Mustermann::Match

Inherits:
Object
  • Object
show all
Defined in:
lib/mustermann/match.rb

Overview

The return value of Pattern#match, Pattern#peek_match, Set#match, and similar methods. Mimics large parts of the MatchData API, but also provides access to the pattern and params hash.

Direct Known Subclasses

Set::Match

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern, string, **options) ⇒ Match #initialize(match, **options) ⇒ Match #initialize(pattern, match, **options) ⇒ Match

Returns a new instance of Match.

Overloads:

  • #initialize(pattern, string, **options) ⇒ Match

    Parameters:

    • pattern (Mustermann::Pattern)

      the pattern that produced the match

    • string (String)

      the string that was matched

  • #initialize(match, **options) ⇒ Match

    Parameters:

  • #initialize(pattern, match, **options) ⇒ Match

    Parameters:

Parameters:

  • options (Hash)

    a customizable set of options



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/mustermann/match.rb', line 48

def initialize(pattern_or_match, string_or_match = nil, matched: nil, params: nil, post_match: nil, pre_match: nil, captures: nil, named_captures: nil, regexp: nil)
  case pattern_or_match
  when Mustermann::Match, MatchData then match = pattern_or_match
  when Mustermann::Pattern          then pattern = pattern_or_match
  else raise ArgumentError, "first argument must be a Mustermann::Pattern or a MatchData, not #{pattern_or_match.class}"
  end

  case string_or_match
  when Mustermann::Match, MatchData then match ||= string_or_match
  when String                       then string = string_or_match
  when nil # ignore
  else raise ArgumentError, "second argument must be a String or a MatchData, not #{string_or_match.class}"
  end

  @pattern        = pattern        || match&.pattern
  @string         = string         || match&.string         || ''
  @params         = params         || match&.params         || {}
  @post_match     = post_match     || match&.post_match     || ''
  @pre_match      = pre_match      || match&.pre_match      || ''
  @captures       = captures       || match&.captures       || @params.values
  @named_captures = named_captures || match&.named_captures || @params
  @matched        = matched        || match&.to_s           || @string

  unless @regexp = regexp
    @regexp = match.regexp if match.respond_to?(:regexp)
    @regexp ||= pattern.respond_to?(:regexp) ? pattern.regexp : nil
  end
end

Instance Attribute Details

#capturesArray (readonly)

Returns the captures array.

Returns:

  • (Array)

    the captures array



17
18
19
# File 'lib/mustermann/match.rb', line 17

def captures
  @captures
end

#named_capturesHash (readonly)

Returns the named captures hash, usually identical to #params.

Returns:

  • (Hash)

    the named captures hash, usually identical to #params



20
21
22
# File 'lib/mustermann/match.rb', line 20

def named_captures
  @named_captures
end

#paramsHash (readonly) Also known as: to_h

Returns the params hash.

Returns:

  • (Hash)

    the params hash



14
15
16
# File 'lib/mustermann/match.rb', line 14

def params
  @params
end

#patternMustermann::Pattern (readonly)

Returns the pattern that produced the match.

Returns:



8
9
10
# File 'lib/mustermann/match.rb', line 8

def pattern
  @pattern
end

#post_matchString (readonly)

Returns the post match string.

Returns:

  • (String)

    the post match string



23
24
25
# File 'lib/mustermann/match.rb', line 23

def post_match
  @post_match
end

#pre_matchString (readonly)

Returns the pre match string.

Returns:

  • (String)

    the pre match string



26
27
28
# File 'lib/mustermann/match.rb', line 26

def pre_match
  @pre_match
end

#regexpRegexp? (readonly)

Returns the regular expression that produced the match, if available.

Returns:

  • (Regexp, nil)

    the regular expression that produced the match, if available



29
30
31
# File 'lib/mustermann/match.rb', line 29

def regexp
  @regexp
end

#stringString (readonly)

Returns the string that was matched.

Returns:

  • (String)

    the string that was matched



11
12
13
# File 'lib/mustermann/match.rb', line 11

def string
  @string
end

Instance Method Details

#[](key) ⇒ Object #[](index) ⇒ Object #[](start, length) ⇒ Array #[](range) ⇒ Array

Overloads:

  • #[](key) ⇒ Object

    Access params by key.

    Parameters:

    • key (String, Symbol)

      the key to access

    Returns:

    • the value of the param, or nil if not found

  • #[](index) ⇒ Object

    Access captures by index.

    Parameters:

    • index (Integer)

      the index to access

    Returns:

    • the value of the capture, or nil if not found

  • #[](start, length) ⇒ Array

    Access multiple captures by index and length.

    Parameters:

    • start (Integer)

      the starting index to access

    • length (Integer)

      the number of captures to access

    Returns:

    • (Array)

      the values of the captures

  • #[](range) ⇒ Array

    Access multiple captures by range.

    Parameters:

    • range (Range)

      the range of indices to access

    Returns:

    • (Array)

      the values of the captures



97
98
99
100
101
102
103
104
105
# File 'lib/mustermann/match.rb', line 97

def [](key, length = nil)
  case key
  when String  then params[key]
  when Symbol  then params[key.to_s]
  when Integer then length ? captures[key, length] : captures[key]
  when Range   then captures[key]
  else raise ArgumentError, "key must be a String, Symbol, Integer, or Range, not #{key.class}"
  end
end

#deconstruct_keys(keys) ⇒ Hash

Deconstructs the match into a hash of the given keys. Useful for pattern matching.

Parameters:

  • keys (Array)

    the keys to deconstruct

Returns:

  • (Hash)

    a hash of the given keys and their corresponding values

See Also:



111
# File 'lib/mustermann/match.rb', line 111

def deconstruct_keys(keys) = keys.to_h { |key| [key, self[key]] }

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)

See Also:

  • Object#eql?


117
118
119
120
# File 'lib/mustermann/match.rb', line 117

def eql?(other)
  return false unless other.is_a? self.class
  pattern == other.pattern && string == other.string && params == other.params
end

#hashObject

See Also:

  • Object#hash


114
# File 'lib/mustermann/match.rb', line 114

def hash = pattern.hash ^ string.hash ^ params.hash

#to_sString

Returns the matched substring (like MatchData#to_s).

Returns:

  • (String)

    the matched substring (like MatchData#to_s)



128
# File 'lib/mustermann/match.rb', line 128

def to_s = @matched

#values_at(*keys) ⇒ Array

Returns the values of the given keys as an array.

Returns:

  • (Array)

    the values of the given keys



125
# File 'lib/mustermann/match.rb', line 125

def values_at(*keys) = keys.map { |key| self[key] }