Class: Tzispa::Data::Transporter
- Inherits:
-
Object
- Object
- Tzispa::Data::Transporter
- Defined in:
- lib/tzispa/data/transporter.rb
Constant Summary collapse
- DEFAULT_ENCODING =
'ASCII-8BIT'- DEFAULT_BUFFER_SIZE =
2048- DEFAULT_LINE_SEPARATOR =
"\n"
Instance Attribute Summary collapse
-
#buffer_size ⇒ Object
readonly
Returns the value of attribute buffer_size.
-
#check_count ⇒ Object
readonly
Returns the value of attribute check_count.
-
#data_separator ⇒ Object
readonly
Returns the value of attribute data_separator.
-
#encoding ⇒ Object
readonly
Returns the value of attribute encoding.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#line_separator ⇒ Object
readonly
Returns the value of attribute line_separator.
-
#line_size ⇒ Object
readonly
Returns the value of attribute line_size.
-
#lines ⇒ Object
readonly
Returns the value of attribute lines.
-
#strip ⇒ Object
readonly
Returns the value of attribute strip.
Instance Method Summary collapse
- #exist? ⇒ Boolean
- #export(data, append = false, &block) ⇒ Object
- #import(dataset, columns, &block) ⇒ Object
-
#initialize(fn, options = {}) ⇒ Transporter
constructor
A new instance of Transporter.
Constructor Details
#initialize(fn, options = {}) ⇒ Transporter
Returns a new instance of Transporter.
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/tzispa/data/transporter.rb', line 14 def initialize(fn, = {}) @filename = fn @buffer_size = [:buffer_size] || DEFAULT_BUFFER_SIZE @encoding = [:encoding] || DEFAULT_ENCODING @line_separator = [:line_separator] || DEFAULT_LINE_SEPARATOR @line_size = [:line_size] @data_separator = [:data_separator] @strip = [:strip] @check_count = [:check_count] @errors = [] end |
Instance Attribute Details
#buffer_size ⇒ Object (readonly)
Returns the value of attribute buffer_size.
11 12 13 |
# File 'lib/tzispa/data/transporter.rb', line 11 def buffer_size @buffer_size end |
#check_count ⇒ Object (readonly)
Returns the value of attribute check_count.
11 12 13 |
# File 'lib/tzispa/data/transporter.rb', line 11 def check_count @check_count end |
#data_separator ⇒ Object (readonly)
Returns the value of attribute data_separator.
11 12 13 |
# File 'lib/tzispa/data/transporter.rb', line 11 def data_separator @data_separator end |
#encoding ⇒ Object (readonly)
Returns the value of attribute encoding.
11 12 13 |
# File 'lib/tzispa/data/transporter.rb', line 11 def encoding @encoding end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
11 12 13 |
# File 'lib/tzispa/data/transporter.rb', line 11 def errors @errors end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
11 12 13 |
# File 'lib/tzispa/data/transporter.rb', line 11 def filename @filename end |
#line_separator ⇒ Object (readonly)
Returns the value of attribute line_separator.
11 12 13 |
# File 'lib/tzispa/data/transporter.rb', line 11 def line_separator @line_separator end |
#line_size ⇒ Object (readonly)
Returns the value of attribute line_size.
11 12 13 |
# File 'lib/tzispa/data/transporter.rb', line 11 def line_size @line_size end |
#lines ⇒ Object (readonly)
Returns the value of attribute lines.
11 12 13 |
# File 'lib/tzispa/data/transporter.rb', line 11 def lines @lines end |
#strip ⇒ Object (readonly)
Returns the value of attribute strip.
11 12 13 |
# File 'lib/tzispa/data/transporter.rb', line 11 def strip @strip end |
Instance Method Details
#exist? ⇒ Boolean
26 27 28 |
# File 'lib/tzispa/data/transporter.rb', line 26 def exist? File.exist? filename end |
#export(data, append = false, &block) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/tzispa/data/transporter.rb', line 40 def export(data, append = false, &block) count = 0 File.open(filename, append ? "ab:#{encoding}" : "wb:#{encoding}") do |fh| lock(fh, File::LOCK_EX) do |lfh| if data.is_a? Hash lfh << build_line(data, &block) count += 1 else data.each do |row| lfh << build_line(row, &block) count += 1 end end end end count end |
#import(dataset, columns, &block) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/tzispa/data/transporter.rb', line 30 def import(dataset, columns, &block) errors.clear File.open(filename, "rb:#{encoding}") do |fh| lines = process_file_import fh, dataset, columns, &block ds_count = dataset.count raise TransporterRecordCount.new(lines, ds_count) if check_count && lines != ds_count [lines, ds_count] end end |