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.
This class handles the conversion from Ruby types to JSON Schema property definitions, and provides support for common constraints like minimum/maximum values, string patterns, and custom validators.
Constant Summary collapse
- TYPE_TO_BUILDER =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Mapping of Ruby type names to their corresponding schema builder classes. Each builder knows how to convert a specific Ruby type to JSON Schema.
{ 'String' => Builders::StringBuilder, 'Integer' => Builders::IntegerBuilder, 'Float' => Builders::NumberBuilder, 'BigDecimal' => Builders::NumberBuilder, 'T::Boolean' => Builders::BooleanBuilder, 'TrueClass' => Builders::BooleanBuilder, 'NilClass' => Builders::NullBuilder, 'Date' => Builders::TemporalBuilder::DateBuilder, 'DateTime' => Builders::TemporalBuilder::DatetimeBuilder, 'Time' => Builders::TemporalBuilder::TimeBuilder, 'anyOf' => Builders::CompositionBuilder::AnyOfBuilder, 'allOf' => Builders::CompositionBuilder::AllOfBuilder, 'oneOf' => Builders::CompositionBuilder::OneOfBuilder, 'T::Types::TypedArray' => Builders::TypedArrayBuilder, 'T::Types::Union' => Builders::UnionBuilder }.freeze
Instance Attribute Summary collapse
-
#constraints ⇒ Hash<Symbol, Object>
readonly
Additional constraints applied to the property.
-
#name ⇒ Symbol
readonly
The name of the property.
-
#type ⇒ Object
readonly
The type definition of the property.
Instance Method Summary collapse
-
#as_json(*_args) ⇒ Hash
Converts the property definition to a JSON-compatible format.
-
#build ⇒ Hash
Builds the property schema based on its type and constraints.
-
#builder ⇒ Class?
Returns the builder class 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.
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/easy_talk/property.rb', line 88 def initialize(name, type = nil, constraints = {}) @name = name @type = type @constraints = constraints if type.nil? || (type.respond_to?(:empty?) && type.is_a?(String) && type.strip.empty?) raise ArgumentError, 'property type is missing' end raise ArgumentError, 'property type is not supported' if type.is_a?(Array) && type.empty? end |
Instance Attribute Details
#constraints ⇒ Hash<Symbol, Object> (readonly)
Returns Additional constraints applied to the property.
46 47 48 |
# File 'lib/easy_talk/property.rb', line 46 def constraints @constraints end |
#name ⇒ Symbol (readonly)
Returns The name of the property.
40 41 42 |
# File 'lib/easy_talk/property.rb', line 40 def name @name end |
#type ⇒ Object (readonly)
Returns The type definition of the property.
43 44 45 |
# File 'lib/easy_talk/property.rb', line 43 def type @type end |
Instance Method Details
#as_json(*_args) ⇒ Hash
Converts the property definition to a JSON-compatible format.
This method enables seamless integration with Ruby’s JSON library.
141 142 143 |
# File 'lib/easy_talk/property.rb', line 141 def as_json(*_args) build.as_json end |
#build ⇒ Hash
Builds the property schema based on its type and constraints.
This method handles different types of properties:
-
Nilable types (can be null)
-
Types with dedicated builders
-
Types that implement their own schema method
-
Default fallback to ‘object’ type
117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/easy_talk/property.rb', line 117 def build if nilable_type? build_nilable_schema elsif builder args = builder.collection_type? ? [name, type, constraints] : [name, constraints] builder.new(*args).build elsif type.respond_to?(:schema) # merge the top-level constraints from *this* property # e.g. :title, :description, :default, etc type.schema.merge!(constraints) else 'object' end end |
#builder ⇒ Class?
Returns the builder class associated with the property type.
The builder is responsible for converting the Ruby type to a JSON Schema type.
151 152 153 |
# File 'lib/easy_talk/property.rb', line 151 def builder @builder ||= find_builder_for_type end |