Class: Jsapi::Meta::Property

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

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)


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

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.



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

attribute :name, read_only: true

#read_onlyObject

:attr: read_only



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

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.



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

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)


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

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

#schemaObject

:attr_reader: schema The Schema of the parameter.



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

attribute :schema, read_only: true

#sourceObject

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



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

attribute :source, Callable

#to_openapi(version) ⇒ Object

Returns a hash representing the OpenAPI schema object.



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

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



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

attribute :write_only, values: [true, false]