Class: Lutaml::Cli::InteractiveShell::ExportHandler

Inherits:
CommandBase
  • Object
show all
Defined in:
lib/lutaml/cli/interactive_shell/export_handler.rb

Instance Attribute Summary

Attributes inherited from CommandBase

#shell

Instance Method Summary collapse

Methods inherited from CommandBase

#bookmarks, #config, #current_path, #current_path=, #initialize, #last_results, #last_results=, #path_history, #repository

Constructor Details

This class inherits a constructor from Lutaml::Cli::InteractiveShell::CommandBase

Instance Method Details

#cmd_export(args) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/lutaml/cli/interactive_shell/export_handler.rb', line 9

def cmd_export(args)
  if last_results.nil? || last_results.empty?
    puts OutputFormatter.warning("No results to export")
    return
  end

  if args.size < 3 || args[0] != "last"
    puts OutputFormatter.warning("Usage: export last FORMAT FILE")
    return
  end

  format = args[1].downcase
  file_path = args[2]

  case format
  when "csv"
    export_csv(file_path)
  when "json"
    export_json(file_path)
  when "yaml"
    export_yaml(file_path)
  else
    puts OutputFormatter.error("Unsupported format: #{format}")
  end
end

#export_csv(file_path) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/lutaml/cli/interactive_shell/export_handler.rb', line 35

def export_csv(file_path)
  require "csv"

  CSV.open(file_path, "w") do |csv|
    csv << ["Qualified Name"]
    last_results.each do |qname|
      csv << [qname]
    end
  end

  puts OutputFormatter.success("Exported #{last_results.size} " \
                               "results to #{file_path}")
end

#export_json(file_path) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/lutaml/cli/interactive_shell/export_handler.rb', line 49

def export_json(file_path)
  require "json"

  File.write(file_path, JSON.pretty_generate(last_results))
  puts OutputFormatter.success("Exported #{last_results.size} " \
                               "results to #{file_path}")
end

#export_yaml(file_path) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/lutaml/cli/interactive_shell/export_handler.rb', line 57

def export_yaml(file_path)
  require "yaml"

  File.write(file_path, last_results.to_yaml)
  puts OutputFormatter.success("Exported #{last_results.size} " \
                               "results to #{file_path}")
end