Class: TSV::Table

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/tsv/table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, params = {}, &block) ⇒ Table

Returns a new instance of Table.



10
11
12
13
14
# File 'lib/tsv/table.rb', line 10

def initialize(source, params = {}, &block)
  self.header = params.fetch(:header, true)
  self.source = source
  self.enumerator.each(&block) if block_given?
end

Instance Attribute Details

#headerObject

Returns the value of attribute header.



8
9
10
# File 'lib/tsv/table.rb', line 8

def header
  @header
end

#sourceObject

Returns the value of attribute source.



8
9
10
# File 'lib/tsv/table.rb', line 8

def source
  @source
end

Instance Method Details

#data_enumeratorObject



47
48
49
# File 'lib/tsv/table.rb', line 47

def data_enumerator
  source.each_line
end

#enumeratorObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/tsv/table.rb', line 24

def enumerator
  @enumerator ||= ::Enumerator.new do |y|
    lines = data_enumerator

    first_line = generate_row_from begin
      lines.next
    rescue StopIteration => ex
      ''
    end

    local_header = if self.header
      first_line
    else
      lines.rewind
      generate_default_header_from first_line
    end

    loop do
      y << TSV::Row.new(generate_row_from(lines.next).freeze, local_header.freeze)
    end
  end
end

#with_headerObject



16
17
18
# File 'lib/tsv/table.rb', line 16

def with_header
  self.class.new(self.source, header: true)
end

#without_headerObject



20
21
22
# File 'lib/tsv/table.rb', line 20

def without_header
  self.class.new(self.source, header: false)
end