Class: NtiReceiptBuilder::Variables

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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_objectObject (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_definitionObject



46
# File 'lib/nti_receipt_builder/variables.rb', line 46

def collection_definition = collection_key && definitions[collection_key]

.collection_keyObject



44
# File 'lib/nti_receipt_builder/variables.rb', line 44

def collection_key = schema.collection_key

.definitionsObject



41
# File 'lib/nti_receipt_builder/variables.rb', line 41

def definitions = schema.definitions

.keysObject



45
# File 'lib/nti_receipt_builder/variables.rb', line 45

def keys = definitions.keys

.labelsObject



42
# File 'lib/nti_receipt_builder/variables.rb', line 42

def labels = schema.labels

.overridesObject



52
53
54
# File 'lib/nti_receipt_builder/variables.rb', line 52

def overrides
  @overrides ||= {}
end

.sample_dataObject



48
49
50
# File 'lib/nti_receipt_builder/variables.rb', line 48

def sample_data
  definitions.transform_values(&:sample)
end

.scalar_keysObject



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

#resolveObject

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