lib/regexp-examples/backreferences.rb in regexp-examples-0.1.0 vs lib/regexp-examples/backreferences.rb in regexp-examples-0.2.0

- old
+ new

@@ -5,14 +5,19 @@ @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 @@ -34,19 +39,13 @@ private def find_backref_for(full_example, group_id) full_example.each do |partial_example| next unless partial_example.respond_to?(:group_id) - case - when partial_example.group_id == group_id - return partial_example - when sub_partial_example = find_backref_for(partial_example.subgroups, group_id) - # TODO: This line does NOT work for all nested backreference groups - # Need to revisit this logic and find a better solution, if possible - return sub_partial_example.result.detect{|sub_partial_result| partial_example.include? sub_partial_result} - end + partial_example.all_subgroups.each do |subgroup| + return subgroup if subgroup.group_id == group_id + end end - nil end end end