Class: Tzispa::Data::Transporter

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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, options = {})
  @filename = fn
  @buffer_size = options[:buffer_size] || DEFAULT_BUFFER_SIZE
  @encoding = options[:encoding] || DEFAULT_ENCODING
  @line_separator = options[:line_separator] || DEFAULT_LINE_SEPARATOR
  @line_size = options[:line_size]
  @data_separator = options[:data_separator]
  @strip = options[:strip]
  @check_count = options[:check_count]
  @errors = []
end

Instance Attribute Details

#buffer_sizeObject (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_countObject (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_separatorObject (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

#encodingObject (readonly)

Returns the value of attribute encoding.



11
12
13
# File 'lib/tzispa/data/transporter.rb', line 11

def encoding
  @encoding
end

#errorsObject (readonly)

Returns the value of attribute errors.



11
12
13
# File 'lib/tzispa/data/transporter.rb', line 11

def errors
  @errors
end

#filenameObject (readonly)

Returns the value of attribute filename.



11
12
13
# File 'lib/tzispa/data/transporter.rb', line 11

def filename
  @filename
end

#line_separatorObject (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_sizeObject (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

#linesObject (readonly)

Returns the value of attribute lines.



11
12
13
# File 'lib/tzispa/data/transporter.rb', line 11

def lines
  @lines
end

#stripObject (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

Returns:

  • (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