Class: LLM::Schema::Leaf
- Inherits:
-
Object
- Object
- LLM::Schema::Leaf
- 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
- #hash ⇒ Integer
- #owner=(owner)
-
#resolve(value) ⇒ Object
Resolves a configured value against the owner, following Utils#resolve_option.
-
#description(str = nil) ⇒ LLM::Schema::Leaf
Set the description of a leaf.
-
#default(value = nil) ⇒ LLM::Schema::Leaf
Set the default value of a leaf.
-
#enum(*values) ⇒ LLM::Schema::Leaf
Set the allowed values of a leaf.
-
#const(value = nil) ⇒ LLM::Schema::Leaf
Set the value of a leaf to be a constant value.
-
#required(value = true) ⇒ LLM::Schema::Leaf
Mark a leaf as required.
- #required? ⇒ Boolean
-
#optional ⇒ LLM::Schema::Leaf
Mark a leaf as optional.
- #optional? ⇒ Boolean
- #to_h ⇒ Hash
- #to_json(options = {}) ⇒ String
- #to_s ⇒ String
- #==(other) ⇒ Boolean (also: #eql?)
-
#initialize ⇒ LLM::Schema::Leaf
constructor
Returns a leaf.
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
Instance Method Details
#owner=(owner)
This method returns an undefined value.
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.
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
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
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
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
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
102 103 104 |
# File 'lib/llm/schema/leaf.rb', line 102 def required(value = true) tap { @required = value } end |
#required? ⇒ Boolean
108 109 110 |
# File 'lib/llm/schema/leaf.rb', line 108 def required? @required end |
#optional ⇒ LLM::Schema::Leaf
Mark a leaf as optional
115 116 117 |
# File 'lib/llm/schema/leaf.rb', line 115 def optional tap { @required = false } end |
#optional? ⇒ Boolean
121 122 123 |
# File 'lib/llm/schema/leaf.rb', line 121 def optional? !@required end |
#to_h ⇒ 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
133 134 135 |
# File 'lib/llm/schema/leaf.rb', line 133 def to_json( = {}) to_h.to_json() end |
#to_s ⇒ String
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?
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 |