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.

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Property.

Raises:

  • (ArgumentError)


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

#constraintsObject (readonly)

Returns the value of attribute constraints.



34
35
36
# File 'lib/easy_talk/property.rb', line 34

def constraints
  @constraints
end

#nameObject (readonly)

Returns the value of attribute name.



34
35
36
# File 'lib/easy_talk/property.rb', line 34

def name
  @name
end

#typeObject (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.

Parameters:

  • _args (Array)

    Optional arguments

Returns:

  • (Hash)

    The JSON representation of the object



97
98
99
# File 'lib/easy_talk/property.rb', line 97

def as_json(*_args)
  build.as_json
end

#buildObject

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.

Returns:

  • (Object)

    The built property.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/easy_talk/property.rb', line 78

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

#builderBuilder

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.

Returns:

  • (Builder)

    The builder associated with the property type.



107
108
109
# File 'lib/easy_talk/property.rb', line 107

def builder
  @builder ||= TYPE_TO_BUILDER[type.class.name.to_s] || TYPE_TO_BUILDER[type.name.to_s]
end