Sha256: 0de5098d5cb9140e03ac054c396f1306d0fcabfe5e98301dad6dadd8d2983c8f

Contents?: true

Size: 957 Bytes

Versions: 3

Compression:

Stored size: 957 Bytes

Contents

module RevealCK
  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
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
reveal-ck-0.1.5 lib/reveal-ck/file_splicer.rb
reveal-ck-0.1.4 lib/reveal-ck/file_splicer.rb
reveal-ck-0.1.3 lib/reveal-ck/file_splicer.rb