Sha256: bca5126aa5bd2586273fb921f7930b841a2db25cb2c613a33be4173d389f51d0

Contents?: true

Size: 949 Bytes

Versions: 11

Compression:

Stored size: 949 Bytes

Contents

require 'csv_row_model/export/dynamic_columns'
require 'csv_row_model/export/attributes'

module CsvRowModel
  # Include this to with {Model} to have a RowModel for exporting to CSVs.
  module Export
    extend ActiveSupport::Concern

    included do
      include Attributes
      include DynamicColumns

      attr_reader :source_model, :context
      validates :source_model, presence: true
    end

    # @param [Model] source_model object to export to CSV
    # @param [Hash]  context
    def initialize(source_model, context={})
      @source_model = source_model
      @context      = OpenStruct.new(context)
    end

    def to_rows
      [to_row]
    end

    # @return [Array] an array of public_send(column_name) of the CSV model
    def to_row
      formatted_attributes.values
    end

    class_methods do
      def setup(csv, context={}, with_headers: true)
        csv << headers(context) if with_headers
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
csv_row_model-0.3.10 lib/csv_row_model/export.rb
csv_row_model-0.3.9 lib/csv_row_model/export.rb
csv_row_model-0.3.8 lib/csv_row_model/export.rb
csv_row_model-0.3.7 lib/csv_row_model/export.rb
csv_row_model-0.3.6 lib/csv_row_model/export.rb
csv_row_model-0.3.5 lib/csv_row_model/export.rb
csv_row_model-0.3.4 lib/csv_row_model/export.rb
csv_row_model-0.3.3 lib/csv_row_model/export.rb
csv_row_model-0.3.2 lib/csv_row_model/export.rb
csv_row_model-0.3.1 lib/csv_row_model/export.rb
csv_row_model-0.3.0 lib/csv_row_model/export.rb