Class: Unitsdb::Commands::EntityPresenter

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

Overview

Present a single entity as text for the Get/Search CLI commands. Pulls every displayable field off the typed model — no respond_to? feature detection. New entity types extend TYPE_NAME and extras; nothing else changes.

Constant Summary collapse

TYPE_NAME =
{
  Unitsdb::Unit => "Unit",
  Unitsdb::Prefix => "Prefix",
  Unitsdb::Quantity => "Quantity",
  Unitsdb::Dimension => "Dimension",
  Unitsdb::UnitSystem => "UnitSystem",
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(entity) ⇒ EntityPresenter

Returns a new instance of EntityPresenter.



18
19
20
# File 'lib/unitsdb/commands/entity_presenter.rb', line 18

def initialize(entity)
  @entity = entity
end

Instance Method Details

#display_nameObject

Best-effort display name: first localized name value → short → "N/A".



28
29
30
31
32
33
34
# File 'lib/unitsdb/commands/entity_presenter.rb', line 28

def display_name
  name = @entity.names.first
  return name.value.to_s if name&.value
  return @entity.short.to_s if @entity.short

  "N/A"
end

Multi-line "details" output for unitsdb get ID.



37
38
39
40
41
42
43
44
45
# File 'lib/unitsdb/commands/entity_presenter.rb', line 37

def print_details
  puts "Entity details:"
  puts "  - Type: #{type_name}"
  puts "  - Name: #{display_name}"
  puts "  - Description: #{@entity.short}" if show_short?
  print_identifiers(header: "  - Identifiers:", item_indent: "      ")
  print_extras
  print_references
end

Single-block summary used by unitsdb search per result.



48
49
50
51
52
53
# File 'lib/unitsdb/commands/entity_presenter.rb', line 48

def print_summary
  puts "  - #{type_name}: #{display_name}"
  print_identifiers(header: "    IDs:", item_indent: "      ")
  puts "    Description: #{@entity.short}" if show_short?
  puts ""
end

#type_nameObject

Human-readable type label, e.g. "Unit", "Prefix".



23
24
25
# File 'lib/unitsdb/commands/entity_presenter.rb', line 23

def type_name
  TYPE_NAME[@entity.class] || @entity.class.name.split("::").last
end