Class: Unitsdb::Commands::EntityPresenter
- Inherits:
-
Object
- Object
- Unitsdb::Commands::EntityPresenter
- 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
-
#display_name ⇒ Object
Best-effort display name: first localized name value → short → "N/A".
-
#initialize(entity) ⇒ EntityPresenter
constructor
A new instance of EntityPresenter.
-
#print_details ⇒ Object
Multi-line "details" output for
unitsdb get ID. -
#print_summary ⇒ Object
Single-block summary used by
unitsdb searchper result. -
#type_name ⇒ Object
Human-readable type label, e.g.
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_name ⇒ Object
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 |
#print_details ⇒ Object
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 |
#print_summary ⇒ Object
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_name ⇒ Object
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 |