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

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

Constant Summary collapse

EXPORT_FORMATS =
{
  "csv" => :export_csv,
  "json" => :export_json,
  "yaml" => :export_yaml,
}.freeze

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



13
14
15
16
17
18
# File 'lib/lutaml/cli/interactive_shell/export_handler.rb', line 13

def cmd_export(args)
  return warn_no_results if last_results.nil? || last_results.empty?
  return warn_export_usage unless valid_export_args?(args)

  dispatch_export(args[1].downcase, args[2])
end

#dispatch_export(format, file_path) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/lutaml/cli/interactive_shell/export_handler.rb', line 24

def dispatch_export(format, file_path)
  exporter = EXPORT_FORMATS[format]
  if exporter
    public_send(exporter, file_path)
  else
    puts OutputFormatter.error("Unsupported format: #{format}")
  end
end

#export_csv(file_path) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/lutaml/cli/interactive_shell/export_handler.rb', line 41

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



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

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



63
64
65
66
67
68
69
# File 'lib/lutaml/cli/interactive_shell/export_handler.rb', line 63

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

#valid_export_args?(args) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/lutaml/cli/interactive_shell/export_handler.rb', line 20

def valid_export_args?(args)
  args.size >= 3 && args[0] == "last"
end

#warn_export_usageObject



37
38
39
# File 'lib/lutaml/cli/interactive_shell/export_handler.rb', line 37

def warn_export_usage
  puts OutputFormatter.warning("Usage: export last FORMAT FILE")
end

#warn_no_resultsObject



33
34
35
# File 'lib/lutaml/cli/interactive_shell/export_handler.rb', line 33

def warn_no_results
  puts OutputFormatter.warning("No results to export")
end