Class: Unitsdb::Commands::Get

Inherits:
Base
  • Object
show all
Defined in:
lib/unitsdb/commands/get.rb

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Unitsdb::Commands::Base

Instance Method Details

#get(id) ⇒ 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
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/unitsdb/commands/get.rb', line 10

def get(id)
  # Database path is guaranteed by Thor's global option
  id_type = @options[:id_type]
  format = @options[:format] || "text"

  begin
    database = load_database(@options[:database])

    # Search by ID
    entity = database.get_by_id(id: id, type: id_type)

    unless entity
      puts "No entity found with ID: '#{id}'"
      return
    end

    # Output based on format
    if %w[json yaml].include?(format.downcase)
      begin
        puts entity.send("to_#{format.downcase}")
        return
      rescue NoMethodError
        puts "Error: Unable to convert entity to #{format} format"
        exit(1)
      end
    end

    # Default text output
    print_entity_details(entity)
  rescue Unitsdb::Errors::DatabaseError => e
    puts "Error: #{e.message}"
    exit(1)
  rescue StandardError => e
    puts "Error searching database: #{e.message}"
    exit(1)
  end
end