Class: Tabular::Column
- Inherits:
-
Object
- Object
- Tabular::Column
- Defined in:
- lib/tabular/column.rb
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) ⇒ Column
constructor
table
– parent Tablecolumn
– parent Columnskey
should be a normalized, 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
- #to_space_delimited ⇒ Object
-
#width ⇒ Object
Widest string in column.
Constructor Details
#initialize(table, columns, key = nil) ⇒ Column
table
– parent Table column
– parent Columns key
should be a normalized, downcase, underscored symbol
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/tabular/column.rb', line 8 def initialize(table, columns, key = nil) @columns = columns @table = table @key = self.columns.column_mapper.map(key) if @key && @key.to_s["date"] @column_type = :date elsif @key && @key.to_s[/\?\z/] @column_type = :boolean else @column_type = :string end end |
Instance Attribute Details
#column_type ⇒ Object (readonly)
Returns the value of attribute column_type.
3 4 5 |
# File 'lib/tabular/column.rb', line 3 def column_type @column_type end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
3 4 5 |
# File 'lib/tabular/column.rb', line 3 def key @key end |
Instance Method Details
#cells ⇒ Object
All cells value under this Column
27 28 29 |
# File 'lib/tabular/column.rb', line 27 def cells rows.map { |r| r[key] } end |
#inspect ⇒ Object
60 61 62 |
# File 'lib/tabular/column.rb', line 60 def inspect "#<Tabular::Column #{key} #{column_type}>" end |
#max ⇒ Object
Maximum value for cells in the Column. Determine with Ruby #max
32 33 34 |
# File 'lib/tabular/column.rb', line 32 def max cells.compact.max end |
#precision ⇒ Object
Number of zeros to the right of the decimal point. Useful for formtting time data.
37 38 39 |
# File 'lib/tabular/column.rb', line 37 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.
47 48 49 |
# File 'lib/tabular/column.rb', line 47 def render renderer.render_header self end |
#renderer ⇒ Object
Renderer
52 53 54 |
# File 'lib/tabular/column.rb', line 52 def renderer @columns.renderer(key) end |
#rows ⇒ Object
22 23 24 |
# File 'lib/tabular/column.rb', line 22 def rows @table.rows end |
#to_s ⇒ Object
64 65 66 |
# File 'lib/tabular/column.rb', line 64 def to_s key.to_s end |
#to_space_delimited ⇒ Object
56 57 58 |
# File 'lib/tabular/column.rb', line 56 def to_space_delimited to_s.ljust width end |
#width ⇒ Object
Widest string in column
42 43 44 |
# File 'lib/tabular/column.rb', line 42 def width @width ||= (cells.map(&:to_s) << to_s).map(&:size).max end |