Sha256: 97afe3fac959b75adc222b97974ef2e04c3e10a1277e1b1cd73baa03bd8f2f1b

Contents?: true

Size: 1.8 KB

Versions: 11

Compression:

Stored size: 1.8 KB

Contents

module Martyr
  module Runtime
    class PivotAxis
      include ActiveModel::Model

      attr_accessor :grain_elements
      attr_reader :values

      def inspect
        grain_elements.inspect
      end

      def ids
        grain_elements.map(&:id)
      end

      # = CSV

      # Run on column axis only
      def add_header_column_cells_to_csv(csv, row_axis)
        grain_elements.each do |grain|
          prev_value = nil
          column_values = values.map do |x|
            # #chomp is just a trick to avoid removing consecutive (total)
            prev_value.try(:chomp, PivotCell::TOTAL_VALUE) == x[grain.id] ? nil : prev_value = x[grain.id]
          end
          csv << row_axis.csv_empty_row_axis_cells + column_values
        end
      end

      # Run on row axis only
      def csv_empty_row_axis_cells
        [nil] * grain_elements.length
      end

      # @return [Array<Hash>] where each hash is of format {level_id_1 => value_1, ... }
      def load_values(cells, reset: false)
        @values = nil if reset
        @values ||= cells.map { |cell| hash_grain_value_for(cell) }.uniq
      end

      def index_values_lookup
        @index_values_lookup ||= Hash[values.each_with_index.map{|value_hash, i| [value_hash, i]}]
      end

      def sort_cells_by_values(cells)
        cells.sort_by{|cell| index_values_lookup[hash_grain_value_for(cell)]}
      end

      def flat_values_nil_hash
        Hash[values.map{|hash| [hash.values.join(' : '), nil]}]
      end

      def hash_values_nil_hash
        Hash[values.map{|x| [x, nil]}]
      end

      def flat_grain_value_for(cell)
        grain_elements.map{ |grain| grain.cell_value(cell) }.join(' : ')
      end

      def hash_grain_value_for(cell)
        Hash[grain_elements.map{ |grain| [grain.id, grain.cell_value(cell)] }]
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
martyr-0.1.85.pre lib/martyr/runtime/pivot/pivot_axis.rb
martyr-0.1.84.pre lib/martyr/runtime/pivot/pivot_axis.rb
martyr-0.1.82.pre lib/martyr/runtime/pivot/pivot_axis.rb
martyr-0.1.81.pre lib/martyr/runtime/pivot/pivot_axis.rb
martyr-0.1.80.pre lib/martyr/runtime/pivot/pivot_axis.rb
martyr-0.1.79.pre lib/martyr/runtime/pivot/pivot_axis.rb
martyr-0.1.78.pre lib/martyr/runtime/pivot/pivot_axis.rb
martyr-0.1.77.pre lib/martyr/runtime/pivot/pivot_axis.rb
martyr-0.1.76.pre lib/martyr/runtime/pivot/pivot_axis.rb
martyr-0.1.75.pre lib/martyr/runtime/pivot/pivot_axis.rb
martyr-0.1.74.pre lib/martyr/runtime/pivot/pivot_axis.rb