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) ⇒ 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 = Array.new end |
Instance Attribute Details
#buffer_size ⇒ Object (readonly)
Returns the value of attribute buffer_size.
12 13 14 |
# File 'lib/tzispa/data/transporter.rb', line 12 def buffer_size @buffer_size end |
#check_count ⇒ Object (readonly)
Returns the value of attribute check_count.
12 13 14 |
# File 'lib/tzispa/data/transporter.rb', line 12 def check_count @check_count end |
#data_separator ⇒ Object (readonly)
Returns the value of attribute data_separator.
12 13 14 |
# File 'lib/tzispa/data/transporter.rb', line 12 def data_separator @data_separator end |
#encoding ⇒ Object (readonly)
Returns the value of attribute encoding.
12 13 14 |
# File 'lib/tzispa/data/transporter.rb', line 12 def encoding @encoding end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
12 13 14 |
# File 'lib/tzispa/data/transporter.rb', line 12 def errors @errors end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
12 13 14 |
# File 'lib/tzispa/data/transporter.rb', line 12 def filename @filename end |
#line_separator ⇒ Object (readonly)
Returns the value of attribute line_separator.
12 13 14 |
# File 'lib/tzispa/data/transporter.rb', line 12 def line_separator @line_separator end |
#line_size ⇒ Object (readonly)
Returns the value of attribute line_size.
12 13 14 |
# File 'lib/tzispa/data/transporter.rb', line 12 def line_size @line_size end |
#lines ⇒ Object (readonly)
Returns the value of attribute lines.
12 13 14 |
# File 'lib/tzispa/data/transporter.rb', line 12 def lines @lines end |
#strip ⇒ Object (readonly)
Returns the value of attribute strip.
12 13 14 |
# File 'lib/tzispa/data/transporter.rb', line 12 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
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/tzispa/data/transporter.rb', line 49 def export(data, append = false, &block) count = 0 File.open(filename, append ? "ab:#{encoding}" : "wb:#{encoding}") { |fh| lock(fh, File::LOCK_EX) { |lfh| if data.is_a? Hash lfh << build_line(data, &block) count += 1 else data.each { |row| lfh << build_line(row, &block) count += 1 } end } } count end |
#import(dataset, columns) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/tzispa/data/transporter.rb', line 30 def import(dataset, columns) lines = 0 errors.clear buffer = Array.new File.open(filename, "rb:#{encoding}") { |fh| while line = read_line(fh, lines) lines += 1 values = block_given? ? yield(line) : line.split(data_separator) raise TransporterBadFormat.new("Bad file format at line #{lines}: columns number does not match with values") unless values.count == columns.count buffer << values flush_data(dataset, columns, buffer) if lines % buffer_size == 0 end flush_data dataset, columns, buffer } ds_count = dataset.count raise TransporterRecordCount.new ("Lines count (#{lines}) and records count (#{ds_count}) does not match") if check_count && lines != ds_count [lines, ds_count] end |