Class: Lutaml::Model::CompiledRule

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/model/compiled_rule.rb

Overview

Pre-compiled transformation rule for efficient serialization.

A CompiledRule contains all information needed to transform a model attribute into its serialized representation without triggering type resolution or imports during transformation.

CompiledRules are frozen after creation to ensure immutability.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute_name:, serialized_name:, attribute_type: nil, child_transformation: nil, value_transformer: nil, collection_info: nil, namespace_class: nil, custom_methods: nil, alias_names: nil, **options) ⇒ CompiledRule

Initialize a new compiled rule

Parameters:

  • attribute_name (Symbol)

    The attribute name in the model

  • serialized_name (String)

    The serialized name

  • attribute_type (Symbol, Class, nil) (defaults to: nil)

    The attribute type

  • child_transformation (Transformation, nil) (defaults to: nil)

    Pre-compiled child transformation

  • value_transformer (Proc, nil) (defaults to: nil)

    Value transformation lambda

  • collection_info (Hash, nil) (defaults to: nil)

    Collection metadata

  • namespace_class (Class, nil) (defaults to: nil)

    Namespace class for XML

  • custom_methods (Hash) (defaults to: nil)

    Custom serialization methods (..., from: ...)

  • alias_names (Array<String>, nil) (defaults to: nil)

    Alias names for multiple mappings

  • options (Hash)

    Additional options



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/lutaml/model/compiled_rule.rb', line 55

def initialize(
  attribute_name:,
  serialized_name:,
  attribute_type: nil,
  child_transformation: nil,
  value_transformer: nil,
  collection_info: nil,
  namespace_class: nil,
  custom_methods: nil,
  alias_names: nil,
  **options
)
  @attribute_name = attribute_name
  @serialized_name = serialized_name
  @attribute_type = attribute_type
  @child_transformation = child_transformation
  @value_transformer = value_transformer
  @collection_info = collection_info
  @namespace_class = namespace_class
  @custom_methods = custom_methods || {}
  @alias_names = alias_names
  @options = options
  freeze
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object

Handle method calls for accessing options dynamically

This allows options to be accessed as methods (e.g., rule.cdata, rule.raw, rule.mixed_content) instead of using rule.option(:cdata)

Parameters:

  • method_name (Symbol)

    The method name to look up

  • args (Array)

    Arguments (ignored for options)

  • block (Proc)

    Block (ignored for options)

Returns:

  • (Object)

    The option value or nil



195
196
197
198
199
200
201
202
203
# File 'lib/lutaml/model/compiled_rule.rb', line 195

def method_missing(method_name, *args, &)
  # Check if this is an option key
  if options.key?(method_name)
    return options[method_name]
  end

  # Fall back to default method_missing behavior
  super
end

Instance Attribute Details

#alias_namesArray<String>? (readonly)

Returns Alias names for multiple mappings.

Returns:

  • (Array<String>, nil)

    Alias names for multiple mappings



41
42
43
# File 'lib/lutaml/model/compiled_rule.rb', line 41

def alias_names
  @alias_names
end

#attribute_nameSymbol (readonly)

Returns The attribute name in the model.

Returns:

  • (Symbol)

    The attribute name in the model



14
15
16
# File 'lib/lutaml/model/compiled_rule.rb', line 14

def attribute_name
  @attribute_name
end

#attribute_typeSymbol, ... (readonly)

Returns The attribute type.

Returns:

  • (Symbol, Class, nil)

    The attribute type



20
21
22
# File 'lib/lutaml/model/compiled_rule.rb', line 20

def attribute_type
  @attribute_type
end

#child_transformationTransformation? (readonly)

Returns Pre-compiled transformation for nested models.

Returns:

  • (Transformation, nil)

    Pre-compiled transformation for nested models



23
24
25
# File 'lib/lutaml/model/compiled_rule.rb', line 23

def child_transformation
  @child_transformation
end

#collection_infoHash? (readonly)

Returns Collection metadata (range, etc.).

Returns:

  • (Hash, nil)

    Collection metadata (range, etc.)



29
30
31
# File 'lib/lutaml/model/compiled_rule.rb', line 29

def collection_info
  @collection_info
end

#custom_methodsHash (readonly)

Returns Custom serialization methods (..., from: ...).

Returns:

  • (Hash)

    Custom serialization methods (..., from: ...)



38
39
40
# File 'lib/lutaml/model/compiled_rule.rb', line 38

def custom_methods
  @custom_methods
end

#namespace_classClass? (readonly)

Returns Namespace class for XML.

Returns:

  • (Class, nil)

    Namespace class for XML



32
33
34
# File 'lib/lutaml/model/compiled_rule.rb', line 32

def namespace_class
  @namespace_class
end

#optionsHash (readonly)

Returns Additional rule-specific options.

Returns:

  • (Hash)

    Additional rule-specific options



