Sha256: c2bdafc97c1a154ecf1021c101f2fabacdd95b84e20bd148ce0d73623b84bf70

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

#
# Copyright (c) 2018-present, Blue Marble Payroll, LLC
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
#

module Bumblebee
  # Wraps up columns and provides to main methods:
  # generate_csv: take in an array of objects and return a string (CSV contents)
  # parse_csv: take in a string and return an array of OpenStruct objects
  class Template
    attr_reader :columns

    def initialize(columns = [])
      @columns = ::Bumblebee::Column.array(columns)
    end

    # Return array of strings (headers)
    def headers
      columns.map(&:header)
    end

    def generate_csv(objects, options = {})
      objects = objects.is_a?(Hash) ? [objects] : Array(objects)

      CSV.generate(make_options(options)) do |csv|
        objects.each do |object|
          row = columns.map { |column| column.object_to_csv(object) }

          csv << row
        end
      end
    end

    def parse_csv(string, options = {})
      csv = CSV.new(string, make_options(options))

      # Drop the first record, it is the header record
      csv.to_a[1..-1].map do |row|
        # Build up a hash using the column one at a time
        extracted_hash = columns.inject({}) do |hash, column|
          hash.merge(column.csv_to_object(row))
        end

        extracted_hash
      end
    end

    private

    def make_options(options = {})
      options.merge(headers: headers, write_headers: true)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bumblebee-1.0.0 lib/bumblebee/template.rb