Class: LLM::Schema::Leaf

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

Overview

The LLM::Schema::Leaf class is the superclass of all values that can appear in a JSON schema. See the instance methods of LLM::Schema for an example of how to create instances of LLM::Schema::Leaf through its subclasses.

Direct Known Subclasses

AllOf, AnyOf, Array, Boolean, Deferred, Integer, Null, Number, Object, OneOf, String

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ LLM::Schema::Leaf

Returns a leaf.



18
19
20
21
22
23
24
25
26
# File 'lib/llm/schema/leaf.rb', line 18

def initialize
  @description = nil
  @default = nil
  @enum = nil
  @required = nil
  @const = nil
  @index = nil
  @owner = nil
end

Instance Attribute Details

#index ⇒ Integer?

Returns:



13
14
15
# File 'lib/llm/schema/leaf.rb', line 13

def index
  @index
end

Instance Method Details

#hash ⇒ Integer

Returns:



155
156
157
# File 'lib/llm/schema/leaf.rb', line 155

def hash
  to_h.hash
end

#owner=(owner)

This method returns an undefined value.

Parameters:

  • owner (Object) —

    The owner of a schema, typically an LLM::Tool or LLM::Schema class



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

def owner=(owner)
  @owner = owner
end

#resolve(value) ⇒ Object

Resolves a configured value against the owner, following Utils#resolve_option. A Proc runs against the owner, a Symbol is sent to it, and everything else passes through unchanged.

Parameters:

Returns:



43
44
45
# File 'lib/llm/schema/leaf.rb', line 43

def resolve(value)
  LLM::Utils.resolve_option(@owner, value, resolve_symbol: true)
end

#description(str = nil) ⇒ LLM::Schema::Leaf

Set the description of a leaf

Parameters:

  • str (String) (defaults to: nil) —

    The description

Returns:



51
52
53
54
55
56
57
# File 'lib/llm/schema/leaf.rb', line 51

def description(str = nil)
  if str.nil?
    @description
  else
    tap { @description = str }
  end
end

#default(value = nil) ⇒ LLM::Schema::Leaf

Set the default value of a leaf

Parameters:

  • value (Object) (defaults to: nil) —

    The default value

Returns:



63
64
65
66
67
68
69
# File 'lib/llm/schema/leaf.rb', line 63

def default(value = nil)
  if value.nil?
    resolve(@default)
  else
    tap { @default = value }
  end
end

#enum(*values) ⇒ LLM::Schema::Leaf

Set the allowed values of a leaf

Parameters:

  • values (Array) —

    The allowed values

Returns:

See Also:



76
77
78
79
80
81
82
# File 'lib/llm/schema/leaf.rb', line 76

def enum(*values)
  if values.empty?
    @enum
  else
    tap { @enum = values }
  end
end

#const(value = nil) ⇒ LLM::Schema::Leaf

Set the value of a leaf to be a constant value

Parameters:

  • value (Object) (defaults to: nil) —

    The constant value

Returns:

See Also:



89
90
91
92
93
94
95
# File 'lib/llm/schema/leaf.rb', line 89

def const(value = nil)
  if value.nil?
    @const
  else
    tap { @const = value }
  end
end

#required(value = true) ⇒ LLM::Schema::Leaf

Mark a leaf as required

Parameters:

  • value (Boolean) (defaults to: true) —

    When false, marks the leaf as optional

Returns:



102
103
104
# File 'lib/llm/schema/leaf.rb', line 102

def required(value = true)
  tap { @required = value }
end

#required? ⇒ Boolean

Returns:



108
109
110
# File 'lib/llm/schema/leaf.rb', line 108

def required?
  @required
end

#optional ⇒ LLM::Schema::Leaf

Mark a leaf as optional

Returns:



115
116
117
# File 'lib/llm/schema/leaf.rb', line 115

def optional
  tap { @required = false }
end

#optional? ⇒ Boolean

Returns:



121
122
123
# File 'lib/llm/schema/leaf.rb', line 121

def optional?
  !@required
end

#to_h ⇒ Hash

Returns:

  • (Hash)


127
128
129
# File 'lib/llm/schema/leaf.rb', line 127

def to_h
  {description: @description, default:, enum: @enum, const: @const}.compact
end

#to_json(options = {}) ⇒ String

Returns:



133
134
135
# File 'lib/llm/schema/leaf.rb', line 133

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

#to_s ⇒ String

Returns:



139
140
141
# File 'lib/llm/schema/leaf.rb', line 139

def to_s
  LLM::Schema::Renderer.render(self)
end

#==(other) ⇒ Boolean Also known as: eql?

Parameters:

Returns:



147
148
149
150
# File 'lib/llm/schema/leaf.rb', line 147

def ==(other)
  return false unless self.class === other
  to_h == other.to_h
end