Class: FatTable::Footer

Inherits:
Object
  • Object
show all
Defined in:
lib/fat_table/footer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label = 'Total', table, label_col: nil, group: false) ⇒ Footer

Initialize a labeled footer, optionally specifying a column for the label and whether the footer is to be a group footer. One or more values for the footer are added later with the #add_value method.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fat_table/footer.rb', line 16

def initialize(label = 'Total', table, label_col: nil, group: false)
  @label = label

  unless table.is_a?(Table)
    raise ArgumentError, 'Footer.new needs a table argument'
  end
  if label_col.nil?
    @label_col = table.headers.first
  else
    unless table.headers.include?(label_col.as_sym)
      raise ArgumentError, "Footer.new label column '#{label_col}' not a header of table."
    end
    @label_col = label_col.as_sym
  end
  @table = table
  @group = group
  @group_cols = {}
  @values = {}
  insert_labels_in_label_col
  make_accessor_methods
end

Instance Attribute Details

#groupObject (readonly)

Returns the value of attribute group.



5
6
7
# File 'lib/fat_table/footer.rb', line 5

def group
  @group
end

#label_colObject (readonly)

Returns the value of attribute label_col.



5
6
7
# File 'lib/fat_table/footer.rb', line 5

def label_col
  @label_col
end

#tableObject (readonly)

Returns the value of attribute table.



5
6
7
# File 'lib/fat_table/footer.rb', line 5

def table
  @table
end

#valuesObject (readonly)

Returns the value of attribute values.



5
6
7
# File 'lib/fat_table/footer.rb', line 5

def values
  @values
end

Instance Method Details

#[](key) ⇒ Object

Return the value of under the +key+ header, or if this is a group footer, return an array of the values for all the groups under the +key+ header.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/fat_table/footer.rb', line 78

def [](key)
  key = key.as_sym
  if values.keys.include?(key)
    if group
      values[key]
    else
      values[key].last
    end
  elsif table.headers.include?(label_col.as_sym)
    nil
  else
    raise ArgumentError, "No column header '#{key}' in footer table"
  end
end

#add_value(col, agg) ⇒ Object

Add a value to a footer for the footer's table at COL. The value of the footer is determined by AGG. If it is a symbol, such as :sum or :avg, it must be a valid aggregating function and the value is determined by applying the aggregate to the columns in the table, or in a group footer, to the rows in the group. If AGG is not a symbol, but it can be converted to a valid type for a FatTable::Table column, then it is so converted and the value set to it directly without invoking an aggregate function.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fat_table/footer.rb', line 48

def add_value(col, agg)
  col = col.as_sym
  if col.nil?
    raise ArgumentError, 'Footer#add_value col is nil but must name a table column.'
  else
    unless table.headers.include?(col.as_sym)
      raise ArgumentError, "Footer#add_value col '#{col}' not a header of the table."
    end
  end

  if group
    number_of_groups.times do |k|
      values[col] ||= []
      values[col] << calc_val(agg, col, k)
    end
  else
    values[col] = [calc_val(agg, col)]
  end
end

#column(h, k = nil) ⇒ Object

Return a FatTable::Column object for the header h and, if a group, the kth group.



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/fat_table/footer.rb', line 106

def column(h, k = nil)
  if group && k.nil?
    raise ArgumentError, 'Footer#column(h, k) missing the group number argument k'
  end

  if group
    @group_cols[h] ||= table.group_cols(h)
    @group_cols[h][k]
  else
    table.column(h)
  end
end

#items(h, k = nil) ⇒ Object

Return an Array of the values for the header h and, if a group, for the kth group.



123
124
125
# File 'lib/fat_table/footer.rb', line 123

def items(h, k = nil)
  column(h, k).items
end

#label(k = 0) ⇒ Object

Return the value of the label, for the kth group if grouped.



69
70
71
# File 'lib/fat_table/footer.rb', line 69

def label(k = 0)
  @values[@label_col][k]
end

#number_of_groupsObject

Return the total number of groups in the table to which this footer belongs. Note that if the table has both group footers and normal footers, this will return the number of groups even for a normal footer.



98
99
100
# File 'lib/fat_table/footer.rb', line 98

def number_of_groups
  table.number_of_groups
end

#to_h(k = nil) ⇒ Object

Return a Hash with a key for each column header mapped to the footer value for that column, nil for unused columns. Use the key +k+ to specify which group to access in the case of a group footer.



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/fat_table/footer.rb', line 132

def to_h(k = nil)
  hsh = {}
  if group
    table.headers.each do |h|
      hsh[h] = values[h] ? values[h][k] : nil
    end
  else
    table.headers.each do |h|
      hsh[h] =
        if values[h]
          values[h].first
        else
          nil
        end
    end
  end
  hsh
end