Class: Lutaml::Model::Schema::Generator::DefinitionsCollection
- Inherits:
-
Object
- Object
- Lutaml::Model::Schema::Generator::DefinitionsCollection
- Extended by:
- SharedMethods
- Defined in:
- lib/lutaml/model/schema/generator/definitions_collection.rb
Instance Attribute Summary collapse
-
#definitions ⇒ Object
readonly
Returns the value of attribute definitions.
Class Method Summary collapse
- .from_class(klass) ⇒ Object
- .process_attribute(collection, attribute, register) ⇒ Object
- .process_attributes(collection, klass) ⇒ Object
- .process_polymorphic_types(collection, attribute) ⇒ Object
-
.process_union_members(collection, attribute) ⇒ Object
A union attribute's type is Type::Union, not a model, so collect definitions for each of its Serializable members directly (a union schema emits a $ref per model member).
Instance Method Summary collapse
- #add_definition(definition) ⇒ Object (also: #<<, #push)
-
#initialize(definitions = []) ⇒ DefinitionsCollection
constructor
A new instance of DefinitionsCollection.
- #merge(collection) ⇒ Object
- #to_schema ⇒ Object
Methods included from SharedMethods
Constructor Details
#initialize(definitions = []) ⇒ DefinitionsCollection
Returns a new instance of DefinitionsCollection.
62 63 64 65 66 67 68 |
# File 'lib/lutaml/model/schema/generator/definitions_collection.rb', line 62 def initialize(definitions = []) @definitions = definitions.map do |definition| next definition if definition.is_a?(Definition) Definition.new(definition) end end |
Instance Attribute Details
#definitions ⇒ Object (readonly)
Returns the value of attribute definitions.
60 61 62 |
# File 'lib/lutaml/model/schema/generator/definitions_collection.rb', line 60 def definitions @definitions end |
Class Method Details
.from_class(klass) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/lutaml/model/schema/generator/definitions_collection.rb', line 11 def from_class(klass) new.tap do |collection| collection << Definition.new(klass) process_attributes(collection, klass) end end |
.process_attribute(collection, attribute, register) ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/lutaml/model/schema/generator/definitions_collection.rb', line 42 def process_attribute(collection, attribute, register) attr_type = Lutaml::Model::GlobalContext.resolve_type( attribute.type, register ) collection.merge(DefinitionsCollection.from_class(attr_type)) process_polymorphic_types(collection, attribute) end |
.process_attributes(collection, klass) ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/lutaml/model/schema/generator/definitions_collection.rb', line 19 def process_attributes(collection, klass) register = extract_register_from(klass) klass.attributes.each_value do |attribute| if attribute.union? process_union_members(collection, attribute) elsif attribute.serializable?(register) process_attribute(collection, attribute, register) end end end |
.process_polymorphic_types(collection, attribute) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/lutaml/model/schema/generator/definitions_collection.rb', line 51 def process_polymorphic_types(collection, attribute) return unless attribute.&.[](:polymorphic) attribute.[:polymorphic].each do |child| collection.merge(DefinitionsCollection.from_class(child)) end end |
.process_union_members(collection, attribute) ⇒ Object
A union attribute's type is Type::Union, not a model, so collect definitions for each of its Serializable members directly (a union schema emits a $ref per model member).
33 34 35 36 37 38 39 40 |
# File 'lib/lutaml/model/schema/generator/definitions_collection.rb', line 33 def process_union_members(collection, attribute) attribute.union_member_types.each do |member| next unless member.is_a?(::Class) && member.include?(Lutaml::Model::Serialize) collection.merge(DefinitionsCollection.from_class(member)) end end |
Instance Method Details
#add_definition(definition) ⇒ Object Also known as: <<, push
76 77 78 79 |
# File 'lib/lutaml/model/schema/generator/definitions_collection.rb', line 76 def add_definition(definition) @definitions ||= [] @definitions << definition end |
#merge(collection) ⇒ Object
83 84 85 86 87 88 89 90 91 |
# File 'lib/lutaml/model/schema/generator/definitions_collection.rb', line 83 def merge(collection) @definitions ||= [] if collection.is_a?(Array) @definitions.concat(collection) else @definitions.concat(collection.definitions) end end |
#to_schema ⇒ Object
70 71 72 73 74 |
# File 'lib/lutaml/model/schema/generator/definitions_collection.rb', line 70 def to_schema definitions.each_with_object({}) do |definition, schema| schema.merge!(definition.to_schema) end end |