class Lab42::Match
Constants
- AmbigousArgAndBlock
- NotMatchedYet
- Parts
- UnsuccessfulMerge
- VERSION
Attributes
rgx[R]
subject[R]
Public Class Methods
new(rgx, subject=nil)
click to toggle source
# File lib/lab42/match.rb, line 175 def initialize(rgx, subject=nil) @rgx = rgx @subject = subject _init_defaults match subject if subject end
Public Instance Methods
[](capt_index)
click to toggle source
# File lib/lab42/match.rb, line 23 def [](capt_index) match&.[](capt_index) end
capts()
click to toggle source
# File lib/lab42/match.rb, line 27 def capts _assure_matched? match && @par match&.captures end
groups()
click to toggle source
# File lib/lab42/match.rb, line 33 def groups _assure_success @groups end
match(with=nil)
click to toggle source
# File lib/lab42/match.rb, line 38 def match(with=nil) if with return _match(with) end _assure_matched? @match end
matched?()
click to toggle source
# File lib/lab42/match.rb, line 46 def matched? @matched end
parts()
click to toggle source
# File lib/lab42/match.rb, line 50 def parts _assure_success @parts end
replace(cpt_index, with=nil, &blk)
click to toggle source
# 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)
click to toggle source
# 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
string()
click to toggle source
# File lib/lab42/match.rb, line 73 def string _assure_success @string end
success?()
click to toggle source
# File lib/lab42/match.rb, line 78 def success? !!@match end
Private Instance Methods
_assure_matched?()
click to toggle source
# File lib/lab42/match.rb, line 85 def _assure_matched? raise NotMatchedYet, "you must not execute this operation on an unmatched object" unless matched? end
_assure_success()
click to toggle source
# File lib/lab42/match.rb, line 89 def _assure_success raise UnsuccessfulMerge, "this operation is not allowed after an unsuccessful merge" unless success? end
_assure_unambigous_args(arg, blk)
click to toggle source
# File lib/lab42/match.rb, line 93 def _assure_unambigous_args(arg, blk) raise AmbigousArgAndBlock, "you provided #{arg} and a block when only one of them is allowed" if arg && blk end
_clone_myself(&blk)
click to toggle source
# File lib/lab42/match.rb, line 97 def _clone_myself &blk m = @match md = @matched parts = @parts.clone subject = @subject newbie = self .class .new(rgx) newbie .instance_eval do @is_dirty = true @match = m @matched = md @rgx = rgx @subject = subject @parts = parts.clone end newbie.instance_eval(&blk) newbie.send(:_refresh) end
_init_defaults()
click to toggle source
# File lib/lab42/match.rb, line 124 def _init_defaults @match = nil @matched = false end
_init_parts()
click to toggle source
# File lib/lab42/match.rb, line 129 def _init_parts return _init_parts_with_captures if @match.size > 1 @parts = [ @match.pre_match, @match.to_s, @match.post_match ] _refresh end
_init_parts_with_captures()
click to toggle source
# File lib/lab42/match.rb, line 139 def _init_parts_with_captures captures = @match.captures.map{ |x| x || "" } last_match_idx = 0 begin_ends = (1...@match.size) .map do |idx| x = @match.begin(idx) || last_match_idx y = @match.end(idx) || x last_match_idx = y [x, y] end @parts = [@match.pre_match, subject[@match.begin(0)...begin_ends.first.first]] (1...@match.size.pred).each do |capture_index| @parts << captures[capture_index.pred] @parts << subject[begin_ends[capture_index.pred].last...begin_ends[capture_index].first] end @parts << (@match[-1] || "") @parts << subject[begin_ends.last.last...@match.end(0)] @parts << @match.post_match _refresh end
_match(with)
click to toggle source
# File lib/lab42/match.rb, line 161 def _match(with) @subject = with @matched = true @match = @rgx.match(with) _init_parts if @match self end
_refresh()
click to toggle source
# File lib/lab42/match.rb, line 118 def _refresh @groups = (1...match.length).map{ |n| @parts[2*n] } @string = @parts.join self end
_replace_part(index, with, &blk)
click to toggle source
# File lib/lab42/match.rb, line 169 def _replace_part(index, with, &blk) _clone_myself do @parts[index] = (with || blk.(@parts[index])).to_s end end