Class: Eco::API::UseCases::Default::Utils::GroupCsv

Inherits:
Custom::UseCase show all
Defined in:
lib/eco/api/usecases/default/utils/group_csv_case.rb,
lib/eco/api/usecases/default/utils/cli/group_csv_cli.rb,
lib/eco/api/usecases/default/utils/group_csv_case/file_handler.rb

Overview

Note:

you might run first the sort-csv case.

Note:

when using jsonl as an output format, it doesn't merge fields, but it groups them based on some criteria.

  • In this case you need to define a json_builder method that returns a hash.
Note:

you must inherit from this case and define the constants.

GROUP_BY_FIELD = 'target_csv_field'.freeze # if -by command option isn't used GROUPED_FIELDS = [ 'joined_field_1', 'joined_field_2', 'joined_field_3', ].freeze

Note:

that GROUPED_FIELDS isn't necessary if jsonl is used as an output format

This script assumes that for the GROUP_BY_FIELD rows are consecutive.

Defined Under Namespace

Classes: Cli, FileHandler

Constant Summary collapse

OUTPUT_FORMAT =

:csv or :jsonl

:csv

Instance Attribute Summary

Attributes included from Language::AuxiliarLogger

#logger

Instance Method Summary collapse

Methods inherited from Common::Loaders::UseCase

#cli_apply!, #initialize

Methods included from Common::Loaders::UseCase::CliIdentify

#cli, #cli!

Methods included from Common::Loaders::UseCase::TargetModel

#target_model

Methods included from Common::Loaders::UseCase::Type

#type

Methods inherited from Common::Loaders::CaseBase

#name, name_only_once!, original_name

Methods inherited from Common::Loaders::Base

<=>, created_at, #initialize, set_created_at!

Methods included from Language::Klass::InheritableClassVars

#inheritable_attrs, #inheritable_class_vars, #inherited

Methods included from Language::Klass::Naming

#instance_variable_name, #to_constant

Methods included from Language::Klass::Hierarchy

#descendants, #descendants?

Methods included from Language::Klass::Builder

#new_class

Methods included from Language::Klass::Uid

#uid

Methods included from Language::Klass::Resolver

#class_resolver, #resolve_class

Methods included from Language::Klass::Const

#if_const, #redef_without_warning

Methods included from Language::AuxiliarLogger

#log

Constructor Details

This class inherits a constructor from Eco::API::Common::Loaders::UseCase

Instance Method Details

#main(*_args) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/eco/api/usecases/default/utils/group_csv_case.rb', line 24

def main(*_args)
  if simulate?
    count = Eco::CSV.count(input_file)
    log(:info) { "CSV '#{input_file}' has #{count} rows." }
  else
    msg = "You should define a json_builder method when using jsonl as output format"
    raise msg unless respond_to?(:json_builder, true) || output_format != :jsonl

    generate_file
  end
end