Class: NtiReceiptBuilder::Variables
- Inherits:
-
Object
- Object
- NtiReceiptBuilder::Variables
- Defined in:
- lib/nti_receipt_builder/variables.rb
Overview
Base class for a host application's receipt variable vocabulary.
class OrderReceiptVariables < NtiReceiptBuilder::Variables
MAPPINGS = {
'ORDER_REFERENCE' => 'order_reference',
'STORE_NAME' => 'store_name'
}.freeze
def order_reference = receipt_object.reference
def store_name = receipt_object.address.name
end
MAPPINGS supplies the placeholder keys and the method producing each value. Everything
else — label, formatting type, preview sample — is derived, and overridden per key with
variable. The key that declares columns: is the collection.
The schema is built once on first read and frozen, so render-path reads take no lock. It is memoised on the subclass itself, so a host code reload discards it along with the class it describes — there is no separate cache to invalidate and nothing that outlives what it points at.
Instance Attribute Summary collapse
-
#receipt_object ⇒ Object
readonly
Returns the value of attribute receipt_object.
Class Method Summary collapse
- .collection_definition ⇒ Object
- .collection_key ⇒ Object
- .definitions ⇒ Object
- .keys ⇒ Object
- .labels ⇒ Object
- .overrides ⇒ Object
- .sample_data ⇒ Object
- .scalar_keys ⇒ Object
-
.variable(key, label: nil, type: nil, sample: nil, columns: nil) ⇒ Object
Override any derived aspect of a key already present in MAPPINGS.
Instance Method Summary collapse
-
#initialize(receipt_object) ⇒ Variables
constructor
A new instance of Variables.
-
#resolve ⇒ Object
Raw values, unformatted — VariableResolver applies formatting.
Constructor Details
#initialize(receipt_object) ⇒ Variables
Returns a new instance of Variables.
146 147 148 |
# File 'lib/nti_receipt_builder/variables.rb', line 146 def initialize(receipt_object) @receipt_object = receipt_object end |
Instance Attribute Details
#receipt_object ⇒ Object (readonly)
Returns the value of attribute receipt_object.
144 145 146 |
# File 'lib/nti_receipt_builder/variables.rb', line 144 def receipt_object @receipt_object end |
Class Method Details
.collection_definition ⇒ Object
46 |
# File 'lib/nti_receipt_builder/variables.rb', line 46 def collection_definition = collection_key && definitions[collection_key] |
.collection_key ⇒ Object
44 |
# File 'lib/nti_receipt_builder/variables.rb', line 44 def collection_key = schema.collection_key |
.definitions ⇒ Object
41 |
# File 'lib/nti_receipt_builder/variables.rb', line 41 def definitions = schema.definitions |
.keys ⇒ Object
45 |
# File 'lib/nti_receipt_builder/variables.rb', line 45 def keys = definitions.keys |
.labels ⇒ Object
42 |
# File 'lib/nti_receipt_builder/variables.rb', line 42 def labels = schema.labels |
.overrides ⇒ Object
52 53 54 |
# File 'lib/nti_receipt_builder/variables.rb', line 52 def overrides @overrides ||= {} end |
.sample_data ⇒ Object
48 49 50 |
# File 'lib/nti_receipt_builder/variables.rb', line 48 def sample_data definitions.transform_values(&:sample) end |
.scalar_keys ⇒ Object
43 |
# File 'lib/nti_receipt_builder/variables.rb', line 43 def scalar_keys = schema.scalar_keys |
.variable(key, label: nil, type: nil, sample: nil, columns: nil) ⇒ Object
Override any derived aspect of a key already present in MAPPINGS.
variable 'ORDER_DATETIME', label: 'Order Date/Time', sample: -> { Time.current }
variable 'ORDER_LINES', columns: { 'description' => { label: 'Item' } }
Column options: label:, type:, width_mm:, align: — all optional.
35 36 37 38 39 |
# File 'lib/nti_receipt_builder/variables.rb', line 35 def variable(key, label: nil, type: nil, sample: nil, columns: nil) overrides[key.to_s] = { label: label, type: type, sample: sample, columns: columns } @schema = nil self end |
Instance Method Details
#resolve ⇒ Object
Raw values, unformatted — VariableResolver applies formatting. An error raised by a mapping method propagates deliberately: a receipt printing silently without its total is worse than a visible failure.
153 154 155 156 157 |
# File 'lib/nti_receipt_builder/variables.rb', line 153 def resolve self.class.definitions.each_with_object({}) do |(key, definition), acc| acc[key] = send(definition.method_name) end end |