Class: Jsapi::Meta::Property

Inherits:
Jsapi::Model::Base show all
Includes:
Model::Wrappable
Defined in:
lib/jsapi/meta/property.rb

Overview

Specifies a property

Defined Under Namespace

Classes: Wrapper

Instance Method Summary collapse

Methods inherited from Jsapi::Model::Base

#==, #errors, #inspect, model_name, #respond_to_missing?

Constructor Details

#initialize(name, keywords = {}) ⇒ Property

Creates a new property.

Raises an ArgumentError if name is blank.

Raises:

  • (ArgumentError)


45
46
47
48
49
50
51
52
53
54
55
# File 'lib/jsapi/meta/property.rb', line 45

def initialize(name, keywords = {})
  raise ArgumentError, "property name can't be blank" if name.blank?

  @name = name.to_s

  keywords = keywords.dup
  super(keywords.extract!(:read_only, :source, :write_only))
  keywords[:ref] = keywords.delete(:schema) if keywords.key?(:schema)

  @schema = Schema.new(keywords)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Jsapi::Model::Base

Instance Method Details

#nameObject

:attr_reader: name The name of the property.



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

attribute :name, accessors: %i[reader]

#read_onlyObject

:attr: read_only



26
# File 'lib/jsapi/meta/property.rb', line 26

attribute :read_only, values: [true, false]

#readerObject

Returns the Callable used to read a property value. By default, a property value is read by calling the method whose name matches the property name.



59
60
61
# File 'lib/jsapi/meta/property.rb', line 59

def reader
  source || (@reader ||= Callable.from(name.underscore.to_sym))
end

#required?Boolean

Returns true if the level of existence is greater than or equal to ALLOW_NIL, false otherwise.

Returns:

  • (Boolean)


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

def required?
  schema.existence >= Existence::ALLOW_NIL
end

#schemaObject

:attr_reader: schema The Schema of the parameter.



31
# File 'lib/jsapi/meta/property.rb', line 31

attribute :schema, accessors: %i[reader]

#sourceObject

:attr: source The alternative Callable used to read property values.



36
# File 'lib/jsapi/meta/property.rb', line 36

attribute :source, Callable

#to_openapi(version) ⇒ Object

Returns a hash representing the OpenAPI schema object.



70
71
72
73
74
75
76
77
# File 'lib/jsapi/meta/property.rb', line 70

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

  schema.to_openapi(version).tap do |result|
    result[:readOnly] = true if read_only?
    result[:writeOnly] = true if write_only? && version.major > 2
  end
end

#write_onlyObject

:attr: write_only



40
# File 'lib/jsapi/meta/property.rb', line 40

attribute :write_only, values: [true, false]