def append_to_existing_spec(class_or_module, dry_run, rails_mode, spec_path)
      existing_spec = File.read(spec_path)
      lacking_methods = class_or_module.method_list.
        select { |m| m.visibility == :public }.
        reject { |m| existing_spec.match(m.name) }

      if lacking_methods.empty?
        puts "#{spec_path} skipped."
      else
        # These names are used in ERB template, don't delete.
        # rubocop:disable Lint/UselessAssignment
        methods_to_generate = lacking_methods
        c = class_or_module
        # rubocop:enable Lint/UselessAssignment

        erb = RSpecKickstarter::ERBFactory.new(@delta_template).get_instance_for_appending(rails_mode, spec_path)
        additional_spec = erb.result(binding)

        last_end_not_found = true
        code = existing_spec.split("\n").reverse.reject { |line|
          before_modified = last_end_not_found
          last_end_not_found = line.gsub(/#.+$/, '').strip != 'end' if before_modified
          before_modified
        }.reverse.join("\n") + "\n" + additional_spec + "\nend\n"

        if dry_run
          puts "----- #{spec_path} -----"
          puts code
        else
          File.open(spec_path, 'w') { |f| f.write(code) }
        end
        puts "#{spec_path} modified."
      end
    end
def append_to_existing_spec(class_or_module, dry_run, rails_mode, spec_path)
      existing_spec = File.read(spec_path)
      lacking_methods = class_or_module.method_list.
        select { |m| m.visibility == :public }.
        reject { |m| existing_spec.match(m.name) }

      if lacking_methods.empty?
        puts "#{spec_path} skipped."
      else
        # These names are used in ERB template, don't delete.
        # rubocop:disable Lint/UselessAssignment
        methods_to_generate = lacking_methods
        c = class_or_module
        # rubocop:enable Lint/UselessAssignment

        erb = RSpecKickstarter::ERBFactory.new(@delta_template).get_instance_for_appending(rails_mode, spec_path)
        additional_spec = erb.result(binding)

        last_end_not_found = true
        code = existing_spec.split("\n").reverse.reject { |line|
          before_modified = last_end_not_found
          last_end_not_found = line.gsub(/#.+$/, '').strip != 'end' if before_modified
          before_modified
        }.reverse.join("\n") + "\n" + additional_spec + "\nend\n"

        if dry_run
          puts "----- #{spec_path} -----"
          puts code
        else
          File.open(spec_path, 'w') { |f| f.write(code) }
        end
        puts "#{spec_path} modified."
      end
    end
rubocop:enable Metrics/AbcSize

 Appends new tests to the existing spec.

 rubocop:disable Metrics/AbcSize