Sha256: 44590fb43d1333314310111820a6dec7c920777c6cc571231edf7e643a76c5ad

Contents?: true

Size: 1.59 KB

Versions: 9

Compression:

Stored size: 1.59 KB

Contents

class Eco::API::UseCases::Default::People::SupersHierarchy < Eco::API::Common::Loaders::UseCase
  name "supers-hierarchy"
  type :export

  attr_reader :people

  def main(people, session, options, usecase)
    options[:end_get] = false
    @people = people

    save!(hierarchy)
  end

  private

  def hierarchy
    Eco::API::Common::People::SupervisorHelpers.supervisors_tree(people)
  end

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

  def save!(data)
    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(tree, file:, format: :txt)
    File.open(file, "w") do |fd|
      fd << tree_to_str(tree, format: format)
    end
    puts "Generated file #{file}"
  end

  def tree_to_str(tree, lev: 0, format: :txt)
    raise "Required Hash tree structure. Given: #{tree.class}" unless tree.is_a?(Hash)
    "".tap do |str|
      tree.each do |entry, subtree|
        str << "#{"  " * lev}#{(lev > 0)? "+-#{lev}- " : ""}#{entry.name} (#{entry.external_id}|#{entry.email}|#{entry.id})\n"
        str << tree_to_str(subtree, lev: lev + 1, format: format) unless !subtree || subtree.empty?
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
eco-helpers-2.7.4 lib/eco/api/usecases/default/people/supers_hierarchy_case.rb
eco-helpers-2.7.2 lib/eco/api/usecases/default/people/supers_hierarchy_case.rb
eco-helpers-2.7.1 lib/eco/api/usecases/default/people/supers_hierarchy_case.rb
eco-helpers-2.7.0 lib/eco/api/usecases/default/people/supers_hierarchy_case.rb
eco-helpers-2.6.4 lib/eco/api/usecases/default/people/supers_hierarchy_case.rb
eco-helpers-2.6.3 lib/eco/api/usecases/default/people/supers_hierarchy_case.rb
eco-helpers-2.6.2 lib/eco/api/usecases/default/people/supers_hierarchy_case.rb
eco-helpers-2.6.1 lib/eco/api/usecases/default/people/supers_hierarchy_case.rb
eco-helpers-2.6.0 lib/eco/api/usecases/default/people/supers_hierarchy_case.rb