Class: EasyTalk::Property

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(name, type = nil, constraints = {}) ⇒ Property

Returns a new instance of Property.

Raises:

  • (ArgumentError)


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

#constraintsHash<Symbol, Object> (readonly)

Returns Additional constraints applied to the property.

Returns:

  • (Hash<Symbol, Object>)

    Additional constraints applied to the property



46
47
48
# File 'lib/easy_talk/property.rb', line 46

def constraints
  @constraints
end

#nameSymbol (readonly)

Returns The name of the property.

Returns:

  • (Symbol)

    The name of the property



40
41
42
# File 'lib/easy_talk/property.rb', line 40

def name
  @name
end

#typeObject (readonly)

Returns The type definition of the property.

Returns:

  • (Object)

    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.

Parameters:

  • _args (Array)

    Optional arguments passed to #as_json (ignored)

Returns:

  • (Hash)

    The JSON-compatible representation of the property schema

See Also:



150
151
152
# File 'lib/easy_talk/property.rb', line 150

def as_json(*_args)
  build.as_json
end

#buildHash

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 (EasyTalk models)

  • Default fallback to ‘object’ type

When use_refs is enabled (globally or per-property), EasyTalk models are referenced via $ref instead of being inlined.

Examples:

Simple string property

property = Property.new(:name, 'String')
property.build  # => {"type"=>"string"}

Complex nested schema (inlined)

address = Address.new  # A class with a .schema method
property = Property.new(:shipping_address, address, description: "Shipping address")
property.build  # => Address schema merged with the description constraint

Nested schema with $ref

property = Property.new(:shipping_address, Address, ref: true)
property.build  # => {"$ref"=>"#/$defs/Address", ...constraints}

Returns:

  • (Hash)

    The complete JSON Schema property definition



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/easy_talk/property.rb', line 124

def build
  if nilable_type?
    build_nilable_schema
  elsif should_use_ref?
    build_ref_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

#builderClass?

Returns the builder class associated with the property type.

The builder is responsible for converting the Ruby type to a JSON Schema type.

Returns:

  • (Class, nil)

    The builder class for this property’s type, or nil if no dedicated builder exists

See Also:

  • #find_builder_for_type


160
161
162
# File 'lib/easy_talk/property.rb', line 160

def builder
  @builder ||= find_builder_for_type
end