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

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

Overview

The base reference class.

Defined Under Namespace

Classes: Resolver

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize, #inspect, #merge!

Methods included from Attributes

#attributes_frozen?, #freeze_attributes, included

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.



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

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

.openapi_component_typeObject

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



40
41
42
# File 'lib/jsapi/meta/model/reference.rb', line 40

def 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.



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

attribute :description, String

#refObject

:attr: ref The name of the referred object.



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

attribute :ref, String

#reference?Boolean

Returns true.

Returns:

  • (Boolean)


65
66
67
# File 'lib/jsapi/meta/model/reference.rb', line 65

def reference?
  true
end

#resolve(definitions, deep: true) ⇒ Object

Resolves the reference by looking up the referred object in definitions.

Raises a ReferenceError if the reference could not be resolved.

Raises:



72
73
74
75
76
77
# File 'lib/jsapi/meta/model/reference.rb', line 72

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

  deep ? object.resolve(definitions, deep: true) : object
end

#resolve_lazily(definitions) ⇒ Object

Lazily resolves the reference.

Raises a ReferenceError if the reference could not be resolved.



82
83
84
# File 'lib/jsapi/meta/model/reference.rb', line 82

def resolve_lazily(definitions)
  Resolver.new(self, 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.



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

attribute :summary, String

#to_openapi(version) ⇒ Object

Returns a hash representing the OpenAPI reference object.



87
88
89
90
91
92
93
94
95
96
# File 'lib/jsapi/meta/model/reference.rb', line 87

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