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, #required?

Constructor Details

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

Parameters:

  • params (Hash)

    A hash of properties



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

def initialize(properties)
  @properties = properties
end

Instance Attribute Details

#propertiesHash (readonly)

Returns:

  • (Hash)


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

def properties
  @properties
end

Instance Method Details

#to_hHash

Returns:

  • (Hash)


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

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

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

Get a property

Returns:



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

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

#[]=(key, val) ⇒ void

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

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

Returns self

Returns:

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_json(options = {}) ⇒ String

Returns:



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

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

#keysArray<String>

Returns:



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

def keys
  @properties.keys
end