Class: Jsapi::Meta::Property

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

Instance Method Summary collapse

Methods inherited from Base

#inspect, #reference?, #resolve

Methods included from Attributes::ClassMethods

#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
# File 'lib/jsapi/meta/property.rb', line 35

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

  keywords = keywords.dup
  super(keywords.extract!(:read_only, :source, :write_only))

  @name = name.to_s
  @schema = Schema.new(keywords)
end

Instance Method Details

#nameObject

:attr_reader: name The name of the property.



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

attribute :name, writer: false

#read_onlyObject

:attr: read_only



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

attribute :read_only, values: [true, false]

#required?Boolean

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

Returns:

  • (Boolean)


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

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

#schemaObject

:attr_reader: schema The Schema of the parameter.



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

attribute :schema, writer: false

#sourceObject

:attr: source The alternative method to read a property value when serializing an object.



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

attribute :source, Symbol

#to_openapi(version) ⇒ Object

Returns a hash representing the OpenAPI schema object.



52
53
54
55
56
57
58
59
# File 'lib/jsapi/meta/property.rb', line 52

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



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

attribute :write_only, values: [true, false]