Sha256: 75e6e414c7bdc8c06a79ca95fd7e4b56d35d1622cccbd40a831b1022ab841977

Contents?: true

Size: 1.37 KB

Versions: 2

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

require_relative '../column_instruction'

class ActiveSet
  module Exporting
    class Operation
      def initialize(set, instructions_hash)
        @set = set
        @instructions_hash = instructions_hash
      end

      def execute
        strategy_for(format: operation_instructions[:format].to_s.downcase)
          .new(@set, operation_instructions[:columns])
          .execute
      end

      def operation_instructions
        @instructions_hash.symbolize_keys
      end

      private

      def strategy_for(format:)
        return CSVStrategy if format == 'csv'
      end
    end

    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

2 entries across 2 versions & 1 rubygems

Version Path
activeset-0.7.1 lib/active_set/exporting/operation.rb
activeset-0.7.0 lib/active_set/exporting/operation.rb