Sha256: 2b3c732412e6079606d1d1ad76249da70d2a39c0f4dbf5d0d393eaf012900a0b

Contents?: true

Size: 1.9 KB

Versions: 19

Compression:

Stored size: 1.9 KB

Contents

class Eco::API::UseCases::Default::People::Treat::SupersCyclicIdentify < Eco::API::Common::Loaders::UseCase
  name "identify-cyclic-supers"
  type :export

  attr_reader :people

  def main(_people, _session, options, _usecase)
    options[:end_get] = false
    save!(cyclic_sets)
  end

  private

  def cyclic_sets
    Eco::API::Common::People::SupervisorHelpers.identify_cyclic_chains(people)
  end

  def file
    @file ||= options.dig(:output, :file) || "cyclic_supers.txt"
  end

  def save!(data)
    if data.empty?
      log(:info) { "There were no cyclic supervisors identified!!" }
      return
    end

    ext = File.extname(file).downcase.delete(".")

    case ext
    when "txt"
      create_file(data, file: file, format: :txt)
    when "html"
      log(:warn) { "html is still not supported" }
      exit(1)
      # create_file(data, file: file, format: :html)
    when "json"
      log(:warn) { "json is still not supported" }
      exit(1)
      # create_file(data, file: file, format: :json)
    end
  end

  def create_file(sets, file:, format: :txt)
    File.open(file, "w") do |fd|
      fd << sets_to_str(sets, format: format)
    end
    puts "Generated file #{file}"
  end

  def sets_to_str(sets, format: :txt)
    raise "Required Array. Given: #{sets.class}" unless sets.is_a?(Array)
    "".tap do |str|
      sets.each do |set|
        str << set_to_str(set, format: format)
      end
    end
  end

  def set_to_str(set, lev: 0, format: :txt)
    raise "Required Array. Given: #{set.class}" unless set.is_a?(Array)

    "".tap do |str|
      entry = set.shift
      str << ("  " * lev)
      str << lev&.positive? ? "+-#{lev}- " : ""
      str << entry.name
      str << " (#{entry.external_id}|#{entry.email}|#{entry.id})\n"
      next unless set && !set.empty?
      str << set_to_str(set, lev: lev + 1, format: format)
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
eco-helpers-3.0.4 lib/eco/api/usecases/default/people/treat/supers_cyclic_identify_case.rb
eco-helpers-3.0.3 lib/eco/api/usecases/default/people/treat/supers_cyclic_identify_case.rb
eco-helpers-3.0.2 lib/eco/api/usecases/default/people/treat/supers_cyclic_identify_case.rb
eco-helpers-3.0.1 lib/eco/api/usecases/default/people/treat/supers_cyclic_identify_case.rb
eco-helpers-3.0.0 lib/eco/api/usecases/default/people/treat/supers_cyclic_identify_case.rb
eco-helpers-2.7.25 lib/eco/api/usecases/default/people/treat/supers_cyclic_identify_case.rb
eco-helpers-2.7.24 lib/eco/api/usecases/default/people/treat/supers_cyclic_identify_case.rb
eco-helpers-2.7.23 lib/eco/api/usecases/default/people/treat/supers_cyclic_identify_case.rb
eco-helpers-2.7.22 lib/eco/api/usecases/default/people/treat/supers_cyclic_identify_case.rb
eco-helpers-2.7.21 lib/eco/api/usecases/default/people/treat/supers_cyclic_identify_case.rb
eco-helpers-2.7.20 lib/eco/api/usecases/default/people/treat/supers_cyclic_identify_case.rb
eco-helpers-2.7.19 lib/eco/api/usecases/default/people/treat/supers_cyclic_identify_case.rb
eco-helpers-2.7.18 lib/eco/api/usecases/default/people/treat/supers_cyclic_identify_case.rb
eco-helpers-2.7.17 lib/eco/api/usecases/default/people/treat/supers_cyclic_identify_case.rb
eco-helpers-2.7.16 lib/eco/api/usecases/default/people/treat/supers_cyclic_identify_case.rb
eco-helpers-2.7.15 lib/eco/api/usecases/default/people/treat/supers_cyclic_identify_case.rb
eco-helpers-2.7.14 lib/eco/api/usecases/default/people/treat/supers_cyclic_identify_case.rb
eco-helpers-2.7.13 lib/eco/api/usecases/default/people/treat/supers_cyclic_identify_case.rb
eco-helpers-2.7.12 lib/eco/api/usecases/default/people/treat/supers_cyclic_identify_case.rb