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