Class: TablePrint::Column
- Inherits:
-
Object
- Object
- TablePrint::Column
- Defined in:
- lib/table_print/column.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#default_width ⇒ Object
Returns the value of attribute default_width.
-
#fixed_width ⇒ Object
Returns the value of attribute fixed_width.
-
#formatters ⇒ Object
Returns the value of attribute formatters.
-
#min_width ⇒ Object
Returns the value of attribute min_width.
-
#name ⇒ Object
Returns the value of attribute name.
-
#time_format ⇒ Object
Returns the value of attribute time_format.
Instance Method Summary collapse
- #add_formatter(formatter) ⇒ Object
- #data_width ⇒ Object
- #display_method ⇒ Object
- #display_method=(method) ⇒ Object
-
#initialize(attr_hash = {}) ⇒ Column
constructor
A new instance of Column.
- #width ⇒ Object
Constructor Details
#initialize(attr_hash = {}) ⇒ Column
Returns a new instance of Column.
6 7 8 9 10 11 |
# File 'lib/table_print/column.rb', line 6 def initialize(attr_hash={}) @formatters = [] attr_hash.each do |k, v| self.send("#{k}=", v) end end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
4 5 6 |
# File 'lib/table_print/column.rb', line 4 def data @data end |
#default_width ⇒ Object
Returns the value of attribute default_width.
4 5 6 |
# File 'lib/table_print/column.rb', line 4 def default_width @default_width end |
#fixed_width ⇒ Object
Returns the value of attribute fixed_width.
4 5 6 |
# File 'lib/table_print/column.rb', line 4 def fixed_width @fixed_width end |
#formatters ⇒ Object
Returns the value of attribute formatters.
3 4 5 |
# File 'lib/table_print/column.rb', line 3 def formatters @formatters end |
#min_width ⇒ Object
Returns the value of attribute min_width.
4 5 6 |
# File 'lib/table_print/column.rb', line 4 def min_width @min_width end |
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/table_print/column.rb', line 4 def name @name end |
#time_format ⇒ Object
Returns the value of attribute time_format.
4 5 6 |
# File 'lib/table_print/column.rb', line 4 def time_format @time_format end |
Instance Method Details
#add_formatter(formatter) ⇒ Object
32 33 34 |
# File 'lib/table_print/column.rb', line 32 def add_formatter(formatter) @formatters << formatter end |
#data_width ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/table_print/column.rb', line 36 def data_width if multibyte_count [ name.each_char.collect{|c| c.bytesize == 1 ? 1 : 2}.inject(0, &:+), Array(data).compact.collect(&:to_s).collect{|m| m.each_char.collect{|n| n.bytesize == 1 ? 1 : 2}.inject(0, &:+)}.max ].compact.max || 0 else [ name.length, Array(data).compact.collect(&:to_s).collect(&:length).max ].compact.max || 0 end end |
#display_method ⇒ Object
28 29 30 |
# File 'lib/table_print/column.rb', line 28 def display_method @display_method ||= name end |
#display_method=(method) ⇒ Object
23 24 25 26 |
# File 'lib/table_print/column.rb', line 23 def display_method=(method) method = method.to_s unless method.is_a? Proc @display_method = method end |
#width ⇒ Object
50 51 52 53 54 55 |
# File 'lib/table_print/column.rb', line 50 def width return fixed_width if fixed_width width = [(default_width || max_width), data_width].min [(min_width || 0), width].max end |