Class: Sycsvpro::Inserter

Inherits:
Object
  • Object
show all
Defined in:
lib/sycsvpro/inserter.rb

Overview

Insert a text file into another textfile at a specified position

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Inserter

Creates an Inserter and takes options infile, outfile, insert-file and position where to insert the insert-file content. Default position is top



18
19
20
21
22
23
# File 'lib/sycsvpro/inserter.rb', line 18

def initialize(options={})
  @infile  = options[:infile]
  @outfile = options[:outfile]
  @insert  = options[:insert]
  @position = options[:position] || 'top'
end

Instance Attribute Details

#infileObject (readonly)

file to insert lines to



8
9
10
# File 'lib/sycsvpro/inserter.rb', line 8

def infile
  @infile
end

#insertObject (readonly)

file that contains the lines to insert



12
13
14
# File 'lib/sycsvpro/inserter.rb', line 12

def insert
  @insert
end

#outfileObject (readonly)

file to write result to



10
11
12
# File 'lib/sycsvpro/inserter.rb', line 10

def outfile
  @outfile
end

#positionObject (readonly)

position (top or bottom) where to insert the rows



14
15
16
# File 'lib/sycsvpro/inserter.rb', line 14

def position
  @position
end

Instance Method Details

#executeObject

Inserts the content of the insert-file at the specified positions (top or bottom)



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sycsvpro/inserter.rb', line 26

def execute
  File.open(outfile, 'w') do |out|
    if position.downcase == 'bottom'
      out.puts File.read(infile)
      out.puts File.read(insert)
    else
      out.puts File.read(insert)
      out.puts File.read(infile)
    end
  end
end