Class: Turnip::Table

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

Defined Under Namespace

Classes: WidthMismatch

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ Table

Returns a new instance of Table.



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

def initialize(raw)
  @raw = raw
end

Instance Attribute Details

#rawObject (readonly) Also known as: to_a

Returns the value of attribute raw.



3
4
5
# File 'lib/turnip/table.rb', line 3

def raw
  @raw
end

Instance Method Details

#eachObject



33
34
35
# File 'lib/turnip/table.rb', line 33

def each
  raw.each { |row| yield(row) }
end

#hashesObject



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

def hashes
  rows.map { |row| Hash[headers.zip(row)] }
end

#headersObject



12
13
14
# File 'lib/turnip/table.rb', line 12

def headers
  raw.first
end

#rowsObject



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

def rows
  raw.drop(1)
end

#rows_hashObject

Raises:



24
25
26
27
# File 'lib/turnip/table.rb', line 24

def rows_hash
  raise WidthMismatch.new(2, width) unless width == 2
  transpose.hashes.first
end

#transposeObject



29
30
31
# File 'lib/turnip/table.rb', line 29

def transpose
  self.class.new(raw.transpose)
end