lib/regexp-examples/backreferences.rb in regexp-examples-0.2.1 vs lib/regexp-examples/backreferences.rb in regexp-examples-0.2.2

- old
+ new

@@ -1,50 +1,19 @@ module RegexpExamples - class CaptureGroupResult < String - attr_reader :group_id, :subgroups - def initialize(group_id, subgroups, values) - @group_id = group_id - @subgroups = subgroups - super(values) - end - - def all_subgroups - [self, subgroups].flatten - end - - # Overridden in order to preserve the @group_id and @subgroups - def *(int) - self.class.new(group_id, subgroups, super) - end - # Overridden in order to preserve the @group_id and @subgroups - def gsub(regex) - self.class.new(group_id, subgroups, super) - end - end - class BackReferenceReplacer def substitute_backreferences(full_examples) - full_examples.map! do |full_example| - if full_example.is_a? String - [full_example] - else - full_example.map! do |partial_example| - partial_example.gsub(/__(\w+)__/) do |match| - find_backref_for(full_example, $1) - end - end + full_examples.map do |full_example| + while full_example.match(/__(\w+?)__/) + full_example.sub!(/__(\w+?)__/, find_backref_for(full_example, $1)) end + full_example end - full_examples end private def find_backref_for(full_example, group_id) - full_example.each do |partial_example| - next unless partial_example.respond_to?(:group_id) - partial_example.all_subgroups.each do |subgroup| - return subgroup if subgroup.group_id == group_id - end + full_example.all_subgroups.detect do |subgroup| + subgroup.group_id == group_id end end end