Class: Tabular::Column

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table, columns, key = nil) ⇒ Column

table – parent Table column – parent Columns key should be a normalized, downcase, underscored symbol



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/tabular/column.rb', line 10

def initialize(table, columns, key = nil)
  @columns = columns
  @table = table
  @key = self.columns.column_mapper.map(key)

  @column_type = if @key && @key.to_s["date"]
                   :date
                 elsif @key && @key.to_s[/\?\z/]
                   :boolean
                 else
                   :string
                 end
end

Instance Attribute Details

#column_typeObject (readonly)

Returns the value of attribute column_type.



5
6
7
# File 'lib/tabular/column.rb', line 5

def column_type
  @column_type
end

#keyObject (readonly)

Returns the value of attribute key.



5
6
7
# File 'lib/tabular/column.rb', line 5

def key
  @key
end

Instance Method Details

#cellsObject

All cells value under this Column



29
30
31
# File 'lib/tabular/column.rb', line 29

def cells
  rows.map { |r| r[key] }
end

#inspectObject



62
63
64
# File 'lib/tabular/column.rb', line 62

def inspect
  "#<Tabular::Column #{key} #{column_type}>"
end

#maxObject

Maximum value for cells in the Column. Determine with Ruby #max



34
35
36
# File 'lib/tabular/column.rb', line 34

def max
  cells.compact.max
end

#precisionObject

Number of zeros to the right of the decimal point. Useful for formtting time data.



39
40
41
# File 'lib/tabular/column.rb', line 39

def precision
  @precision ||= cells.map(&:to_f).map { |n| n.round(3) }.map { |n| n.to_s.split(".").last.gsub(/0+$/, "").length }.max
end

#renderObject

Human-friendly header string. Delegate to renderer‘s render_header method.



49
50
51
# File 'lib/tabular/column.rb', line 49

def render
  renderer.render_header self
end

#rendererObject

Renderer



54
55
56
# File 'lib/tabular/column.rb', line 54

def renderer
  @columns.renderer(key)
end

#rowsObject



24
25
26
# File 'lib/tabular/column.rb', line 24

def rows
  @table.rows
end

#to_sObject



66
67
68
# File 'lib/tabular/column.rb', line 66

def to_s
  key.to_s
end

#to_space_delimitedObject



58
59
60
# File 'lib/tabular/column.rb', line 58

def to_space_delimited
  to_s.ljust width
end

#widthObject

Widest string in column



44
45
46
# File 'lib/tabular/column.rb', line 44

def width
  @width ||= (cells.map(&:to_s) << to_s).map(&:size).max
end