Class: Mustermann::SimpleMatch

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

Overview

Fakes MatchData for patterns that do not support capturing.

See Also:

Instance Method Summary collapse

Constructor Details

#initialize(string = "", names: [], captures: []) ⇒ SimpleMatch

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of SimpleMatch.



7
8
9
10
11
# File 'lib/mustermann/simple_match.rb', line 7

def initialize(string = "", names: [], captures: [])
  @string   = string.dup
  @names    = names
  @captures = captures
end

Instance Method Details

#[](*args) ⇒ nil

Returns imitates MatchData interface.

Returns:

  • (nil)

    imitates MatchData interface



29
30
31
32
33
34
35
# File 'lib/mustermann/simple_match.rb', line 29

def [](*args)
  args.map! do |arg|
    next arg unless arg.is_a? Symbol or arg.is_a? String
    names.index(arg.to_s)
  end
  @captures[*args]
end

#capturesArray<String>

Returns empty array for imitating MatchData interface.

Returns:

  • (Array<String>)

    empty array for imitating MatchData interface



24
25
26
# File 'lib/mustermann/simple_match.rb', line 24

def captures
  @captures.dup
end

#inspectString

Returns string representation.

Returns:

  • (String)

    string representation



45
46
47
# File 'lib/mustermann/simple_match.rb', line 45

def inspect
  "#<%p %p>" % [self.class, @string]
end

#namesArray<String>

Returns empty array for imitating MatchData interface.

Returns:

  • (Array<String>)

    empty array for imitating MatchData interface



19
20
21
# File 'lib/mustermann/simple_match.rb', line 19

def names
  @names.dup
end

#to_sString

Returns the string that was matched against.

Returns:

  • (String)

    the string that was matched against



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

def to_s
  @string.dup
end