Class: Jsapi::Meta::Model::Reference

Inherits:
Base
  • Object
show all
Defined in:
lib/jsapi/meta/model/reference.rb

Overview

The base reference class.

Constant Summary

Constants included from Attributes

Attributes::DEFAULT_ARRAY, Attributes::DEFAULT_HASH

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize, #inspect, #merge!

Methods included from Attributes

#attribute, #attribute_names

Constructor Details

This class inherits a constructor from Jsapi::Meta::Model::Base

Class Method Details

.component_typeObject

Derrives the component type from the inner most module name.



9
10
11
# File 'lib/jsapi/meta/model/reference.rb', line 9

def self.component_type
  @component_type ||= name.split('::')[-2].underscore
end

.openapi_component_typeObject

Derrives the OpenAPI component type from the inner most module name.



14
15
16
# File 'lib/jsapi/meta/model/reference.rb', line 14

def self.openapi_component_type
  @openapi_component_type ||= name.split('::')[-2].pluralize.camelize(:lower)
end

Instance Method Details

#descriptionObject

:attr: description The description to be displayed instead of the description of the referred object. Applies to OpenAPI 3.1 and higher.



22
# File 'lib/jsapi/meta/model/reference.rb', line 22

attribute :description, String

#refObject

:attr: ref The name of the referred object.



27
# File 'lib/jsapi/meta/model/reference.rb', line 27

attribute :ref, String

#reference?Boolean

Returns true.



36
37
38
# File 'lib/jsapi/meta/model/reference.rb', line 36

def reference?
  true
end

#resolve(definitions) ⇒ Object

Resolves ref by looking up the object with that name in definitions.

Raises a ReferenceError if ref could not be resolved.

Raises:



43
44
45
46
47
48
# File 'lib/jsapi/meta/model/reference.rb', line 43

def resolve(definitions)
  object = definitions.send("find_#{self.class.component_type}", ref)
  raise ReferenceError, ref if object.nil?

  object.resolve(definitions)
end

#summaryObject

:attr: summary The summary to be displayed instead of the summary of the referred object. Applies to OpenAPI 3.1 and higher.



33
# File 'lib/jsapi/meta/model/reference.rb', line 33

attribute :summary, String

#to_openapi(version) ⇒ Object

Returns a hash representing the OpenAPI reference object.



51
52
53
54
55
56
57
58
59
60
# File 'lib/jsapi/meta/model/reference.rb', line 51

def to_openapi(version, *)
  version = OpenAPI::Version.from(version)

  { '$ref': "#/#{openapi_components_path(version)}/#{ref}" }.tap do |result|
    if version >= OpenAPI::V3_1
      result[:summary] = summary if summary
      result[:description] = description if description
    end
  end
end