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.

Parameters:

  • schema (Hash)

    The JSON schema to be decorated.

  • options (Hash)

    Additional options for the decorator.



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

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.



12
13
14
# File 'lib/lutaml/model/schema/decorators/class_definition.rb', line 12

def additional_properties
  @additional_properties
end

#base_classObject

Returns the value of attribute base_class.



11
12
13
# File 'lib/lutaml/model/schema/decorators/class_definition.rb', line 11

def base_class
  @base_class
end

#namespaced_nameObject (readonly)

Returns the value of attribute namespaced_name.



12
13
14
# File 'lib/lutaml/model/schema/decorators/class_definition.rb', line 12

def namespaced_name
  @namespaced_name
end

#propertiesObject (readonly)

Returns the value of attribute properties.



12
13
14
# File 'lib/lutaml/model/schema/decorators/class_definition.rb', line 12

def properties
  @properties
end

#sub_classesObject

Returns the value of attribute sub_classes.



11
12
13
# File 'lib/lutaml/model/schema/decorators/class_definition.rb', line 11

def sub_classes
  @sub_classes
end

Instance Method Details

#attributesObject



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

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

Returns:

  • (Boolean)


50
51
52
# File 'lib/lutaml/model/schema/decorators/class_definition.rb', line 50

def choice?
  @choices&.any?
end

#nameObject



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

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

#namespacesObject



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

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

#parent_classObject



46
47
48
# File 'lib/lutaml/model/schema/decorators/class_definition.rb', line 46

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

#polymorphic?Boolean

Returns:

  • (Boolean)


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

def polymorphic?
  polymorphic_attributes.any?
end

#polymorphic_attributesObject



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

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

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

  @polymorphic_attributes
end