Class: Lutaml::Model::Collection

Inherits:
Serializable show all
Includes:
Enumerable
Defined in:
lib/lutaml/model/collection.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes included from Serialize

#element_order, #encoding, #mixed, #ordered, #schema_location

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Serialize

#attr_value, #attribute_exist?, #extract_register_id, included, #key_exist?, #key_value, #method_missing, #mixed?, #ordered?, #pretty_print_instance_variables, register_format_mapping_method, register_from_format_method, register_to_format_method, #respond_to_missing?, #to_yaml_hash, #using_default?, #using_default_for, #validate_attribute!, #value_map, #value_set_for

Methods included from Liquefiable

included, #to_liquid

Methods included from Validation

#validate, #validate!, #validate_helper

Methods included from ComparableModel

#already_compared?, #attributes_hash, #calculate_hash, #comparison_key, #eql?, #hash, included, #same_class?

Constructor Details

#initialize(items = [], __register: Lutaml::Model::Config.default_register) ⇒ Collection

Returns a new instance of Collection.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/lutaml/model/collection.rb', line 92

def initialize(items = [], __register: Lutaml::Model::Config.default_register)
  super()

  @__register = __register
  items = [items].compact unless items.is_a?(Array)

  register_object = Lutaml::Model::GlobalRegister.lookup(@__register)
  type = register_object.get_class_without_register(self.class.instance_type)
  self.collection = items.map do |item|
    if item.is_a?(type)
      item
    elsif type <= Lutaml::Model::Type::Value
      type.cast(item)
    else
      item[:__register] = __register
      type.new(item)
    end
  end

  sort_items!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Lutaml::Model::Serialize

Class Attribute Details

.instance_nameObject (readonly)

Returns the value of attribute instance_name.



7
8
9
# File 'lib/lutaml/model/collection.rb', line 7

def instance_name
  @instance_name
end

.instance_typeObject (readonly)

Returns the value of attribute instance_type.



7
8
9
# File 'lib/lutaml/model/collection.rb', line 7

def instance_type
  @instance_type
end

.order_by_fieldObject (readonly)

Returns the value of attribute order_by_field.



7
8
9
# File 'lib/lutaml/model/collection.rb', line 7

def order_by_field
  @order_by_field
end

.order_directionObject (readonly)

Returns the value of attribute order_direction.



7
8
9
# File 'lib/lutaml/model/collection.rb', line 7

def order_direction
  @order_direction
end

Instance Attribute Details

#__registerObject (readonly)

Returns the value of attribute __register.



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

def __register
  @__register
end

Class Method Details

.apply_mappings(data, format, options = {}) ⇒ Object



85
86
87
# File 'lib/lutaml/model/collection.rb', line 85

def apply_mappings(data, format, options = {})
  super(data, format, options.merge(collection: true))
end

.as(format, instance, options = {}) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/lutaml/model/collection.rb', line 54

def as(format, instance, options = {})
  mappings = mappings_for(format)
  data = super

  if mappings.no_root? && format != :xml && !mappings.root_mapping
    Utils.fetch_str_or_sym(data, instance_name)
  else
    data
  end
end

.from(format, data, options = {}) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/lutaml/model/collection.rb', line 65

def from(format, data, options = {})
  mappings = mappings_for(format)
  if mappings.no_root? && format == :xml
    tag_name = mappings.find_by_to(instance_name).name
    data = "<#{tag_name}>#{data}</#{tag_name}>"
  end

  super(format, data, options.merge(from_collection: true))
end

.instances(name, type, &block) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/lutaml/model/collection.rb', line 12

def instances(name, type, &block)
  attribute(name, type, collection: true, validations: block)

  @instance_type = Lutaml::Model::Attribute.cast_type!(type)
  @instance_name = name

  define_method(:"#{name}=") do |collection|
    self.collection = collection
  end
end

.of(format, data, options = {}) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/lutaml/model/collection.rb', line 75

def of(format, data, options = {})
  mappings = mappings_for(format)

  if mappings.no_root? && format != :xml && !mappings.root_mapping
    data = { mappings.find_by_to(instance_name).name => data }
  end

  super(format, data, options.merge(from_collection: true))
