Class: Nukumber::Model::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/nukumber/model/table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rows) ⇒ Table

Returns a new instance of Table.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/nukumber/model/table.rb', line 8

def initialize(rows)
  @headers, @rows, @lengths, @empty = [], [], [], true
  return if rows.nil?
  @empty = false
  rows.each_with_index do |r, i|
    if i == 0
      @headers = r.cells.map { |c| c.to_s }
      @lengths = @headers.map { |h| h.length }
    else
      row = []
      r.cells.each_with_index do |c, j|
        row << c.to_s
        @lengths[j] = [ c.to_s.length, @lengths[j] ].max
      end
      @rows << row
    end
  end
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



6
7
8
# File 'lib/nukumber/model/table.rb', line 6

def headers
  @headers
end

#lengthsObject (readonly)

Returns the value of attribute lengths.



6
7
8
# File 'lib/nukumber/model/table.rb', line 6

def lengths
  @lengths
end

#rowsObject (readonly)

Returns the value of attribute rows.



6
7
8
# File 'lib/nukumber/model/table.rb', line 6

def rows
  @rows
end

Instance Method Details

#all_row_hashesObject



43
44
45
46
47
# File 'lib/nukumber/model/table.rb', line 43

def all_row_hashes
  out = []
  row_count.times { |i| out << row_hash(i) }
  out
end

#empty?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/nukumber/model/table.rb', line 27

def empty?
  @empty
end

#row_countObject



31
32
33
# File 'lib/nukumber/model/table.rb', line 31

def row_count
  @rows.size
end

#row_hash(n) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/nukumber/model/table.rb', line 35

def row_hash(n)
  out = {}
  @rows[n].each_with_index do |val,i|
    out[@headers[i]] = val
  end
  out
end