Class: TablePrint::Column

Inherits:
Object
  • Object
show all
Defined in:
lib/table_print/column.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#dataObject

Returns the value of attribute data.



4
5
6
# File 'lib/table_print/column.rb', line 4

def data
  @data
end

#default_widthObject

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_widthObject

Returns the value of attribute fixed_width.



4
5
6
# File 'lib/table_print/column.rb', line 4

def fixed_width
  @fixed_width
end

#formattersObject

Returns the value of attribute formatters.



3
4
5
# File 'lib/table_print/column.rb', line 3

def formatters
  @formatters
end

#min_widthObject

Returns the value of attribute min_width.



4
5
6
# File 'lib/table_print/column.rb', line 4

def min_width
  @min_width
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/table_print/column.rb', line 4

def name
  @name
end

#time_formatObject

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_widthObject



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_methodObject



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

#widthObject



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