Sha256: 648ad40de41d618c02bf64461b3cfa9286435be08a23266154ecbe839dc24f5b

Contents?: true

Size: 886 Bytes

Versions: 6

Compression:

Stored size: 886 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
          elsif object.respond_to?(:values)
            csv << object.values
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
nocode-0.0.10 lib/nocode/steps/serialize/csv.rb
nocode-0.0.9 lib/nocode/steps/serialize/csv.rb
nocode-0.0.8 lib/nocode/steps/serialize/csv.rb
nocode-0.0.7 lib/nocode/steps/serialize/csv.rb
nocode-0.0.6 lib/nocode/steps/serialize/csv.rb
nocode-0.0.5 lib/nocode/steps/serialize/csv.rb