Sha256: 966d9b38d538d942db5338db82cd1c9f8e4b53790db87a93b0a726a1e6c3845f

Contents?: true

Size: 853 Bytes

Versions: 1

Compression:

Stored size: 853 Bytes

Contents

module RevealCK
  #
  # Public: A FileSplicer knows how to insert a list of lines into a
  # file.
  #
  class FileSplicer

    def initialize(lines)
      @lines = lines
    end

    def insert(new_lines, opts)
      index = @lines.each.with_index do |line, i|
        break i + 1 if line.include? opts[:after]
      end
      @lines.insert(index, new_lines).flatten
    end

    def self.insert!(to_insert, opts)
      to_insert_lines = readlines to_insert
      insert_into_file = opts[:into]
      insert_into_lines = readlines insert_into_file

      splicer = FileSplicer.new insert_into_lines
      spliced_lines = splicer.insert to_insert_lines, after: opts[:after]
      File.open(insert_into_file, 'w') { |file| file << spliced_lines.join }
    end

    private

    def self.readlines(file)
      File.open(file).readlines
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
reveal-ck-0.1.6 lib/reveal-ck/file_splicer.rb