Class: Tabular::Row
- Inherits:
-
Object
- Object
- Tabular::Row
- Includes:
- Enumerable, Keys
- Defined in:
- lib/tabular/row.rb
Overview
Associate list of cells. Each Table has a list of Rows. Access Row cells via symbols. Ex: row
Instance Attribute Summary collapse
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Cell value by symbol.
-
#[]=(key, value) ⇒ Object
Set cell value.
-
#columns ⇒ Object
Tabluar::Columns.
- #delete(key) ⇒ Object
-
#each(&block) ⇒ Object
Call
blockfor each cell. -
#each_with_key(&block) ⇒ Object
Call
blockfor each cell. -
#initialize(table, cells = [], source = nil) ⇒ Row
constructor
table– Tablecells– array (not neccessarily Strings)source– original data before mapped to Hash or Array (optional). - #inspect ⇒ Object
-
#join(sep = nil) ⇒ Object
For pretty-printing cell values.
-
#keys ⇒ Object
Keys for all columns.
-
#previous ⇒ Object
Previous Row.
-
#render(key) ⇒ Object
By default, return self.
- #to_hash ⇒ Object
- #to_s ⇒ Object
Methods included from Keys
Constructor Details
#initialize(table, cells = [], source = nil) ⇒ Row
table – Table cells – array (not neccessarily Strings) source – original data before mapped to Hash or Array (optional)
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/tabular/row.rb', line 15 def initialize(table, cells = [], source = nil) @table = table @source = source || cells if cells.respond_to?(:keys) @array = cells.values @hash = {} cells.each do |key, value| @hash[key_to_sym(key)] = value end else @array = cells @hash = nil end @index = table.rows.size end |
Instance Attribute Details
#index ⇒ Object (readonly)
Returns the value of attribute index.
9 10 11 |
# File 'lib/tabular/row.rb', line 9 def index @index end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
10 11 12 |
# File 'lib/tabular/row.rb', line 10 def source @source end |
Instance Method Details
#[](key) ⇒ Object
Cell value by symbol. E.g., row
34 35 36 |
# File 'lib/tabular/row.rb', line 34 def [](key) hash[key] end |
#[]=(key, value) ⇒ Object
Set cell value. Adds cell to end of Row and adds new Column if there is no Column for +key_
39 40 41 42 43 44 45 46 47 |
# File 'lib/tabular/row.rb', line 39 def []=(key, value) if columns.has_key?(key) @array[columns.index(key)] = value else @array << value columns << key end hash[key] = value end |
#columns ⇒ Object
Tabluar::Columns
82 83 84 |
# File 'lib/tabular/row.rb', line 82 def columns @table.columns end |
#delete(key) ⇒ Object
69 70 71 72 |
# File 'lib/tabular/row.rb', line 69 def delete(key) @array.delete key hash.delete key end |
#each(&block) ⇒ Object
Call block for each cell
50 51 52 |
# File 'lib/tabular/row.rb', line 50 def each(&block) @array.each(&block) end |
#each_with_key(&block) ⇒ Object
Call block for each cell
55 56 57 |
# File 'lib/tabular/row.rb', line 55 def each_with_key(&block) hash.each(&block) end |
#inspect ⇒ Object
96 97 98 |
# File 'lib/tabular/row.rb', line 96 def inspect hash.inspect end |
#join(sep = nil) ⇒ Object
For pretty-printing cell values
65 66 67 |
# File 'lib/tabular/row.rb', line 65 def join(sep = nil) @array.join(sep) end |
#keys ⇒ Object
Keys for all columns
60 61 62 |
# File 'lib/tabular/row.rb', line 60 def keys hash.keys end |
#previous ⇒ Object
Previous Row
75 76 77 78 79 |
# File 'lib/tabular/row.rb', line 75 def previous if index > 0 @table.rows[index - 1] end end |
#render(key) ⇒ Object
By default, return self. Customize by setting Table#renderer or Column#renderers
87 88 89 90 |
# File 'lib/tabular/row.rb', line 87 def render(key) column = columns[key] column.renderer.render column, self end |
#to_hash ⇒ Object
92 93 94 |
# File 'lib/tabular/row.rb', line 92 def to_hash hash.dup end |
#to_s ⇒ Object
100 101 102 |
# File 'lib/tabular/row.rb', line 100 def to_s @array.join(", ").to_s end |