Class: NtiReceiptBuilder::VariableDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/nti_receipt_builder/variable_definition.rb

Overview

One receipt placeholder: where its value comes from, how it is labelled, how it is formatted, and what stands in for it in the designer preview.

Frozen on construction — these are read on every render and must never be mutated.

Constant Summary collapse

DEFAULT_SAMPLE_ROW_COUNT =
3
DEFAULT_COLLECTION_WIDTH_MM =

Matches the builder's default order_lines element width, so freshly dropped tables fill it.

60.0
DEFAULT_COLUMN_ALIGN =
'left'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key:, method_name:, label:, type: nil, sample: nil, columns: nil) ⇒ VariableDefinition

Returns a new instance of VariableDefinition.



16
17
18
19
20
21
22
23
24
# File 'lib/nti_receipt_builder/variable_definition.rb', line 16

def initialize(key:, method_name:, label:, type: nil, sample: nil, columns: nil)
  @key = -key.to_s
  @method_name = -method_name.to_s
  @label = -label.to_s
  @type = type
  @sample = sample
  @columns = columns
  freeze
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



14
15
16
# File 'lib/nti_receipt_builder/variable_definition.rb', line 14

def columns
  @columns
end

#keyObject (readonly)

Returns the value of attribute key.



14
15
16
# File 'lib/nti_receipt_builder/variable_definition.rb', line 14

def key
  @key
end

#labelObject (readonly)

Returns the value of attribute label.



14
15
16
# File 'lib/nti_receipt_builder/variable_definition.rb', line 14

def label
  @label
end

#method_nameObject (readonly)

Returns the value of attribute method_name.



14
15
16
# File 'lib/nti_receipt_builder/variable_definition.rb', line 14

def method_name
  @method_name
end

#typeObject (readonly)

Returns the value of attribute type.



14
15
16
# File 'lib/nti_receipt_builder/variable_definition.rb', line 14

def type
  @type
end

Instance Method Details

#collection?Boolean

A variable that declares columns: IS the collection — there is no separate marker.

Returns:

  • (Boolean)


27
# File 'lib/nti_receipt_builder/variable_definition.rb', line 27

def collection? = !@columns.nil?

#column_keysObject



29
# File 'lib/nti_receipt_builder/variable_definition.rb', line 29

def column_keys = collection? ? @columns.keys : []

#column_label(column_key) ⇒ Object



31
32
33
# File 'lib/nti_receipt_builder/variable_definition.rb', line 31

def column_label(column_key)
  @columns&.dig(column_key, :label) || column_key.to_s.titleize
end

#column_type(column_key) ⇒ Object



35
36
37
# File 'lib/nti_receipt_builder/variable_definition.rb', line 35

def column_type(column_key)
  @columns&.dig(column_key, :type)
end

#default_columns_configObject

The column set the builder uses when a designer drops a fresh table onto the canvas. Without this the builder would have to hardcode column keys, and any host whose columns differ would get a layout its own validator rejects.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/nti_receipt_builder/variable_definition.rb', line 42

def default_columns_config
  return [] unless collection?

  even_width_mm = (DEFAULT_COLLECTION_WIDTH_MM / column_keys.size).round(2)

  column_keys.map do |column_key|
    {
      'key' => column_key,
      'label' => column_label(column_key),
      'width_mm' => @columns.dig(column_key, :width_mm) || even_width_mm,
      'align' => @columns.dig(column_key, :align) || DEFAULT_COLUMN_ALIGN
    }
  end
end

#sampleObject

The declared sample, or a derived stand-in. Only ever called on the preview path, so nothing is memoised — a frozen object cannot memoise, and previews are not hot.



59
60
61
62
63
64
# File 'lib/nti_receipt_builder/variable_definition.rb', line 59

def sample
  declared = @sample.respond_to?(:call) ? @sample.call : @sample
  return declared unless declared.nil?

  collection? ? derived_collection_sample : label
end