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



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

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



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

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



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

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



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

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



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

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)


22
23
24
# File 'lib/lutaml/cli/interactive_shell/export_handler.rb', line 22

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

#warn_export_usageObject



39
40
41
# File 'lib/lutaml/cli/interactive_shell/export_handler.rb', line 39

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

#warn_no_resultsObject



35
36
37
# File 'lib/lutaml/cli/interactive_shell/export_handler.rb', line 35

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