Class: Docit::SchemaDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/docit/schema_definition.rb

Overview

A reusable schema definition that can be referenced via $ref. Defined with define_schema and rendered under components/schemas in the OpenAPI spec.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ SchemaDefinition

Returns a new instance of SchemaDefinition.



10
11
12
13
# File 'lib/docit/schema_definition.rb', line 10

def initialize(name)
  @name = name
  @properties = []
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/docit/schema_definition.rb', line 8

def name
  @name
end

#propertiesObject (readonly)

Returns the value of attribute properties.



8
9
10
# File 'lib/docit/schema_definition.rb', line 8

def properties
  @properties
end

Instance Method Details

#property(prop_name, type:, format: nil, example: nil, enum: nil, description: nil, items: nil, **opts, &block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/docit/schema_definition.rb', line 15

def property(prop_name, type:, format: nil, example: nil, enum: nil, description: nil, items: nil, **opts, &block)
  prop = { name: prop_name, type: type }
  prop[:format] = format if format
  prop[:example] = example if example
  prop[:enum] = enum if enum
  prop[:description] = description if description
  prop[:items] = items if items
  prop.merge!(opts)

  if block_given?
    nested = self.class.new(:"#{@name}_#{prop_name}")
    nested.instance_eval(&block)
    prop[:children] = nested.properties
  end

  @properties << prop
end