Class: Jsapi::Meta::Property

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

Overview

Specifies a property

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)


35
36
37
38
39
40
41
42
43
44
45
# File 'lib/jsapi/meta/property.rb', line 35

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.



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

attribute :name, accessors: i[reader]

#read_onlyObject

:attr: read_only



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

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.



49
50
51
# File 'lib/jsapi/meta/property.rb', line 49

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.



55
56
57
# File 'lib/jsapi/meta/property.rb', line 55

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

#schemaObject

:attr_reader: schema The Schema of the parameter.



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

attribute :schema, accessors: i[reader]

#sourceObject

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



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

attribute :source, Callable

#to_openapi(version) ⇒ Object

Returns a hash representing the OpenAPI schema object.



60
61
62
63
64
65
66
67
# File 'lib/jsapi/meta/property.rb', line 60

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



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

attribute :write_only, values: [true, false]