Class: Lutaml::Model::Schema::Decorators::Attribute

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

Constant Summary collapse

ATTRIBUTE_TYPES =
{
  "string" => ":string",
  "integer" => ":integer",
  "boolean" => ":boolean",
  "number" => ":float",
  "object" => ":hash",
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options) ⇒ Attribute

Returns a new instance of Attribute.

Parameters:

  • attribute (Hash)

    The JSON schema attribute to be decorated.



20
21
22
23
24
25
# File 'lib/lutaml/model/schema/decorators/attribute.rb', line 20

def initialize(name, options)
  @name = name
  @options = options
  @type = extract_type(options)
  @polymorphic = options["oneOf"]&.map { |choice| choice["$ref"].split("/").last }
end

Instance Attribute Details

#base_classObject

Returns the value of attribute base_class.



17
18
19
# File 'lib/lutaml/model/schema/decorators/attribute.rb', line 17

def base_class
  @base_class
end

#nameObject (readonly)

Returns the value of attribute name.



16
17
18
# File 'lib/lutaml/model/schema/decorators/attribute.rb', line 16

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



16
17
18
# File 'lib/lutaml/model/schema/decorators/attribute.rb', line 16

def options
  @options
end

#polymorphicObject (readonly)

Returns the value of attribute polymorphic.



16
17
18
# File 'lib/lutaml/model/schema/decorators/attribute.rb', line 16

def polymorphic
  @polymorphic
end

#typeObject

Returns the value of attribute type.



17
18
19
# File 'lib/lutaml/model/schema/decorators/attribute.rb', line 17

def type
  @type
end

Instance Method Details

#choice?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/lutaml/model/schema/decorators/attribute.rb', line 61

def choice?
  false
end

#collectionObject



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/lutaml/model/schema/decorators/attribute.rb', line 47

def collection
  return unless collection?

  if @options["minItems"] && @options["maxItems"]
    @options["minItems"]..@options["maxItems"]
  elsif @options["minItems"]
    @options["minItems"]..Float::INFINITY
  elsif @options["maxItems"]
    0..@options["maxItems"]
  else
    true
  end
end

#collection?Boolean

Returns:

  • (Boolean)


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

def collection?
  @options["type"] == "array"
end

#defaultObject



27
28
29
# File 'lib/lutaml/model/schema/decorators/attribute.rb', line 27

def default
  @options["default"]
end

#polymorphic?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/lutaml/model/schema/decorators/attribute.rb', line 31

def polymorphic?
  !!@polymorphic
end

#polymorphic_classesObject



35
36
37
38
39
40
41
# File 'lib/lutaml/model/schema/decorators/attribute.rb', line 35

def polymorphic_classes
  return [] unless polymorphic?

  @base_class.sub_classes.map do |klass|
    klass.namespaced_name.gsub("_", "::")
  end
end