Class: Mustermann::Match

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

Direct Known Subclasses

Set::Match

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern, string, params = {}, post_match: '', pre_match: '') ⇒ Match

Returns a new instance of Match.



7
8
9
10
11
12
13
# File 'lib/mustermann/match.rb', line 7

def initialize(pattern, string, params = {}, post_match: '', pre_match: '')
  @pattern    = pattern
  @string     = string.freeze
  @params     = params.freeze
  @post_match = post_match.freeze
  @pre_match  = pre_match.freeze
end

Instance Attribute Details

#paramsObject (readonly) Also known as: to_h

Returns the value of attribute params.



5
6
7
# File 'lib/mustermann/match.rb', line 5

def params
  @params
end

#patternObject (readonly)

Returns the value of attribute pattern.



5
6
7
# File 'lib/mustermann/match.rb', line 5

def pattern
  @pattern
end

#post_matchObject (readonly)

Returns the value of attribute post_match.



5
6
7
# File 'lib/mustermann/match.rb', line 5

def post_match
  @post_match
end

#pre_matchObject (readonly)

Returns the value of attribute pre_match.



5
6
7
# File 'lib/mustermann/match.rb', line 5

def pre_match
  @pre_match
end

#stringObject (readonly) Also known as: to_s

Returns the value of attribute string.



5
6
7
# File 'lib/mustermann/match.rb', line 5

def string
  @string
end

Instance Method Details

#[](key) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/mustermann/match.rb', line 15

def [](key)
  case key
  when String  then params[key]
  when Symbol  then params[key.to_s]
  else raise ArgumentError, "key must be a String or Symbol, not #{key.class}"
  end
end

#deconstruct_keys(keys) ⇒ Object



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

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

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

Returns:

  • (Boolean)


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

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

#hashObject



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

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

#values_at(*keys) ⇒ Object



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

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