Class: Lutaml::Model::Schema::Decorators::ClassDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/model/schema/decorators/class_definition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespaced_name, schema) ⇒ ClassDefinition

Decorates a JSON schema information to be used in class definitions. This class is used to provide a structured way to handle schema information for class definitions, including attributes, required fields, and JSON mappings.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/lutaml/model/schema/decorators/class_definition.rb', line 18

def initialize(namespaced_name, schema)
  @namespaced_name = namespaced_name
  @choices = schema["oneOf"] || []
  @additional_properties = schema["additionalProperties"] || false
  @polymorphic_attributes = []

  @properties = (schema["properties"] || {}).to_h do |name, attr|
    attribute = Decorators::Attribute.new(name, attr)
    polymorphic_attributes << attribute if attribute.polymorphic?

    [name, attribute]
  end

  @base_class = nil
  @sub_classes = []
end

Instance Attribute Details

#additional_propertiesObject (readonly)

Returns the value of attribute additional_properties.



9
10
11
# File 'lib/lutaml/model/schema/decorators/class_definition.rb', line 9

def additional_properties
  @additional_properties
end

#base_classObject

Returns the value of attribute base_class.



8
9
10
# File 'lib/lutaml/model/schema/decorators/class_definition.rb', line 8

def base_class
  @base_class
end

#namespaced_nameObject (readonly)

Returns the value of attribute namespaced_name.



9
10
11
# File 'lib/lutaml/model/schema/decorators/class_definition.rb', line 9

def namespaced_name
  @namespaced_name
end

#propertiesObject (readonly)

Returns the value of attribute properties.



9
10
11
# File 'lib/lutaml/model/schema/decorators/class_definition.rb', line 9

def properties
  @properties
end

#sub_classesObject

Returns the value of attribute sub_classes.



8
9
10
# File 'lib/lutaml/model/schema/decorators/class_definition.rb', line 8

def sub_classes
  @sub_classes
end

Instance Method Details

#attributesObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/lutaml/model/schema/decorators/class_definition.rb', line 52

def attributes
  return @properties.values if !choice?

  choice_attributes = {}
  @choices.each do |choice|
    choice["properties"].each do |name, attr|
      choice_attributes[name] =
        properties[name] || Decorators::Attribute.new(name, attr)
      properties[name] = nil if properties[name]
    end
  end

  @properties.values.compact + [Choices.new(choice_attributes)]
end

#choice?Boolean



48
49
50
# File 'lib/lutaml/model/schema/decorators/class_definition.rb', line 48

def choice?
  @choices&.any?
end

#nameObject



35
36
37
# File 'lib/lutaml/model/schema/decorators/class_definition.rb', line 35

def name
  @name ||= @namespaced_name.split("_").last
end

#namespacesObject



39
40
41
# File 'lib/lutaml/model/schema/decorators/class_definition.rb', line 39

def namespaces
  @namespaces ||= @namespaced_name.split("_")[0..-2]
end

#parent_classObject



43
44
45
46
# File 'lib/lutaml/model/schema/decorators/class_definition.rb', line 43

def parent_class
  @parent_class ||= @base_class&.namespaced_name&.gsub("_",
                                                       "::") || "Lutaml::Model::Serializable"
end

#polymorphic?Boolean



67
68
69
# File 'lib/lutaml/model/schema/decorators/class_definition.rb', line 67

def polymorphic?
  polymorphic_attributes.any?
end

#polymorphic_attributesObject



71
72
73
74
75
76
77
78
79
# File 'lib/lutaml/model/schema/decorators/class_definition.rb', line 71

def polymorphic_attributes
  return @polymorphic_attributes if !@polymorphic_attributes.nil?

  attributes.each do |attr|
    @polymorphic_attributes << attr if attr.polymorphic?
  end

  @polymorphic_attributes
end