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

Instance Method Details

#build_collection(*args) ⇒ Object

Build a new collection with the given values

Parameters:

  • args (Array)

    Values to include in the collection

Returns:

  • (Object)

    A new collection instance



53
54
55
# File 'lib/lutaml/model/collection_handler.rb', line 53

def build_collection(*args)
  collection_class.new(args.flatten)
end

#collectionObject?

Get the collection options

Returns:

  • (Object, nil)

    The collection option value



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

Returns:

  • (Boolean)

    true if attribute is a collection



21
22
23
# File 'lib/lutaml/model/collection_handler.rb', line 21

def collection?
  @collection ||= collection || false
end

#collection_classClass

Get the collection class to use for this attribute Performance: Memoized

Returns:

  • (Class)

    The collection class (Array or custom collection class)



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

Parameters:

  • value (Object)

    The value to check

Returns:

  • (Boolean)

    true if value is a collection instance



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

Returns:

  • (Boolean)

    true if using a custom collection class



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

Returns:

  • (Boolean)

    true if collection min 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_collectionRange?

Get the resolved collection range Performance: Memoized

Returns:

  • (Range, nil)

    The collection range or nil if not a collection



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

Returns:

  • (Boolean)

    true if attribute is not a collection



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

def singular?
  @singular ||= !collection?
end