Sha256: 00f2e9328a57b27065ffee46fe525e61dd65d7f1c6132e5de78abedf094f3345

Contents?: true

Size: 1.8 KB

Versions: 6

Compression:

Stored size: 1.8 KB

Contents

module Eco
  module API
    module UseCases
      class DefaultCases
        class ToCsvCase < BaseCase

          def process
            @cases.define("to-csv", type: :export) do |people, session, options|
              unless people && !people.empty?
                session.logger.warn("No source people to create the file... aborting!")
                next false
              end

              unless file = options[:file]
                session.logger.error("Destination file not specified")
                next false
              end

              session.logger.info("going to create file: #{file}")
              CSV.open(file, "w") do |csv|
                deps   = {"supervisor_id" => {people: people}}
                entry  = session.new_entry(people.first, dependencies: deps)
                header = entry.to_hash.keys

                if options.dig(:nice_header)
                  name_maps = session.schema.fields_by_alt_id.transform_values do |fld|
                    fld.name
                  end.merge({
                    "policy_group_ids" => "User Group(s)",
                    "email" => "Email",
                    "name" => "Name",
                    "supervisor_id" => "Manager ID",
                    "filter_tags" => "Locations",
                    "default_tag" => "Default Location",
                    "id" => "ecoPortal ID"
                  })
                  header = header.map {|name| name_maps[name] ? name_maps[name] : name}
                end

                csv << header
                people.each do |person|
                  csv << session.new_entry(person, dependencies: deps).to_hash.values
                end
              end
              true
            end
          end

        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
eco-helpers-0.8.4 lib/eco/api/usecases/default_cases/to_csv_case.rb
eco-helpers-0.8.3 lib/eco/api/usecases/default_cases/to_csv_case.rb
eco-helpers-0.8.2 lib/eco/api/usecases/default_cases/to_csv_case.rb
eco-helpers-0.8.1 lib/eco/api/usecases/default_cases/to_csv_case.rb
eco-helpers-0.7.2 lib/eco/api/usecases/default_cases/to_csv_case.rb
eco-helpers-0.7.1 lib/eco/api/usecases/default_cases/to_csv_case.rb