Sha256: 42aa8ee4b99fd5c8d53bc6a3e3797b322b5c6c03c5954975f3440f9623b13f7d

Contents?: true

Size: 1.9 KB

Versions: 8

Compression:

Stored size: 1.9 KB

Contents

class Eco::API::UseCases::DefaultCases::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
    @people = people

    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?
      session.logger.info("There were no cyclic supervisors identified!!")
      return
    end

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

    File.open(file, "w") do |fd|
      if ext == "txt"
        create_file(data, file: file, format: :txt)
      elsif ext == "html"
        puts "html is still not supported"
        exit(1)
        create_file(data, file: file, format: :html)
      elsif ext == "json"
        puts "json is still not supported"
        exit(1)
        create_file(data, file: file, format: :json)
      end
    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}#{(lev > 0)? "+-#{lev}- " : ""}#{entry.name} (#{entry.external_id}|#{entry.email}|#{entry.id})\n"
      str << set_to_str(set, lev: lev + 1, format: format) unless !set || set.empty?
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
eco-helpers-2.5.10 lib/eco/api/usecases/default_cases/supers_cyclic_identify_case.rb
eco-helpers-2.5.9 lib/eco/api/usecases/default_cases/supers_cyclic_identify_case.rb
eco-helpers-2.5.8 lib/eco/api/usecases/default_cases/supers_cyclic_identify_case.rb
eco-helpers-2.5.7 lib/eco/api/usecases/default_cases/supers_cyclic_identify_case.rb
eco-helpers-2.5.6 lib/eco/api/usecases/default_cases/supers_cyclic_identify_case.rb
eco-helpers-2.5.5 lib/eco/api/usecases/default_cases/supers_cyclic_identify_case.rb
eco-helpers-2.5.4 lib/eco/api/usecases/default_cases/supers_cyclic_identify_case.rb
eco-helpers-2.5.3 lib/eco/api/usecases/default_cases/supers_cyclic_identify_case.rb