Class: ATV

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/atv.rb

Constant Summary collapse

SUBSTITUTIONS =
{
  'true' => true,
  'false' => false,
  'null' => nil,
  '' => nil
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ ATV

Returns a new instance of ATV.



16
17
18
19
20
21
22
23
# File 'lib/atv.rb', line 16

def initialize(io)
  @io = io
  @has_separators = has_separators?(@io)
  @io.rewind
  @io.readline
  @headers = split_table_line(@io.readline.chomp)
  @io.readline
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



14
15
16
# File 'lib/atv.rb', line 14

def headers
  @headers
end

Class Method Details

.from_string(string) ⇒ Object



42
43
44
# File 'lib/atv.rb', line 42

def self.from_string(string)
  self.new(StringIO.new(string))
end

Instance Method Details

#eachObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/atv.rb', line 25

def each
  line_data = []
  @io.each_line do |line|
    next if line =~ /^\s*#/
    line.chomp!
    if (!@has_separators && !line_data.empty?) || line =~ /^\|\-/
      folded_items = line_data.transpose.map { |tokens| tokens.join(' ').rstrip }
      converted_folded_items = folded_items.map { |token| SUBSTITUTIONS.has_key?(token) ? SUBSTITUTIONS[token] : token }
      csv_row = CSV::Row.new(@headers, converted_folded_items)
      yield csv_row if csv_row.size > 0
      line_data = []
      next if @has_separators
    end
    line_data << split_table_line(line)
  end
end