Class: Lutaml::Cli::Uml::FindCommand

Inherits:
Object
  • Object
show all
Includes:
SharedHelpers
Defined in:
lib/lutaml/cli/uml/find_command.rb

Overview

FindCommand finds elements by criteria

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SharedHelpers

#load_repository, #normalize_path

Constructor Details

#initialize(options = {}) ⇒ FindCommand

Returns a new instance of FindCommand.



15
16
17
# File 'lib/lutaml/cli/uml/find_command.rb', line 15

def initialize(options = {})
  @options = options.transform_keys(&:to_sym)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/lutaml/cli/uml/find_command.rb', line 13

def options
  @options
end

Class Method Details

.add_options_to(thor_class, _method_name) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/lutaml/cli/uml/find_command.rb', line 19

def self.add_options_to(thor_class, _method_name)
  thor_class.long_desc <<-DESC
  Find elements matching specific criteria.

  Examples:
    lutaml uml find model.lur --stereotype interface
    lutaml uml find model.lur --package ModelRoot::Core
    lutaml uml find model.lur --pattern "^Building.*"
  DESC

  thor_class.option :stereotype, type: :string,
                                 desc: "Filter by stereotype"
  thor_class.option :package, type: :string, desc: "Filter by package"
  thor_class.option :pattern, type: :string, desc: "Match name pattern"
  thor_class.option :format, type: :string, default: "text",
                             desc: "Output format (text|yaml|json)"
  thor_class.option :lazy, type: :boolean, default: false,
                           desc: "Use lazy loading"
end

Instance Method Details

#run(lur_path) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/lutaml/cli/uml/find_command.rb', line 39

def run(lur_path) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
  validate_options!

  # Load the repository from the LUR file
  repo = load_repository(lur_path, lazy: options[:lazy])

  # Get results based on the filter type
  results = if options[:pattern] # e.g. "^Building.*"
              types = [options[:type] || :class]
              repo.search(options[:pattern], types: types)
            elsif options[:stereotype]
              repo.find_classes_by_stereotype(options[:stereotype])
            elsif options[:package]
              repo.classes_in_package(options[:package],
                                      recursive: false)
            else
              []
            end

  # Extract classes from the nested structure
  classes = case results
            when Array
              if results.length == 2 && results[0] == :classes
                results[1]  # Extract the actual classes array
              else
                results     # Assume it's already an array of classes
              end
            when NilClass
              []
            else
              [results]     # Wrap single result in array
            end

  if classes.nil? || classes.empty?
    filter_desc = if options[:pattern]
                    "pattern: #{options[:pattern]}"
                  elsif options[:stereotype]
                    "stereotype: #{options[:stereotype]}"
                  elsif options[:package]
                    "package: #{options[:package]}"
                  else
                    "criteria"
                  end
    puts "No elements found matching #{filter_desc}"
    return
  end

  output = classes.map do |cls|
    cls.respond_to?(:name) ? (cls.name || cls.to_s) : cls.to_s
  end

  # output result based on the format option
  case options[:format]
  when "json"
    puts output.to_json
  when "yaml"
    puts output.to_yaml
  else
    puts output.join("\n")
  end
rescue Thor::Error
  raise
rescue ArgumentError => e
  raise Thor::Error, e.message
rescue StandardError => e
  raise Thor::Error, "Find command failed: #{e.message}"
end