Sha256: 2611c4abf5b9fc1eb68dafa1945f6089a1062dffd0f613558cac836b6bbc1fab

Contents?: true

Size: 1.52 KB

Versions: 26

Compression:

Stored size: 1.52 KB

Contents

class Eco::API::UseCases::DefaultCases::ToCsvCase < Eco::API::Common::Loaders::UseCase
  name "to-csv"
  type :export

  def main(people, session, options, usecase)
    unless people && !people.empty?
      session.logger.warn("No source people to create the file... aborting!")
      return false
    end

    unless file = options[:file] || options.dig(:export, :file, :name)
      session.logger.error("Destination file not specified")
      return 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.external_entry.keys

      if options.dig(:nice_header) || options.dig(:export, :options, :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).external_entry.values
      end
    end
    exit(0)
  end

end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
eco-helpers-2.0.13 lib/eco/api/usecases/default_cases/to_csv_case.rb
eco-helpers-2.0.12 lib/eco/api/usecases/default_cases/to_csv_case.rb
eco-helpers-2.0.11 lib/eco/api/usecases/default_cases/to_csv_case.rb
eco-helpers-2.0.10 lib/eco/api/usecases/default_cases/to_csv_case.rb
eco-helpers-2.0.9 lib/eco/api/usecases/default_cases/to_csv_case.rb
eco-helpers-2.0.8 lib/eco/api/usecases/default_cases/to_csv_case.rb
eco-helpers-2.0.7 lib/eco/api/usecases/default_cases/to_csv_case.rb
eco-helpers-2.0.6 lib/eco/api/usecases/default_cases/to_csv_case.rb
eco-helpers-2.0.5 lib/eco/api/usecases/default_cases/to_csv_case.rb
eco-helpers-2.0.4 lib/eco/api/usecases/default_cases/to_csv_case.rb
eco-helpers-2.0.3 lib/eco/api/usecases/default_cases/to_csv_case.rb
eco-helpers-2.0.2 lib/eco/api/usecases/default_cases/to_csv_case.rb
eco-helpers-1.5.15 lib/eco/api/usecases/default_cases/to_csv_case.rb
eco-helpers-1.5.14 lib/eco/api/usecases/default_cases/to_csv_case.rb
eco-helpers-1.5.13 lib/eco/api/usecases/default_cases/to_csv_case.rb
eco-helpers-1.5.12 lib/eco/api/usecases/default_cases/to_csv_case.rb
eco-helpers-1.5.11 lib/eco/api/usecases/default_cases/to_csv_case.rb
eco-helpers-1.5.10 lib/eco/api/usecases/default_cases/to_csv_case.rb
eco-helpers-1.5.9 lib/eco/api/usecases/default_cases/to_csv_case.rb
eco-helpers-1.5.8 lib/eco/api/usecases/default_cases/to_csv_case.rb