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.
Instance Method Summary collapse
- #==(other) ⇒ Boolean (also: #eql?)
-
#const(value = nil) ⇒ LLM::Schema::Leaf
Set the value of a leaf to be a constant value.
-
#default(value = nil) ⇒ LLM::Schema::Leaf
Set the default value of a leaf.
-
#description(str = nil) ⇒ LLM::Schema::Leaf
Set the description of a leaf.
-
#enum(*values) ⇒ LLM::Schema::Leaf
Set the allowed values of a leaf.
-
#initialize ⇒ Leaf
constructor
A new instance of Leaf.
-
#optional ⇒ LLM::Schema::Leaf
Mark a leaf as optional.
- #optional? ⇒ Boolean
-
#required ⇒ LLM::Schema::Leaf
Mark a leaf as required.
- #required? ⇒ Boolean
- #to_h ⇒ Hash
- #to_json(options = {}) ⇒ String
Constructor Details
#initialize ⇒ Leaf
Returns a new instance of Leaf.
11 12 13 14 15 16 17 |
# File 'lib/llm/schema/leaf.rb', line 11 def initialize @description = nil @default = nil @enum = nil @required = nil @const = nil end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
111 112 113 114 |
# File 'lib/llm/schema/leaf.rb', line 111 def ==(other) return false unless self.class === other to_h == other.to_h end |
#const(value = nil) ⇒ LLM::Schema::Leaf
Set the value of a leaf to be a constant value
61 62 63 64 65 66 67 |
# File 'lib/llm/schema/leaf.rb', line 61 def const(value = nil) if value.nil? @const else tap { @const = value } end end |
#default(value = nil) ⇒ LLM::Schema::Leaf
Set the default value of a leaf
35 36 37 38 39 40 41 |
# File 'lib/llm/schema/leaf.rb', line 35 def default(value = nil) if value.nil? @default else tap { @default = value } end end |
#description(str = nil) ⇒ LLM::Schema::Leaf
Set the description of a leaf
23 24 25 26 27 28 29 |
# File 'lib/llm/schema/leaf.rb', line 23 def description(str = nil) if str.nil? @description else tap { @description = str } end end |
#enum(*values) ⇒ LLM::Schema::Leaf
Set the allowed values of a leaf
48 49 50 51 52 53 54 |
# File 'lib/llm/schema/leaf.rb', line 48 def enum(*values) if values.empty? @enum else tap { @enum = values } end end |
#optional ⇒ LLM::Schema::Leaf
Mark a leaf as optional
85 86 87 |
# File 'lib/llm/schema/leaf.rb', line 85 def optional tap { @required = false } end |
#required ⇒ LLM::Schema::Leaf
Mark a leaf as required
72 73 74 |
# File 'lib/llm/schema/leaf.rb', line 72 def required tap { @required = true } end |
#to_h ⇒ Hash
97 98 99 |
# File 'lib/llm/schema/leaf.rb', line 97 def to_h {description: @description, default: @default, enum: @enum}.compact end |
#to_json(options = {}) ⇒ String
103 104 105 |
# File 'lib/llm/schema/leaf.rb', line 103 def to_json( = {}) to_h.to_json() end |