Class: Lutaml::Model::Collection
Class Attribute Summary collapse
Instance Attribute Summary collapse
Attributes included from Serialize
#element_order, #encoding, #mixed, #ordered, #schema_location
Class Method Summary
collapse
-
.apply_mappings(data, format, options = {}) ⇒ Object
-
.as(format, instance, options = {}) ⇒ Object
-
.from(format, data, options = {}) ⇒ Object
-
.instances(name, type, &block) ⇒ Object
-
.of(format, data, options = {}) ⇒ Object
-
.ordered(by:, order: :asc) ⇒ Object
-
.serialize_for_mapping(mapping, instance, format, options) ⇒ Object
-
.sort_configured? ⇒ Boolean
-
.to(format, instance, options = {}) ⇒ Object
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
included, #to_liquid
Methods included from Validation
#validate, #validate!, #validate_helper
#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
|
# 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
type.new(item, register: register)
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_name ⇒ Object
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_type ⇒ Object
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_field ⇒ Object
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_direction ⇒ Object
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
#register ⇒ Object
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
|
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
154
155
156
|
# File 'lib/lutaml/model/collection.rb', line 154
def <<(item)
push(item)
end
|
#[](index) ⇒ Object
163
164
165
|
# File 'lib/lutaml/model/collection.rb', line 163
def [](index)
collection[index]
end
|
#[]=(index, value) ⇒ Object
167
168
169
170
|
# File 'lib/lutaml/model/collection.rb', line 167
def []=(index, value)
collection[index] = value
sort_items!
end
|
#collection ⇒ Object
117
118
119
|
# File 'lib/lutaml/model/collection.rb', line 117
def collection
instance_variable_get(:"@#{self.class.instance_name}")
end
|
#collection=(collection) ⇒ Object
121
122
123
124
|
# File 'lib/lutaml/model/collection.rb', line 121
def collection=(collection)
instance_variable_set(:"@#{self.class.instance_name}", collection)
sort_items!
end
|
#difference(other) ⇒ Object
134
135
136
|
# File 'lib/lutaml/model/collection.rb', line 134
def difference(other)
self.class.new(items - other.items)
end
|
#each(&block) ⇒ Object
138
139
140
|
# File 'lib/lutaml/model/collection.rb', line 138
def each(&block)
collection.each(&block)
end
|
#empty? ⇒ Boolean
172
173
174
|
# File 'lib/lutaml/model/collection.rb', line 172
def empty?
collection&.empty?
end
|
#first ⇒ Object
146
147
148
|
# File 'lib/lutaml/model/collection.rb', line 146
def first
collection.first
end
|
#intersection(other) ⇒ Object
130
131
132
|
# File 'lib/lutaml/model/collection.rb', line 130
def intersection(other)
self.class.new(items & other.items)
end
|
#last ⇒ Object
150
151
152
|
# File 'lib/lutaml/model/collection.rb', line 150
def last
collection.last
end
|
#order_defined? ⇒ Boolean
176
177
178
|
# File 'lib/lutaml/model/collection.rb', line 176
def order_defined?
self.class.sort_configured?
end
|
#push(item) ⇒ Object
158
159
160
161
|
# File 'lib/lutaml/model/collection.rb', line 158
def push(item)
collection.push(item)
sort_items!
end
|
#size ⇒ Object
142
143
144
|
# File 'lib/lutaml/model/collection.rb', line 142
def size
collection.size
end
|
#sort_items! ⇒ Object
180
181
182
183
184
185
186
187
188
189
190
191
|
# File 'lib/lutaml/model/collection.rb', line 180
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
|
113
114
115
|
# File 'lib/lutaml/model/collection.rb', line 113
def to_format(format, options = {})
super(format, options.merge(collection: true))
end
|
#union(other) ⇒ Object
126
127
128
|
# File 'lib/lutaml/model/collection.rb', line 126
def union(other)
self.class.new((items + other.items).uniq)
end
|