Class: Eazypi::Schema::Object

Inherits:
Object
  • Object
show all
Defined in:
lib/eazypi/schema/object.rb

Overview

Object schema for Json

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object_name = nil) ⇒ Object

Returns a new instance of Object.



9
10
11
12
13
14
# File 'lib/eazypi/schema/object.rb', line 9

def initialize(object_name = nil)
  @properties = {}
  @required = []
  @object_name = object_name
  @reference = nil
end

Instance Attribute Details

#object_nameObject (readonly)

Returns the value of attribute object_name.



7
8
9
# File 'lib/eazypi/schema/object.rb', line 7

def object_name
  @object_name
end

Instance Method Details

#==(other) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/eazypi/schema/object.rb', line 43

def ==(other)
  return false unless other.is_a?(Object)

  other.instance_variable_get(:@properties) == @properties &&
    other.instance_variable_get(:@required) == @required &&
    other.object_name == object_name
end

#collect_components(schemas: nil, **kwargs) ⇒ Object



26
27
28
29
30
31
# File 'lib/eazypi/schema/object.rb', line 26

def collect_components(schemas: nil, **kwargs)
  @properties.each_value do |property_schema|
    property_schema.collect_components(schemas: schemas, **kwargs)
    schemas&.call(property_schema)
  end
end

#property(name, schema, required: false) ⇒ Object



21
22
23
24
# File 'lib/eazypi/schema/object.rb', line 21

def property(name, schema, required: false)
  @properties[name] = schema
  @required << name if required
end

#reference!(reference) ⇒ Object



16
17
18
19
# File 'lib/eazypi/schema/object.rb', line 16

def reference!(reference)
  @reference = reference
  self
end

#to_openapi_specObject



33
34
35
36
37
38
39
40
41
# File 'lib/eazypi/schema/object.rb', line 33

def to_openapi_spec
  return { "$ref" => @reference } if @reference

  {
    "type" => "object",
    "required" => @required.empty? ? nil : @required,
    "properties" => @properties.transform_values(&:to_openapi_spec)
  }.compact
end