Class: Mustermann::Pattern Abstract

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

Overview

This class is abstract.

Superclass for all pattern implementations.

Direct Known Subclasses

Identity, RegexpBased, Shell

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string, **options) ⇒ Pattern

Returns a new instance of Pattern.

Raises:

See Also:



55
56
57
58
# File 'lib/mustermann/pattern.rb', line 55

def initialize(string, uri_decode: true, **options)
  @uri_decode = uri_decode
  @string     = string.dup
end

Class Method Details

.new(string, **options) ⇒ Pattern

Returns a new instance of Pattern.

Raises:

  • (ArgumentError)

    if some option is not supported

  • (Mustermann::Error)

    if the pattern can’t be generated from the string

See Also:



38
39
40
41
42
43
44
45
# File 'lib/mustermann/pattern.rb', line 38

def self.new(string, ignore_unknown_options: false, **options)
  unless ignore_unknown_options
    unsupported = options.keys.detect { |key| not supported?(key) }
    raise ArgumentError, "unsupported option %p for %p" % [unsupported, self] if unsupported
  end

  super(string, options)
end

.supported?(option) ⇒ Boolean



28
29
30
# File 'lib/mustermann/pattern.rb', line 28

def self.supported?(option)
  supported_options.include? option
end

.supported_optionsArray<Symbol> .supported_options(*list) ⇒ Array<Symbol>

List of supported options.

Overloads:

  • .supported_options(*list) ⇒ Array<Symbol>

    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.

    Adds options to the list.



19
20
21
22
23
24
# File 'lib/mustermann/pattern.rb', line 19

def self.supported_options(*list)
  @supported_options ||= []
  options = @supported_options.concat(list)
  options += superclass.supported_options if self < Pattern
  options
end

Instance Method Details

#===(string) ⇒ Boolean

Note:

Needs to be overridden by subclass.

Returns Whether or not the pattern matches the given string.

Raises:

  • (NotImplementedError)

See Also:



85
86
87
# File 'lib/mustermann/pattern.rb', line 85

def ===(string)
  raise NotImplementedError, 'subclass responsibility'
end

#=~(string) ⇒ Integer?

Returns nil if pattern does not match the string, zero if it does.

See Also:



77
78
79
# File 'lib/mustermann/pattern.rb', line 77

def =~(string)
  0 if self === string
end

#match(string) ⇒ MatchData, ...

Returns MatchData or similar object if the pattern matches.



70
71
72
# File 'lib/mustermann/pattern.rb', line 70

def match(string)
  SimpleMatch.new(string) if self === string
end

#named_capturesArray<String>

Returns capture names.



91
92
93
# File 'lib/mustermann/pattern.rb', line 91

def named_captures
  {}
end

#namesHash{String}: Array<Integer>

Returns capture names mapped to capture index.

See Also:



97
98
99
# File 'lib/mustermann/pattern.rb', line 97

def names
  []
end

#params(string = nil, captures: nil) ⇒ Hash{String: String, Array<String>}?



103
104
105
106
107
108
109
110
111
112
# File 'lib/mustermann/pattern.rb', line 103

def params(string = nil, captures: nil)
  return unless captures ||= match(string)
  params   = named_captures.map do |name, positions|
    values = positions.map { |pos| map_param(name, captures[pos]) }.flatten
    values = values.first if values.size < 2 and not always_array? name
    [name, values]
  end

  Hash[params]
end

#to_sString



61
62
63
# File 'lib/mustermann/pattern.rb', line 61

def to_s
  @string.dup
end