Module: Hecks::Forms::ValueObjectShape

Defined in:
lib/hecks/forms/value_object_shape.rb

Overview

THE VALUE-OBJECT CLASSIFICATION every consumer of a resolved VO shape needs, spelled once — field_shape.rb (a command's own form), ui_schema.rb (embryonaut_console's table/detail-panel renderer, a separate app, not this repo), and adapters/driven/sql_query_builder.rb (a query's own ORDER BY/ WHERE compiler) each grew their own copy of "is this VO money- shaped," "does it have exactly one attribute, worth unwrapping," and "which member of it is numeric" — the same three questions, answered identically by definition (a VO's shape doesn't change depending on who's asking), so a fix to one copy was never guaranteed to reach the others.

Class Method Summary collapse

Class Method Details

.money?(value_object) ⇒ Boolean

Price{cents:, currency:}/ContractValue{cents:, currency:} — every money-shaped VO in the corpus spells it exactly this way, the two-attribute convention the language itself never enforces but every real chapter follows.

Returns:

  • (Boolean)


21
22
23
# File 'lib/hecks/forms/value_object_shape.rb', line 21

def money?(value_object)
  value_object.attributes.map { |a| a.name.to_s }.sort == %w[cents currency]
end

.numeric_member(value_object) ⇒ Object

The first Integer/Float member, or nil — what a numeric comparison or an ORDER BY compiles against when the VO isn't money-shaped (money's own two members are handled by money? instead, since which one governs ordering is a money-specific decision, not a general "pick the first number" one).



41
42
43
# File 'lib/hecks/forms/value_object_shape.rb', line 41

def numeric_member(value_object)
  value_object.attributes.find { |a| %w[Integer Float].include?(a.type.to_s) }
end

.sole_attribute(value_object) ⇒ Object

A VO with exactly one attribute is a NAME for a scalar, not a genuine group (EmailAddressaddress, CustomerNumbervalue) — [[feedback_name_the_scalar_field]]'s own reasoning, shared here rather than re-decided per caller. Returns the sole attribute, or nil for anything else.



30
31
32
33
34
# File 'lib/hecks/forms/value_object_shape.rb', line 30

def sole_attribute(value_object)
  return nil unless value_object.attributes.size == 1

  value_object.attributes.first
end