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_key(&block) ⇒ Object
Call
block
for each key. -
#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)
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/tabular/row.rb', line 18 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.
12 13 14 |
# File 'lib/tabular/row.rb', line 12 def index @index end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
13 14 15 |
# File 'lib/tabular/row.rb', line 13 def source @source end |
Instance Method Details
#[](key) ⇒ Object
Cell value by symbol. E.g., row
37 38 39 |
# File 'lib/tabular/row.rb', line 37 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_
42 43 44 45 46 47 48 49 50 |
# File 'lib/tabular/row.rb', line 42 def []=(key, value) if columns.key?(key) @array[columns.index(key)] = value else @array << value columns << key end hash[key] = value end |
#blank? ⇒ Boolean
97 98 99 |
# File 'lib/tabular/row.rb', line 97 def blank? @array.all? { |cell| is_blank?(cell) } end |
#columns ⇒ Object
Tabluar::Columns
102 103 104 |
# File 'lib/tabular/row.rb', line 102 def columns @table.columns end |
#delete(key) ⇒ Object
77 78 79 80 |
# File 'lib/tabular/row.rb', line 77 def delete(key) @array.delete key hash.delete key end |
#each(&block) ⇒ Object
Call block
for each cell
53 54 55 |
# File 'lib/tabular/row.rb', line 53 def each(&block) @array.each(&block) end |
#each_key(&block) ⇒ Object
Call block
for each key
58 59 60 |
# File 'lib/tabular/row.rb', line 58 def each_key(&block) keys.each(&block) end |
#each_with_key(&block) ⇒ Object
Call block
for each cell
63 64 65 |
# File 'lib/tabular/row.rb', line 63 def each_with_key(&block) hash.each(&block) end |
#inspect ⇒ Object
130 131 132 |
# File 'lib/tabular/row.rb', line 130 def inspect hash.inspect end |
#join(sep = nil) ⇒ Object
For pretty-printing cell values
73 74 75 |
# File 'lib/tabular/row.rb', line 73 def join(sep = nil) @array.join(sep) end |
#keys ⇒ Object
Keys for all columns
68 69 70 |
# File 'lib/tabular/row.rb', line 68 def keys hash.keys end |
#last? ⇒ Boolean
Is this the last row?
93 94 95 |
# File 'lib/tabular/row.rb', line 93 def last? index == @table.rows.size - 1 end |
#metadata ⇒ Object
112 113 114 |
# File 'lib/tabular/row.rb', line 112 def @table. end |
#next ⇒ Object
Next Row
88 89 90 |
# File 'lib/tabular/row.rb', line 88 def next @table.rows[index + 1] end |
#previous ⇒ Object
Previous Row
83 84 85 |
# File 'lib/tabular/row.rb', line 83 def previous @table.rows[index - 1] if index.positive? end |
#render(key) ⇒ Object
By default, return self. Customize by setting Table#renderer or Column#renderers
107 108 109 110 |
# File 'lib/tabular/row.rb', line 107 def render(key) column = columns[key] column.renderer.render column, self end |
#to_hash ⇒ Object
116 117 118 |
# File 'lib/tabular/row.rb', line 116 def to_hash hash.dup end |
#to_s ⇒ Object
134 135 136 |
# File 'lib/tabular/row.rb', line 134 def to_s @array.join(", ").to_s end |
#to_space_delimited ⇒ Object
120 121 122 123 124 125 126 127 128 |
# File 'lib/tabular/row.rb', line 120 def to_space_delimited cells = [] hash.each_key do |key| cells << (render(key) || "").ljust(columns[key].width) end cells.join " " end |