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 Attribute Summary collapse
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
- #to_s ⇒ String
Constructor Details
#initialize ⇒ Leaf
Returns a new instance of Leaf.
15 16 17 18 19 20 21 22 |
# File 'lib/llm/schema/leaf.rb', line 15 def initialize @description = nil @default = nil @enum = nil @required = nil @const = nil @index = nil end |
Instance Attribute Details
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
122 123 124 125 |
# File 'lib/llm/schema/leaf.rb', line 122 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
66 67 68 69 70 71 72 |
# File 'lib/llm/schema/leaf.rb', line 66 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
40 41 42 43 44 45 46 |
# File 'lib/llm/schema/leaf.rb', line 40 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
28 29 30 31 32 33 34 |
# File 'lib/llm/schema/leaf.rb', line 28 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
53 54 55 56 57 58 59 |
# File 'lib/llm/schema/leaf.rb', line 53 def enum(*values) if values.empty? @enum else tap { @enum = values } end end |
#optional ⇒ LLM::Schema::Leaf
Mark a leaf as optional
90 91 92 |
# File 'lib/llm/schema/leaf.rb', line 90 def optional tap { @required = false } end |
#required ⇒ LLM::Schema::Leaf
Mark a leaf as required
77 78 79 |
# File 'lib/llm/schema/leaf.rb', line 77 def required tap { @required = true } end |
#to_h ⇒ Hash
102 103 104 |
# File 'lib/llm/schema/leaf.rb', line 102 def to_h {description: @description, default: @default, enum: @enum, const: @const}.compact end |
#to_json(options = {}) ⇒ String
108 109 110 |
# File 'lib/llm/schema/leaf.rb', line 108 def to_json( = {}) to_h.to_json() end |