Class: TSV::Table
Instance Attribute Summary collapse
-
#header ⇒ Object
Returns the value of attribute header.
-
#source ⇒ Object
Returns the value of attribute source.
Instance Method Summary collapse
- #data_enumerator ⇒ Object
- #enumerator ⇒ Object
-
#initialize(source, params = {}, &block) ⇒ Table
constructor
A new instance of Table.
- #with_header ⇒ Object
- #without_header ⇒ Object
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
#header ⇒ Object
Returns the value of attribute header.
8 9 10 |
# File 'lib/tsv/table.rb', line 8 def header @header end |
#source ⇒ Object
Returns the value of attribute source.
8 9 10 |
# File 'lib/tsv/table.rb', line 8 def source @source end |
Instance Method Details
#data_enumerator ⇒ Object
47 48 49 |
# File 'lib/tsv/table.rb', line 47 def data_enumerator source.each_line end |
#enumerator ⇒ Object
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_header ⇒ Object
16 17 18 |
# File 'lib/tsv/table.rb', line 16 def with_header self.class.new(self.source, header: true) end |
#without_header ⇒ Object
20 21 22 |
# File 'lib/tsv/table.rb', line 20 def without_header self.class.new(self.source, header: false) end |