Class: EasyTalk::Property
- Inherits:
-
Object
- Object
- EasyTalk::Property
- Extended by:
- T::Sig
- Defined in:
- lib/easy_talk/property.rb
Overview
Property class for building a JSON schema property.
Constant Summary collapse
- TYPE_TO_BUILDER =
{ 'String' => Builders::StringBuilder, 'Integer' => Builders::IntegerBuilder, 'Float' => Builders::NumberBuilder, 'BigDecimal' => Builders::NumberBuilder, 'T::Boolean' => Builders::BooleanBuilder, 'NilClass' => Builders::NullBuilder, 'Date' => Builders::DateBuilder, 'DateTime' => Builders::DatetimeBuilder, 'Time' => Builders::TimeBuilder, 'anyOf' => Builders::AnyOfBuilder, 'allOf' => Builders::AllOfBuilder, 'oneOf' => Builders::OneOfBuilder, 'T::Types::TypedArray' => Builders::TypedArrayBuilder, 'T::Types::Union' => Builders::UnionBuilder }.freeze
Instance Attribute Summary collapse
-
#constraints ⇒ Object
readonly
Returns the value of attribute constraints.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#as_json(*_args) ⇒ Hash
Converts the object to a JSON representation.
-
#build ⇒ Object
Builds the property based on the specified type, constraints, and builder.
-
#builder ⇒ Builder
Returns the builder associated with the property type.
-
#initialize(name, type = nil, constraints = {}) ⇒ Property
constructor
A new instance of Property.
Constructor Details
#initialize(name, type = nil, constraints = {}) ⇒ Property
Returns a new instance of Property.
62 63 64 65 66 67 |
# File 'lib/easy_talk/property.rb', line 62 def initialize(name, type = nil, constraints = {}) @name = name @type = type @constraints = constraints raise ArgumentError, 'property type is missing' if type.blank? end |
Instance Attribute Details
#constraints ⇒ Object (readonly)
Returns the value of attribute constraints.
34 35 36 |
# File 'lib/easy_talk/property.rb', line 34 def constraints @constraints end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
34 35 36 |
# File 'lib/easy_talk/property.rb', line 34 def name @name end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
34 35 36 |
# File 'lib/easy_talk/property.rb', line 34 def type @type end |
Instance Method Details
#as_json(*_args) ⇒ Hash
Converts the object to a JSON representation.
95 96 97 |
# File 'lib/easy_talk/property.rb', line 95 def as_json(*_args) build.as_json end |
#build ⇒ Object
Builds the property based on the specified type, constraints, and builder.
If the type responds to the ‘schema` method, it returns the schema of the type. Otherwise, it returns ’object’.
If a builder is specified, it uses the builder to build the property. The arguments passed to the builder depend on whether the builder is a collection type or not.
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/easy_talk/property.rb', line 78 def build # return type.respond_to?(:schema) ? type.schema : 'object' unless builder # args = builder.collection_type? ? [name, type, constraints] : [name, constraints] # builder.new(*args).build if builder args = builder.collection_type? ? [name, type, constraints] : [name, constraints] builder.new(*args).build else type.respond_to?(:schema) ? type.schema : 'object' end end |
#builder ⇒ Builder
Returns the builder associated with the property type.
The builder is responsible for constructing the property based on its type. It looks up the builder based on the type’s class name or name.
105 106 107 |
# File 'lib/easy_talk/property.rb', line 105 def builder @builder ||= TYPE_TO_BUILDER[type.class.name.to_s] || TYPE_TO_BUILDER[type.name.to_s] end |