Sha256: 8f3894f7726ad5326a16a9508211aca6bad69556ff517e3d5378580517895ce4

Contents?: true

Size: 883 Bytes

Versions: 14

Compression:

Stored size: 883 Bytes

Contents

# frozen_string_literal: true

require_relative '../column_instruction'

class ActiveSet
  module Exporting
    class CSVStrategy
      require 'csv'

      def initialize(set, column_instructions)
        @set = set
        @column_instructions = column_instructions
      end

      def execute
        ::CSV.generate do |output|
          output << column_keys_for(item: @set.first)
          @set.each do |item|
            output << column_values_for(item: item)
          end
        end
      end

      private

      def column_keys_for(item:)
        columns.map do |column|
          ColumnInstruction.new(column, item).key
        end
      end

      def column_values_for(item:)
        columns.map do |column|
          ColumnInstruction.new(column, item).value
        end
      end

      def columns
        @column_instructions.compact
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
actionset-0.11.0 lib/active_set/exporting/csv_strategy.rb
actionset-0.10.0 lib/active_set/exporting/csv_strategy.rb
actionset-0.9.2 lib/active_set/exporting/csv_strategy.rb
actionset-0.9.1 lib/active_set/exporting/csv_strategy.rb
actionset-0.8.2 lib/active_set/exporting/csv_strategy.rb
actionset-0.8.1 lib/active_set/exporting/csv_strategy.rb
actionset-0.8.0 lib/active_set/exporting/csv_strategy.rb
activeset-0.8.6 lib/active_set/exporting/csv_strategy.rb
activeset-0.8.5 lib/active_set/exporting/csv_strategy.rb
activeset-0.8.4 lib/active_set/exporting/csv_strategy.rb
activeset-0.8.3 lib/active_set/exporting/csv_strategy.rb
activeset-0.8.2 lib/active_set/exporting/csv_strategy.rb
activeset-0.8.1 lib/active_set/exporting/csv_strategy.rb
activeset-0.8.0 lib/active_set/exporting/csv_strategy.rb