Sha256: 236be6c53ff2dd84023088987a31a4dd90dbb7fbcc7116b3bfebeec46e52073f

Contents?: true

Size: 1.63 KB

Versions: 2

Compression:

Stored size: 1.63 KB

Contents

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

  attr_reader :session, :people, :options

  def main(people, session, options, usecase)
    @session = session; @options = options; @people = people

    save!(hierarchy)
    exit(0)
  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

2 entries across 2 versions & 1 rubygems

Version Path
eco-helpers-2.0.16 lib/eco/api/usecases/default_cases/supers_hierarchy_case.rb
eco-helpers-2.0.15 lib/eco/api/usecases/default_cases/supers_hierarchy_case.rb