Class: Lutaml::Model::CompiledRule
- Inherits:
-
Object
- Object
- Lutaml::Model::CompiledRule
- 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
-
#alias_names ⇒ Array<String>?
readonly
Alias names for multiple mappings.
-
#attribute_name ⇒ Symbol
readonly
The attribute name in the model.
-
#attribute_type ⇒ Symbol, ...
readonly
The attribute type.
-
#child_transformation ⇒ Transformation?
readonly
Pre-compiled transformation for nested models.
-
#collection_info ⇒ Hash?
readonly
Collection metadata (range, etc.).
-
#custom_methods ⇒ Hash
readonly
Custom serialization methods (…, from: …).
-
#namespace_class ⇒ Class?
readonly
Namespace class for XML.
-
#options ⇒ Hash
readonly
Additional rule-specific options.
-
#serialized_name ⇒ String
readonly
The serialized name (element/key name).
-
#value_transformer ⇒ Proc?
readonly
Value transformation lambda.
Instance Method Summary collapse
-
#all_namespaces ⇒ Array<Class>
Collect all namespaces used in this rule and its children.
-
#collection? ⇒ Boolean
Check if this rule represents a collection.
-
#collection_range ⇒ Range?
Get collection range if this is a collection.
-
#has_custom_methods? ⇒ Boolean
Check if this rule has custom serialization methods.
-
#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
constructor
Initialize a new compiled rule.
-
#matches_name?(name) ⇒ Boolean
Check if a name matches this rule (including alias names for multiple mappings).
-
#method_missing(method_name, *args) ⇒ Object
Handle method calls for accessing options dynamically.
-
#multiple_values? ⇒ Boolean
Check if collection allows multiple values.
-
#nested_model? ⇒ Boolean
Check if this rule represents a nested model.
-
#option(key, default = nil) ⇒ Object
Get an option value.
-
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Check if an option or method exists.
-
#transform_value(value, direction = :export) ⇒ Object
Apply value transformation if present.
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
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, ** ) @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 = 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)
188 189 190 191 192 193 194 195 196 |
# File 'lib/lutaml/model/compiled_rule.rb', line 188 def method_missing(method_name, *args, &) # Check if this is an option key if .key?(method_name) return [method_name] end # Fall back to default method_missing behavior super end |
Instance Attribute Details
#alias_names ⇒ Array<String>? (readonly)
Returns Alias names for multiple mappings.
41 42 43 |
# File 'lib/lutaml/model/compiled_rule.rb', line 41 def alias_names @alias_names end |
#attribute_name ⇒ Symbol (readonly)
Returns 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_type ⇒ Symbol, ... (readonly)
Returns The attribute type.
20 21 22 |
# File 'lib/lutaml/model/compiled_rule.rb', line 20 def attribute_type @attribute_type end |
#child_transformation ⇒ Transformation? (readonly)
Returns 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_info ⇒ Hash? (readonly)
Returns Collection metadata (range, etc.).
29 30 31 |
# File 'lib/lutaml/model/compiled_rule.rb', line 29 def collection_info @collection_info end |
#custom_methods ⇒ Hash (readonly)
Returns Custom serialization methods (…, from: …).
38 39 40 |
# File 'lib/lutaml/model/compiled_rule.rb', line 38 def custom_methods @custom_methods end |
#namespace_class ⇒ Class? (readonly)
Returns Namespace class for XML.
32 33 34 |
# File 'lib/lutaml/model/compiled_rule.rb', line 32 def namespace_class @namespace_class end |
#options ⇒ Hash (readonly)
Returns Additional rule-specific options.
35 36 37 |
# File 'lib/lutaml/model/compiled_rule.rb', line 35 def @options end |
#serialized_name ⇒ String (readonly)
Returns 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_transformer ⇒ Proc? (readonly)
Returns 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_namespaces ⇒ Array<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.
119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/lutaml/model/compiled_rule.rb', line 119 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
83 84 85 |
# File 'lib/lutaml/model/compiled_rule.rb', line 83 def collection? !collection_info.nil? end |
#collection_range ⇒ Range?
Get collection range if this is a collection
136 137 138 |
# File 'lib/lutaml/model/compiled_rule.rb', line 136 def collection_range collection_info&.fetch(:range, nil) end |
#has_custom_methods? ⇒ Boolean
Check if this rule has custom serialization 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)
105 106 107 108 109 110 111 |
# File 'lib/lutaml/model/compiled_rule.rb', line 105 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
143 144 145 146 147 148 149 150 |
# File 'lib/lutaml/model/compiled_rule.rb', line 143 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
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
175 176 177 |
# File 'lib/lutaml/model/compiled_rule.rb', line 175 def option(key, default = nil) .fetch(key, default) end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Check if an option or method exists
203 204 205 |
# File 'lib/lutaml/model/compiled_rule.rb', line 203 def respond_to_missing?(method_name, include_private = false) .key?(method_name) || super end |
#transform_value(value, direction = :export) ⇒ Object
Apply value transformation if present
157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/lutaml/model/compiled_rule.rb', line 157 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 |