Class: Sycsvpro::Inserter
- Inherits:
-
Object
- Object
- Sycsvpro::Inserter
- Defined in:
- lib/sycsvpro/inserter.rb
Overview
Insert a text file into another textfile at a specified position
Instance Attribute Summary collapse
-
#infile ⇒ Object
readonly
file to insert lines to.
-
#insert ⇒ Object
readonly
file that contains the lines to insert.
-
#outfile ⇒ Object
readonly
file to write result to.
-
#position ⇒ Object
readonly
position (top or bottom) where to insert the rows.
Instance Method Summary collapse
-
#execute ⇒ Object
Inserts the content of the insert-file at the specified positions (top or bottom).
-
#initialize(options = {}) ⇒ Inserter
constructor
Creates an Inserter and takes options infile, outfile, insert-file and position where to insert the insert-file content.
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(={}) @infile = [:infile] @outfile = [:outfile] @insert = [:insert] @position = [:position] || 'top' end |
Instance Attribute Details
#infile ⇒ Object (readonly)
file to insert lines to
8 9 10 |
# File 'lib/sycsvpro/inserter.rb', line 8 def infile @infile end |
#insert ⇒ Object (readonly)
file that contains the lines to insert
12 13 14 |
# File 'lib/sycsvpro/inserter.rb', line 12 def insert @insert end |
#outfile ⇒ Object (readonly)
file to write result to
10 11 12 |
# File 'lib/sycsvpro/inserter.rb', line 10 def outfile @outfile end |
#position ⇒ Object (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
#execute ⇒ Object
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 |