Class: Nukumber::Model::Table
- Inherits:
-
Object
- Object
- Nukumber::Model::Table
- Defined in:
- lib/nukumber/model/table.rb
Instance Attribute Summary collapse
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#lengths ⇒ Object
readonly
Returns the value of attribute lengths.
-
#rows ⇒ Object
readonly
Returns the value of attribute rows.
Instance Method Summary collapse
- #all_row_hashes ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(rows) ⇒ Table
constructor
A new instance of Table.
- #row_count ⇒ Object
- #row_hash(n) ⇒ Object
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
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
6 7 8 |
# File 'lib/nukumber/model/table.rb', line 6 def headers @headers end |
#lengths ⇒ Object (readonly)
Returns the value of attribute lengths.
6 7 8 |
# File 'lib/nukumber/model/table.rb', line 6 def lengths @lengths end |
#rows ⇒ Object (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_hashes ⇒ Object
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
27 28 29 |
# File 'lib/nukumber/model/table.rb', line 27 def empty? @empty end |
#row_count ⇒ Object
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 |