Class: Ruport::Data::Table::Pivot

Inherits:
Object
  • Object
show all
Defined in:
lib/ruport/data/table.rb

Overview

:nodoc:

Defined Under Namespace

Modules: Operations

Instance Method Summary collapse

Constructor Details

#initialize(table, group_col, pivot_col, summary_col, options = {}) ⇒ Pivot

Returns a new instance of Pivot.



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ruport/data/table.rb', line 32

def initialize(table, group_col, pivot_col, summary_col, options = {})
  @table = table
  @group_column = group_col
  @pivot_column = pivot_col
  @summary_column = summary_col
  @pivot_order = options[:pivot_order]
  @operation = options[:operation] || :first
  unless Operations.respond_to?(@operation, true)
    raise ArgumentError, "Unknown pivot operation '#{@operation}'"
  end
end

Instance Method Details

#columnObject

Column in the first column in the pivoted table (without the group column)



57
58
59
# File 'lib/ruport/data/table.rb', line 57

def column
  @column ||= @table.map { |row| row[@group_column] }.uniq
end

#rowObject

Row is the first row in the pivoted table (without the group column)



45
46
47
48
49
50
51
52
53
54
# File 'lib/ruport/data/table.rb', line 45

def row
  return @row if defined?(@row)

  pivot_column_grouping = Grouping(@table, :by => @pivot_column)

  ordering = self.class.row_order_to_group_order(@pivot_order)
  pivot_column_grouping.sort_grouping_by!(ordering) if ordering

  @row = pivot_column_grouping.map { |name,_grouping| name }
end

#to_tableObject



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ruport/data/table.rb', line 61

def to_table
  table = Table.new
  create_header(table)

  column.each do |column_entry|
    row_values = row.map { |row_entry| values[column_entry][row_entry] }
    table << [column_entry] + row_values
  end

  table
end

#valuesObject



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ruport/data/table.rb', line 73

def values
  @values ||= Hash.new do |row_values, column_entry|
    rows_group = rows_groups[column_entry]

    row_values[column_entry] =
      row.inject({}) do |values, row_entry|
        matching_rows = rows_group.rows_with(@pivot_column => row_entry)
        values[row_entry] = perform_operation(matching_rows)
        values
      end
  end
end