35
36
37
# File 'lib/lutaml/model/compiled_rule.rb', line 35

def options
  @options
end

#serialized_nameString (readonly)

Returns The serialized name (element/key name).

Returns:

  • (String)

    The serialized name (element/key name)



17
18
19
# File 'lib/lutaml/model/compiled_rule.rb', line 17

def serialized_name
  @serialized_name
end

#value_transformerProc? (readonly)

Returns Value transformation lambda.

Returns:

  • (Proc, nil)

    Value transformation lambda



26
27
28
# File 'lib/lutaml/model/compiled_rule.rb', line 26

def value_transformer
  @value_transformer
end

Instance Method Details

#all_namespacesArray<Class>

Collect all namespaces used in this rule and its children

This method recursively traverses child transformations to collect all namespace classes without triggering type resolution.

Returns:

  • (Array<Class>)

    Array of XmlNamespace classes



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/lutaml/model/compiled_rule.rb', line 126

def all_namespaces
  namespaces = []

  # Add this rule's namespace if present
  namespaces << namespace_class if namespace_class

  # Recursively collect from child transformation
  if child_transformation
    namespaces.concat(child_transformation.all_namespaces)
  end

  namespaces.uniq
end

#collection?Boolean

Check if this rule represents a collection

Returns:

  • (Boolean)

    true if attribute is a collection



83
84
85
# File 'lib/lutaml/model/compiled_rule.rb', line 83

def collection?
  !collection_info.nil?
end

#collection_rangeRange?

Get collection range if this is a collection

Returns:

  • (Range, nil)

    Collection range or nil



143
144
145
# File 'lib/lutaml/model/compiled_rule.rb', line 143

def collection_range
  collection_info&.fetch(:range, nil)
end

#form_set?Boolean

Check if form is explicitly set

Returns:

  • (Boolean)

    true if form option was provided



104
105
106
# File 'lib/lutaml/model/compiled_rule.rb', line 104

def form_set?
  !form.nil?
end

#has_custom_methods?Boolean

Check if this rule has custom serialization methods

Returns:

  • (Boolean)

    true if has custom methods



97
98
99
# File 'lib/lutaml/model/compiled_rule.rb', line 97

def has_custom_methods?
  !custom_methods.empty?
end

#matches_name?(name) ⇒ Boolean

Check if a name matches this rule (including alias names for multiple mappings)

Parameters:

  • name (String, Symbol)

    The name to check

Returns:

  • (Boolean)

    true if the name matches serialized_name or any alias



112
113
114
115
116
117
118
# File 'lib/lutaml/model/compiled_rule.rb', line 112

def matches_name?(name)
  name_str = name.to_s
  return true if serialized_name == name_str

  # Check alias names for multiple mappings
  alias_names&.any? { |alias_name| alias_name.to_s == name_str } || false
end

#multiple_values?Boolean

Check if collection allows multiple values

Returns:

  • (Boolean)

    true if collection allows more than one value



150
151
152
153
154
155
156
157
# File 'lib/lutaml/model/compiled_rule.rb', line 150

def multiple_values?
  return false unless collection?

  range = collection_range
  return true if range.nil? # unbounded collection

  range.end.nil? || range.end > 1
end

#nested_model?Boolean

Check if this rule represents a nested model

Returns:

  • (Boolean)

    true if attribute is a nested model



90
91
92
# File 'lib/lutaml/model/compiled_rule.rb', line 90

def nested_model?
  !child_transformation.nil?
end

#option(key, default = nil) ⇒ Object

Get an option value

Parameters:

  • key (Symbol)

    The option key

  • default (Object) (defaults to: nil)

    Default value if not found

Returns:

  • (Object)

    The option value or default



182
183
184
# File 'lib/lutaml/model/compiled_rule.rb', line 182

def option(key, default = nil)
  options.fetch(key, default)
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Check if an option or method exists

Parameters:

  • method_name (Symbol)

    The method name to check

  • include_private (Boolean) (defaults to: false)

    Whether to include private methods

Returns:

  • (Boolean)

    true if the method exists



210
211
212
# File 'lib/lutaml/model/compiled_rule.rb', line 210

def respond_to_missing?(method_name, include_private = false)
  options.key?(method_name) || super
end

#transform_value(value, direction = :export) ⇒ Object

Apply value transformation if present

Parameters:

  • value (Object)

    The value to transform

  • direction (Symbol) (defaults to: :export)

    :export or :import

Returns:

  • (Object)

    Transformed value



164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/lutaml/model/compiled_rule.rb', line 164

def transform_value(value, direction = :export)
  return value unless value_transformer

  if value_transformer.is_a?(::Hash)
    transformer = value_transformer[direction]
    transformer ? transformer.call(value) : value
  elsif value_transformer.is_a?(Proc)
    value_transformer.call(value)
  else
    value
  end
end