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

Attributes inherited from Leaf

#index

Instance Method Summary collapse

Methods inherited from Leaf

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

Constructor Details

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

Parameters:

  • properties (Hash)

    A hash of properties



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

def initialize(properties)
  @properties = LLM::Object.new
  properties.each { set!(_1, _2) }
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

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

Get a property

Returns:



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

def [](key)
  properties[key]
end

#[]=(key, val) ⇒ void

This method returns an undefined value.

Set a property



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

def []=(key, val)
  set!(key, val)
end

#keysArray<String>

Returns:



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

def keys
  @properties.keys
end

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

Returns self

Returns:

Raises:

  • (TypeError)

    When given an object other than Object



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

def merge!(other)
  raise TypeError, "expected #{self.class} but got #{other.class}" unless self.class === other
  other.properties.each { |key, val| self[key] = val }
  self
end

#to_hHash

Returns:

  • (Hash)


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

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

#to_jsonString

Returns:



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

def to_json(...)
  LLM.json.dump(to_h, ...)
end