Class: LLM::Schema::Object

Inherits:
Leaf
  • Object
show all
Defined in:
lib/llm/schema/object.rb

Overview

The LLM::Schema::Object class represents an object value in a JSON schema. It is a subclass of LLM::Schema::Leaf and provides methods that can act as constraints.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Leaf

#==, #const, #default, #description, #enum, #optional, #optional?, #required?

Constructor Details

#initialize(properties) ⇒ LLM::Schema::Object



18
19
20
# File 'lib/llm/schema/object.rb', line 18

def initialize(properties)
  @properties = properties
end

Instance Attribute Details

#propertiesHash (readonly)



12
13
14
# File 'lib/llm/schema/object.rb', line 12

def properties
  @properties
end

Instance Method Details

#[](key) ⇒ LLM::Schema::Leaf

Get a property



25
26
27
# File 'lib/llm/schema/object.rb', line 25

def [](key)
  properties[key.to_s]
end

#[]=(key, val)

This method returns an undefined value.

Set a property



32
33
34
# File 'lib/llm/schema/object.rb', line 32

def []=(key, val)
  properties[key.to_s] = val
end

#keysArray<String>



61
62
63
# File 'lib/llm/schema/object.rb', line 61

def keys
  @properties.keys
end

#merge!(other) ⇒ LLM::Schema::Object

Returns self

Raises:

  • (TypeError)

    When given an object other than Object



47
48
49
50
51
# File 'lib/llm/schema/object.rb', line 47

def merge!(other)
  raise TypeError, "expected #{self.class} but got #{other.class}" unless self.class === other
  @properties.merge!(other.properties)
  self
end

#to_hHash



38
39
40
# File 'lib/llm/schema/object.rb', line 38

def to_h
  super.merge!({type: "object", properties:, required:})
end

#to_json(options = {}) ⇒ String



55
56
57
# File 'lib/llm/schema/object.rb', line 55

def to_json(options = {})
  to_h.to_json(options)
end