Sha256: 28002505bd184934560835e16daccf6eef248bca3fcbc4ce6d87978b40b6c6a9

Contents?: true

Size: 955 Bytes

Versions: 1

Compression:

Stored size: 955 Bytes

Contents

# frozen_string_literal: true

module Nocode
  module Steps
    module Serialize
      # Take the contents of a register and create a CSV out of its contents.  The CSV contents
      # will override the register specified.
      class Csv < Step
        option :register

        def perform
          input = registers[register_option]

          registers[register_option] = CSV.generate do |csv|
            array(input).each_with_index do |object, index|
              csv << object.keys if index.zero? && object.respond_to?(:keys)

              add_object(object, csv)
            end
          end
        end

        private

        def add_object(object, csv)
          object ||= {}

          if object.is_a?(Array)
            csv << object

            true
          elsif object.respond_to?(:values)
            csv << object.values

            true
          else
            false
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nocode-0.0.4 lib/nocode/steps/serialize/csv.rb