Module: Charting::Transforms::InstanceMethods

Defined in:
lib/ar_to_chart/charting/transforms.rb

Instance Method Summary collapse

Instance Method Details

#pivot(*args) ⇒ Object

Takes one row and returns an array of objects of the same class where columns are pivoted to become rows. The new rows are of the same class as the current instance.

Arguments

The columns that become the row/column pairs in the result array.
An empty list means pivot all attributes.

Options

Specify the optional column names for the result array.  The defaults
are: 

:category => :category 
:value => value


32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ar_to_chart/charting/transforms.rb', line 32

def pivot(*args)
  options = (args.last.is_a?(Hash) ? args.pop : {}).merge(self.class.const_get(:DEFAULT_PIVOT_COLUMNS))
  args.flatten!
  attributes = args.size > 0 ? args : self.attributes.map(&:first)
  klass = self.class
  return attributes.inject([]) do |rows, arg|
    row = klass.new
    row[options[:attribute]]  = arg.to_s
    row[options[:value]]      = self[arg].to_i
    rows << row
    rows
  end
end