Class: Lutaml::Cli::InteractiveShell::QueryCommands

Inherits:
CommandBase
  • Object
show all
Defined in:
lib/lutaml/cli/interactive_shell/query_commands.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_find(args) ⇒ Object



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/query_commands.rb', line 10

def cmd_find(args)
  if args.empty?
    puts OutputFormatter.warning("Usage: find CLASS_NAME")
    return
  end

  query = args.join(" ")
  results = repository.search(query, types: [:class])

  if results[:class].empty?
    puts OutputFormatter.warning("No classes found matching '#{query}'")
  else
    self.last_results = results[:class]

    puts OutputFormatter.colorize(
      "Found #{last_results.size} class(es):", :cyan
    )
    last_results.each_with_index do |qname, i|
      puts "  #{i + 1}. #{qname}"
    end
    puts ""
    puts "Use 'show NUMBER' to view details"
  end
end

#cmd_results(_args) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/lutaml/cli/interactive_shell/query_commands.rb', line 73

def cmd_results(_args)
  if last_results.nil? || last_results.empty?
    puts OutputFormatter.warning("No previous results")
  else
    puts OutputFormatter.colorize(
      "Last results (#{last_results.size}):", :cyan
    )
    last_results.each_with_index do |item, i|
      puts "  #{i + 1}. #{item}"
    end
  end
end

#cmd_search(args) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/lutaml/cli/interactive_shell/query_commands.rb', line 57

def cmd_search(args)
  if args.empty?
    puts OutputFormatter.warning("Usage: search QUERY")
    return
  end

  query = args.join(" ")
  results = repository.search(query)

  if results.values.all?(&:empty?)
    puts OutputFormatter.warning("No results found for '#{query}'")
  else
    display_search_results(results)
  end
end

#cmd_show(args) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/lutaml/cli/interactive_shell/query_commands.rb', line 35

def cmd_show(args)
  if args.empty?
    puts OutputFormatter.warning(
      "Usage: show class QNAME | show package PATH | show NUMBER",
    )
    return
  end

  subcommand = args[0].downcase

  case subcommand
  when "class"
    show_class(args[1..].join(" "))
  when "package"
    show_package(args[1..].join(" "))
  when /^\d+$/
    show_numbered_result(subcommand.to_i)
  else
    show_class(args.join(" "))
  end
end

#display_search_results(results) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/lutaml/cli/interactive_shell/query_commands.rb', line 151

def display_search_results(results)
  results.each do |type, items|
    next if items.empty?

    puts ""
    puts OutputFormatter.colorize(
      "#{type.to_s.capitalize} Results (#{items.size}):", :cyan
    )

    case type
    when :class
      self.last_results = items
      items.each_with_index do |qname, i|
        icon = config[:icons] ? "#{EnhancedFormatter::ICONS[:class]} " : ""
        puts "  #{i + 1}. #{icon}#{qname}"
      end
      puts ""
      puts "Use 'show NUMBER' to view details"
    when :attribute
      items.each do |item|
        puts "  - #{item[:class_name]}::#{item[:attribute_name]} : " \
             "#{item[:type]}"
      end
    when :association
      items.each do |item|
        icon = config[:icons] ? "#{EnhancedFormatter::ICONS[:association]} " : ""
        puts "  #{icon}#{item[:source]}#{item[:target]}"
      end
    end
  end
end

#show_class(qname) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/lutaml/cli/interactive_shell/query_commands.rb', line 86

def show_class(qname)
  cls = repository.find_class(qname)

  unless cls
    puts OutputFormatter.error("Class not found: #{qname}")
    return
  end

  if config[:icons]
    puts EnhancedFormatter.format_class_details_enhanced(cls)
  else
    puts OutputFormatter.colorize("Class: #{qname}", :cyan)
    puts "=" * 50
    puts ""
    puts "Name: #{cls.name}"

    if cls.respond_to?(:attributes) && cls.attributes && !cls.attributes.empty?
      puts ""
      puts OutputFormatter.colorize("Attributes:", :yellow)
      cls.attributes.each do |attr|
        puts "  - #{attr.name}: #{attr.type}"
      end
    end
  end
end

#show_numbered_result(number) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/lutaml/cli/interactive_shell/query_commands.rb', line 136

def show_numbered_result(number)
  if last_results.nil? || last_results.empty?
    puts OutputFormatter.warning("No previous results")
    return
  end

  index = number - 1
  if index.negative? || index >= last_results.size
    puts OutputFormatter.error("Invalid result number: #{number}")
    return
  end

  show_class(last_results[index])
end

#show_package(path) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/lutaml/cli/interactive_shell/query_commands.rb', line 112

def show_package(path)
  nav = shell.instance_variable_get(:@navigation)
  path = nav.resolve_path(path)
  pkg = repository.find_package(path)

  unless pkg
    puts OutputFormatter.error("Package not found: #{path}")
    return
  end

  puts OutputFormatter.colorize("Package: #{path}", :cyan)
  puts "=" * 50
  puts ""
  puts "Name: #{pkg.name}"
  puts ""

  classes = repository.classes_in_package(path)
  puts OutputFormatter.colorize("Classes (#{classes.size}):", :yellow)
  classes.each do |cls|
    icon = config[:icons] ? "#{EnhancedFormatter::ICONS[:class]} " : ""
    puts "  #{icon}#{cls.name}"
  end
end