Class: Jsapi::Meta::Property
- Inherits:
-
Base::Model
- Object
- Base::Model
- Jsapi::Meta::Property
- 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
-
#initialize(name, keywords = {}) ⇒ Property
constructor
Creates a new property.
-
#name ⇒ Object
:attr_reader: name The name of the property.
-
#read_only ⇒ Object
:attr: read_only.
-
#reader ⇒ Object
Returns the Callable used to read a property value.
-
#required? ⇒ Boolean
Returns true if the level of existence is greater than or equal to
ALLOW_NIL
, false otherwise. -
#schema ⇒ Object
:attr_reader: schema The Schema of the parameter.
-
#source ⇒ Object
:attr: source The alternative Callable used to read property values.
-
#to_openapi(version) ⇒ Object
Returns a hash representing the OpenAPI schema object.
-
#write_only ⇒ Object
:attr: write_only.
Methods inherited from Base::Model
#inspect, #merge!, #reference?, #resolve
Methods included from Base::Attributes
Constructor Details
#initialize(name, keywords = {}) ⇒ Property
Creates a new property.
Raises an ArgumentError
if name
is blank.
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
#name ⇒ Object
:attr_reader: name The name of the property.
12 |
# File 'lib/jsapi/meta/property.rb', line 12 attribute :name, read_only: true |
#read_only ⇒ Object
:attr: read_only
16 |
# File 'lib/jsapi/meta/property.rb', line 16 attribute :read_only, values: [true, false] |
#reader ⇒ Object
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 |
#schema ⇒ Object
:attr_reader: schema The Schema of the parameter.
21 |
# File 'lib/jsapi/meta/property.rb', line 21 attribute :schema, read_only: true |
#source ⇒ Object
: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_only ⇒ Object
:attr: write_only
30 |
# File 'lib/jsapi/meta/property.rb', line 30 attribute :write_only, values: [true, false] |