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,
  '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)


59
60
61
62
63
64
# File 'lib/easy_talk/property.rb', line 59

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.



30
31
32
# File 'lib/easy_talk/property.rb', line 30

def constraints
  @constraints
end

#nameObject (readonly)

Returns the value of attribute name.



30
31
32
# File 'lib/easy_talk/property.rb', line 30

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



30
31
32
# File 'lib/easy_talk/property.rb', line 30

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



94
95
96
# File 'lib/easy_talk/property.rb', line 94

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.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/easy_talk/property.rb', line 75

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.



104
105
106
# File 'lib/easy_talk/property.rb', line 104

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