end

.ordered(by:, order: :asc) ⇒ Object



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

def ordered(by:, order: :asc)
  @order_by_field = by.to_sym
  @order_direction = order
end

.serialize_for_mapping(mapping, instance, format, options) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/lutaml/model/collection.rb', line 44

def serialize_for_mapping(mapping, instance, format, options)
  options[:tag_name] = mapping.name

  attr_value = instance.public_send(mapping.to)
  return if attr_value.nil? || attr_value.empty?

  attr_value = [attr_value] unless attr_value.is_a?(Array)
  attr_value.map { |v| v.public_send(:"to_#{format}", options) }
end

.sort_configured?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/lutaml/model/collection.rb', line 28

def sort_configured?
  !!@order_by_field
end

.to(format, instance, options = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/lutaml/model/collection.rb', line 32

def to(format, instance, options = {})
  mappings = mappings_for(format)

  if mappings.no_root? && format == :xml
    mappings.mappings.map do |mapping|
      serialize_for_mapping(mapping, instance, format, options)
    end.join("\n")
  else
    super(format, instance, options.merge(collection: true))
  end
end

Instance Method Details

#<<(item) ⇒ Object



155
156
157
# File 'lib/lutaml/model/collection.rb', line 155

def <<(item)
  push(item)
end

#[](index) ⇒ Object



164
165
166
# File 'lib/lutaml/model/collection.rb', line 164

def [](index)
  collection[index]
end

#[]=(index, value) ⇒ Object



168
169
170
171
# File 'lib/lutaml/model/collection.rb', line 168

def []=(index, value)
  collection[index] = value
  sort_items!
end

#collectionObject



118
119
120
# File 'lib/lutaml/model/collection.rb', line 118

def collection
  instance_variable_get(:"@#{self.class.instance_name}")
end

#collection=(collection) ⇒ Object



122
123
124
125
# File 'lib/lutaml/model/collection.rb', line 122

def collection=(collection)
  instance_variable_set(:"@#{self.class.instance_name}", collection)
  sort_items!
end

#difference(other) ⇒ Object



135
136
137
# File 'lib/lutaml/model/collection.rb', line 135

def difference(other)
  self.class.new(items - other.items)
end

#each(&block) ⇒ Object



139
140
141
# File 'lib/lutaml/model/collection.rb', line 139

def each(&block)
  collection.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


173
174
175
# File 'lib/lutaml/model/collection.rb', line 173

def empty?
  collection&.empty?
end

#firstObject



147
148
149
# File 'lib/lutaml/model/collection.rb', line 147

def first
  collection.first
end

#intersection(other) ⇒ Object



131
132
133
# File 'lib/lutaml/model/collection.rb', line 131

def intersection(other)
  self.class.new(items & other.items)
end

#lastObject



151
152
153
# File 'lib/lutaml/model/collection.rb', line 151

def last
  collection.last
end

#order_defined?Boolean

Returns:

  • (Boolean)


177
178
179
# File 'lib/lutaml/model/collection.rb', line 177

def order_defined?
  self.class.sort_configured?
end

#push(item) ⇒ Object



159
160
161
162
# File 'lib/lutaml/model/collection.rb', line 159

def push(item)
  collection.push(item)
  sort_items!
end

#sizeObject



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

def size
  collection.size
end

#sort_items!Object



181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/lutaml/model/collection.rb', line 181

def sort_items!
  return if collection.nil?
  return unless order_defined?

  unless collection&.one?
    field = self.class.order_by_field
    direction = self.class.order_direction

    collection.sort_by! { |item| item.send(field) }
    collection.reverse! if direction == :desc
  end
end

#to_format(format, options = {}) ⇒ Object



114
115
116
# File 'lib/lutaml/model/collection.rb', line 114

def to_format(format, options = {})
  super(format, options.merge(collection: true))
end

#union(other) ⇒ Object



127
128
129
# File 'lib/lutaml/model/collection.rb', line 127

def union(other)
  self.class.new((items + other.items).uniq)
end