Class: Jsapi::Meta::Property

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

Overview

Specifies a property

Constant Summary

Constants included from Base::Attributes

Base::Attributes::DEFAULT_ARRAY, Base::Attributes::DEFAULT_HASH

Instance Method Summary collapse

Methods inherited from Base::Model

#inspect, #merge!, #reference?, #resolve

Methods included from Base::Attributes

#attribute, #attribute_names

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

Instance Method Details

#nameObject

:attr_reader: name The name of the property.



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

attribute :name, read_only: true

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

Returns:

  • (Boolean)


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, read_only: true

#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 |hash|
    hash[:readOnly] = true if read_only?
    hash[: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]