Class: Tabular::Column
Instance Attribute Summary collapse
-
#column_type ⇒ Object
readonly
Returns the value of attribute column_type.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
Instance Method Summary collapse
-
#cells ⇒ Object
All cells value under this Column.
-
#initialize(table, columns, key = nil, columns_map = {}) ⇒ Column
constructor
table
– parent Tablecolumn
– parent Columnskey
is normalized to a downcase, underscored symbol. - #inspect ⇒ Object
-
#max ⇒ Object
Maximum value for cells in the Column.
-
#precision ⇒ Object
Number of zeros to the right of the decimal point.
-
#render ⇒ Object
Human-friendly header string.
-
#renderer ⇒ Object
Renderer.
- #rows ⇒ Object
- #to_s ⇒ Object
Methods included from Blank
Constructor Details
#initialize(table, columns, key = nil, columns_map = {}) ⇒ Column
table
– parent Table column
– parent Columns key
is normalized to a downcase, underscored symbol
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/tabular/column.rb', line 10 def initialize(table, columns, key = nil, columns_map = {}) @columns = columns @table = table key = symbolize(key) columns_map = columns_map || {} map_for_key = columns_map[key] @column_type = :string case map_for_key when nil @key = key @column_type = :date if key == :date when Symbol @key = map_for_key @column_type = :date if key == :date when Hash @key = key @column_type = map_for_key[:column_type] else raise "Expected Symbol or Hash, but was #{map_for_key.class}" end end |
Instance Attribute Details
#column_type ⇒ Object (readonly)
Returns the value of attribute column_type.
5 6 7 |
# File 'lib/tabular/column.rb', line 5 def column_type @column_type end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
5 6 7 |
# File 'lib/tabular/column.rb', line 5 def key @key end |
Instance Method Details
#cells ⇒ Object
All cells value under this Column
39 40 41 |
# File 'lib/tabular/column.rb', line 39 def cells rows.map { |r| r[key] } end |
#inspect ⇒ Object
63 64 65 |
# File 'lib/tabular/column.rb', line 63 def inspect "#<Tabular::Column #{key} #{column_type}>" end |
#max ⇒ Object
Maximum value for cells in the Column. Determine with Ruby #max
44 45 46 |
# File 'lib/tabular/column.rb', line 44 def max cells.compact.max end |
#precision ⇒ Object
Number of zeros to the right of the decimal point. Useful for formtting time data.
49 50 51 |
# File 'lib/tabular/column.rb', line 49 def precision @precision || cells.map(&:to_f).map {|n| n.round(3) }.map {|n| n.to_s.split(".").last.gsub(/0+$/, "").length }.max end |
#render ⇒ Object
Human-friendly header string. Delegate to renderer
‘s render_header method.
54 55 56 |
# File 'lib/tabular/column.rb', line 54 def render renderer.render_header self end |
#renderer ⇒ Object
Renderer
59 60 61 |
# File 'lib/tabular/column.rb', line 59 def renderer @columns.renderer(key) end |
#rows ⇒ Object
34 35 36 |
# File 'lib/tabular/column.rb', line 34 def rows @table.rows end |
#to_s ⇒ Object
67 68 69 |
# File 'lib/tabular/column.rb', line 67 def to_s key.to_s end |