Module: Lutaml::Model::CollectionHandler
- Included in:
- Attribute
- Defined in:
- lib/lutaml/model/collection_handler.rb
Overview
Module for handling collection-related operations on attributes.
Provides methods for checking if an attribute is a collection, getting the collection class, building collections, and related operations.
Instance Method Summary collapse
-
#build_collection(*args) ⇒ Object
Build a new collection with the given values.
-
#collection ⇒ Object?
Get the collection options.
-
#collection? ⇒ Boolean
Check if this attribute is a collection Performance: Memoized to avoid repeated hash lookups.
-
#collection_class ⇒ Class
Get the collection class to use for this attribute Performance: Memoized.
-
#collection_instance?(value) ⇒ Boolean
Check if value is an instance of the collection class.
-
#custom_collection? ⇒ Boolean
Check if this attribute uses a custom collection class Performance: Memoized.
-
#min_collection_zero? ⇒ Boolean
Check if the collection minimum is zero.
-
#resolved_collection ⇒ Range?
Get the resolved collection range Performance: Memoized.
-
#singular? ⇒ Boolean
Check if this attribute is a singular (non-collection) value Performance: Memoized.
Instance Method Details
#build_collection(*args) ⇒ Object
Build a new collection with the given values
53 54 55 |
# File 'lib/lutaml/model/collection_handler.rb', line 53 def build_collection(*args) collection_class.new(args.flatten) end |
#collection ⇒ Object?
Get the collection options
13 14 15 |
# File 'lib/lutaml/model/collection_handler.rb', line 13 def collection @options[:collection] end |
#collection? ⇒ Boolean
Check if this attribute is a collection Performance: Memoized to avoid repeated hash lookups
21 22 23 |
# File 'lib/lutaml/model/collection_handler.rb', line 21 def collection? @collection ||= collection || false end |
#collection_class ⇒ Class
Get the collection class to use for this attribute Performance: Memoized
37 38 39 |
# File 'lib/lutaml/model/collection_handler.rb', line 37 def collection_class @collection_class ||= custom_collection? ? collection : Array end |
#collection_instance?(value) ⇒ Boolean
Check if value is an instance of the collection class
45 46 47 |
# File 'lib/lutaml/model/collection_handler.rb', line 45 def collection_instance?(value) value.is_a?(collection_class) end |
#custom_collection? ⇒ Boolean
Check if this attribute uses a custom collection class Performance: Memoized
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/lutaml/model/collection_handler.rb', line 61 def custom_collection? return @custom_collection if defined?(@custom_collection) @custom_collection = if singular? false elsif collection == true false elsif collection.is_a?(Range) false else collection <= Lutaml::Model::Collection end end |
#min_collection_zero? ⇒ Boolean
Check if the collection minimum is zero
90 91 92 |
# File 'lib/lutaml/model/collection_handler.rb', line 90 def min_collection_zero? collection? && resolved_collection.min.zero? end |
#resolved_collection ⇒ Range?
Get the resolved collection range Performance: Memoized
79 80 81 82 83 84 85 |
# File 'lib/lutaml/model/collection_handler.rb', line 79 def resolved_collection @resolved_collection ||= begin return unless collection? collection.is_a?(Range) ? validated_range_object : 0..Float::INFINITY end end |
#singular? ⇒ Boolean
Check if this attribute is a singular (non-collection) value Performance: Memoized
29 30 31 |
# File 'lib/lutaml/model/collection_handler.rb', line 29 def singular? @singular ||= !collection? end |