Sha256: 881d845bd764f269e3da9e157326d882217d37192932129eb0a13a33a2c35c48

Contents?: true

Size: 875 Bytes

Versions: 3

Compression:

Stored size: 875 Bytes

Contents

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 = find_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') { |f| f << spliced_lines.join }
  end

  private

  def self.find_file(path)
    if File.exists? path
      path
    else
      File.expand_original(File.join(Dir.pwd, to_insert))
    end
  end

  def self.readlines(path)
    file = find_file path
    File.open(file).readlines
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
reveal-ck-0.1.2 lib/reveal-ck/file_splicer.rb
reveal-ck-0.1.1 lib/reveal-ck/file_splicer.rb
reveal-ck-0.1.0 lib/reveal-ck/file_splicer.rb