Class: Arugula::MatchData

Inherits:
Object
  • Object
show all
Defined in:
lib/arugula/match_data.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(regexp, string) ⇒ MatchData

Returns a new instance of MatchData.



4
5
6
7
8
9
10
# File 'lib/arugula/match_data.rb', line 4

def initialize(regexp, string)
  # require "awesome_print"
  # ap regexp, raw: true
  @regexp = regexp
  @string = string.dup.freeze
  @captures = Hash[regexp.captures.map { |c| [c.name, nil] }]
end

Instance Attribute Details

#end_indexObject

Returns the value of attribute end_index.



23
24
25
# File 'lib/arugula/match_data.rb', line 23

def end_index
  @end_index
end

#regexpObject (readonly)

Returns the value of attribute regexp.



3
4
5
# File 'lib/arugula/match_data.rb', line 3

def regexp
  @regexp
end

#start_indexObject

Returns the value of attribute start_index.



22
23
24
# File 'lib/arugula/match_data.rb', line 22

def start_index
  @start_index
end

#stringObject (readonly)

Returns the value of attribute string.



3
4
5
# File 'lib/arugula/match_data.rb', line 3

def string
  @string
end

Instance Method Details

#==(other) ⇒ Object



68
69
70
71
72
73
# File 'lib/arugula/match_data.rb', line 68

def ==(other)
  return false unless other.is_a?(MatchData) || other.is_a?(::MatchData)
  string == other.string &&
    regexp == other.regexp &&
    captures == other.captures
end

#add_capture(name, start_index, end_index) ⇒ Object



12
13
14
# File 'lib/arugula/match_data.rb', line 12

def add_capture(name, start_index, end_index)
  @captures[name] = start_index...end_index
end

#capturesObject



45
46
47
# File 'lib/arugula/match_data.rb', line 45

def captures
  @captures.map { |_name, range| range && @string[range] }
end

#freezeObject



59
60
61
62
# File 'lib/arugula/match_data.rb', line 59

def freeze
  @captures.freeze
  super
end

#hashObject



64
65
66
# File 'lib/arugula/match_data.rb', line 64

def hash
  @string.hash ^ @regexp.hash ^ @captures.hash
end

#inspectObject



29
30
31
32
33
34
# File 'lib/arugula/match_data.rb', line 29

def inspect
  captures_part = @captures.map do |name, range|
    " #{name}:#{dump_str(range && @string[range])}"
  end.join
  "#<MatchData #{dump_str(to_s)}#{captures_part}>"
end

#post_matchObject



54
55
56
57
# File 'lib/arugula/match_data.rb', line 54

def post_match
  return '' if end_index == string.size
  @string[end_index..-1]
end

#pre_matchObject



49
50
51
52
# File 'lib/arugula/match_data.rb', line 49

def pre_match
  return '' if start_index == 0
  @string[0...start_index]
end

#reset_captures!Object



16
17
18
19
20
# File 'lib/arugula/match_data.rb', line 16

def reset_captures!
  @captures.keys.each do |key|
    @captures[key] = nil
  end
end

#sizeObject Also known as: length



40
41
42
# File 'lib/arugula/match_data.rb', line 40

def size
  @captures.size + 1
end

#to_aObject



36
37
38
# File 'lib/arugula/match_data.rb', line 36

def to_a
  captures.unshift(to_s)
end

#to_sObject



25
26
27
# File 'lib/arugula/match_data.rb', line 25

def to_s
  @string[start_index...end_index]
end