Class: Lab42::Match

Inherits:
Object
  • Object
show all
Defined in:
lib/lab42/match.rb,
lib/lab42/match/version.rb

Constant Summary collapse

AmbigousArgAndBlock =
Class.new RuntimeError
NotMatchedYet =
Class.new RuntimeError
UnsuccessfulMerge =
Class.new RuntimeError
Parts =
{
  first: 0,
  first_capture: 2,
  first_match: 1,
  last: -1,
  last_capture: -3,
  last_match: -2,
  post: -1,
  pre: 0,
  prefix: 0,
  suffix: -1
}
VERSION =
"0.1.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#rgxObject (readonly)

Returns the value of attribute rgx.



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

def rgx
  @rgx
end

#subjectObject (readonly)

Returns the value of attribute subject.



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

def subject
  @subject
end

Instance Method Details

#[](capt_index) ⇒ Object



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

def [](capt_index)
  match&.[](capt_index)
end

#captsObject



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

def capts
  _assure_matched?
  match && @par
  match&.captures
end

#groupsObject



33
34
35
36
# File 'lib/lab42/match.rb', line 33

def groups
  _assure_success
  @groups
end

#match(with = nil) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/lab42/match.rb', line 38

def match(with=nil)
  if with
    return _match(with)
  end
  _assure_matched?
  @match
end

#matched?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/lab42/match.rb', line 46

def matched?
  @matched
end

#partsObject



50
51
52
53
# File 'lib/lab42/match.rb', line 50

def parts
  _assure_success
  @parts
end

#replace(cpt_index, with = nil, &blk) ⇒ Object



55
56
57
58
59
# File 'lib/lab42/match.rb', line 55

def replace(cpt_index, with=nil, &blk)
  _assure_success
  _assure_unambigous_args(with, blk)
  _replace_part(2 * cpt_index, with, &blk)
end

#replace_part(part_index, with = nil, &blk) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/lab42/match.rb', line 61

def replace_part(part_index, with=nil, &blk)
  _assure_success
  _assure_unambigous_args(with, blk)
  case part_index
  when Symbol
    idx = Parts.fetch(part_index){ raise IllegalSymbolicPartIndex, "#{part_index} is not one of the predefined symbolic indices, which are #{Parts.keys.map(&:inspect).join(", ")}" }
    _replace_part(idx, with, &blk)
  else
    _replace_part(part_index, with, &blk)
  end
end

#stringObject



73
74
75
76
# File 'lib/lab42/match.rb', line 73

def string
  _assure_success
  @string
end

#success?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/lab42/match.rb', line 78

def success?
  !!@match
end