Sha256: aa7f0223820498e8a33227f52f076b9281aeadfd9e2438b0fbf84d5694cb5cb6

Contents?: true

Size: 1.27 KB

Versions: 11

Compression:

Stored size: 1.27 KB

Contents

module Martyr
  module Runtime
    class PivotRow
      include ActiveModel::Model

      attr_reader :pivot_table, :header, :cells
      delegate :column_axis, to: :pivot_table

      # @attribute header [Hash] array of keys and values for each grain in the axis
      def initialize(pivot_table, header, cells)
        @pivot_table = pivot_table
        @header = header
        @cells = column_axis.sort_cells_by_values(cells)
      end

      def inspect
        {row_header: header, column_headers: column_headers}.inspect
      end

      # = CSV

      def to_a(previous: nil)
        row_arr = header.values + cells_by_column_headers.values.map(&:value)
        return row_arr unless previous
        row_arr
        # row_arr.each_with_index.map{|x, i| i < header.length && x.try(:chomp, PivotCell::TOTAL_VALUE) == previous[i] ? nil : x}
      end

      # = Value retrieval

      def cell_at(column_header)
        cells_by_column_headers[column_header]
      end

      def [](index)
        cell_at column_headers[index]
      end

      def column_headers
        column_axis.hash_values_nil_hash.keys
      end

      def cells_by_column_headers
        @cells_by_column_headers ||= Hash[cells.map{|cell| [cell.to_axis_values(column_axis, flat: false), 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_row.rb
martyr-0.1.84.pre lib/martyr/runtime/pivot/pivot_row.rb
martyr-0.1.82.pre lib/martyr/runtime/pivot/pivot_row.rb
martyr-0.1.81.pre lib/martyr/runtime/pivot/pivot_row.rb
martyr-0.1.80.pre lib/martyr/runtime/pivot/pivot_row.rb
martyr-0.1.79.pre lib/martyr/runtime/pivot/pivot_row.rb
martyr-0.1.78.pre lib/martyr/runtime/pivot/pivot_row.rb
martyr-0.1.77.pre lib/martyr/runtime/pivot/pivot_row.rb
martyr-0.1.76.pre lib/martyr/runtime/pivot/pivot_row.rb
martyr-0.1.75.pre lib/martyr/runtime/pivot/pivot_row.rb
martyr-0.1.74.pre lib/martyr/runtime/pivot/pivot_row.rb