Class: Tabular::Row
- Inherits:
-
Object
- Object
- Tabular::Row
- 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
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.
- #blank? ⇒ Boolean
-
#columns ⇒ Object
Tabluar::Columns.
- #delete(key) ⇒ Object
-
#each(&block) ⇒ Object
Call
block
for each cell. -
#each_with_key(&block) ⇒ Object
Call
block
for 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.
-
#last? ⇒ Boolean
Is this the last row?.
- #metadata ⇒ Object
-
#next ⇒ Object
Next Row.
-
#previous ⇒ Object
Previous Row.
-
#render(key) ⇒ Object
By default, return self.
- #to_hash ⇒ Object
- #to_s ⇒ Object
- #to_space_delimited ⇒ Object
Methods included from Keys
Methods included from Blank
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)
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/tabular/row.rb', line 16 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
Returns the value of attribute index.
10 11 12 |
# File 'lib/tabular/row.rb', line 10 def index @index end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
11 12 13 |
# File 'lib/tabular/row.rb', line 11 def source @source end |
Instance Method Details
#[](key) ⇒ Object
Cell value by symbol. E.g., row
35 36 37 |
# File 'lib/tabular/row.rb', line 35 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_
40 41 42 43 44 45 46 47 48 |
# File 'lib/tabular/row.rb', line 40 def []=(key, value) if columns.has_key?(key) @array[columns.index(key)] = value else @array << value columns << key end hash[key] = value end |
#blank? ⇒ Boolean
92 93 94 |
# File 'lib/tabular/row.rb', line 92 def blank? @array.all? { |cell| is_blank?(cell) } end |
#columns ⇒ Object
Tabluar::Columns
97 98 99 |
# File 'lib/tabular/row.rb', line 97 def columns @table.columns end |
#delete(key) ⇒ Object
70 71 72 73 |
# File 'lib/tabular/row.rb', line 70 def delete(key) @array.delete key hash.delete key end |
#each(&block) ⇒ Object
Call block
for each cell
51 52 53 |
# File 'lib/tabular/row.rb', line 51 def each(&block) @array.each(&block) end |
#each_with_key(&block) ⇒ Object
Call block
for each cell
56 57 58 |
# File 'lib/tabular/row.rb', line 56 def each_with_key(&block) hash.each(&block) end |
#inspect ⇒ Object
125 126 127 |
# File 'lib/tabular/row.rb', line 125 def inspect hash.inspect end |
#join(sep = nil) ⇒ Object
For pretty-printing cell values
66 67 68 |
# File 'lib/tabular/row.rb', line 66 def join(sep = nil) @array.join(sep) end |
#keys ⇒ Object
Keys for all columns
61 62 63 |
# File 'lib/tabular/row.rb', line 61 def keys hash.keys end |
#last? ⇒ Boolean
Is this the last row?
88 89 90 |
# File 'lib/tabular/row.rb', line 88 def last? index == @table.rows.size - 1 end |
#metadata ⇒ Object
107 108 109 |
# File 'lib/tabular/row.rb', line 107 def @table. end |
#next ⇒ Object
Next Row
83 84 85 |
# File 'lib/tabular/row.rb', line 83 def next @table.rows[index + 1] end |
#previous ⇒ Object
Previous Row
76 77 78 79 80 |
# File 'lib/tabular/row.rb', line 76 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
102 103 104 105 |
# File 'lib/tabular/row.rb', line 102 def render(key) column = columns[key] column.renderer.render column, self end |
#to_hash ⇒ Object
111 112 113 |
# File 'lib/tabular/row.rb', line 111 def to_hash hash.dup end |
#to_s ⇒ Object
129 130 131 |
# File 'lib/tabular/row.rb', line 129 def to_s @array.join(", ").to_s end |
#to_space_delimited ⇒ Object
115 116 117 118 119 120 121 122 123 |
# File 'lib/tabular/row.rb', line 115 def to_space_delimited _cells = [] hash.each do |key, _| _cells << (render(key) || "").ljust(columns[key].width) end _cells.join " " end |