Class: LLM::Object
- Inherits:
-
BasicObject
- Includes:
- Enumerable
- Defined in:
- lib/llm/object.rb,
lib/llm/object/kernel.rb,
lib/llm/object/builder.rb
Overview
The LLM::Object class encapsulates a Hash object. It is
similar in spirit to OpenStruct, and it was introduced after OpenStruct
became a bundled gem rather than a default gem in Ruby 3.5.
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
50
51
52
|
# File 'lib/llm/object.rb', line 50
def initialize(h = {})
@h = h || {}
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &b) ⇒ Object
224
225
226
227
228
229
230
231
232
|
# File 'lib/llm/object.rb', line 224
def method_missing(m, *args, &b)
if m.to_s.end_with?("=")
self[m[0..-2]] = args.first
elsif k = SINGLETON.key(@h, m)
@h[k]
else
nil
end
end
|
Class Method Details
.get(h, k) ⇒ Object?
This method is part of a private API.
You should avoid using this method if possible, as it may be removed or be changed in the future.
37
38
39
40
|
# File 'lib/llm/object.rb', line 37
def self.get(h, k)
name = key(h, k)
h[name] if name
end
|
.key(h, k) ⇒ String, ...
This method is part of a private API.
You should avoid using this method if possible, as it may be removed or be changed in the future.
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/llm/object.rb', line 21
def self.key(h, k)
return nil if k.nil?
if h.key?(k.to_s)
k.to_s
elsif h.key?(k.to_sym)
k.to_sym
else
nil
end
end
|
Instance Method Details
#==(other) ⇒ Boolean
Also known as:
eql?
216
217
218
219
|
# File 'lib/llm/object.rb', line 216
def ==(other)
return false unless other.respond_to?(:to_h)
to_h == other.to_h || to_hash == other.to_h
end
|
#each {|k, v| ... }
This method returns an undefined value.
Yields a key|value pair to a block.
59
60
61
|
# File 'lib/llm/object.rb', line 59
def each(&)
@h.each(&)
end
|
In-place transform of values with a block.
67
68
69
|
# File 'lib/llm/object.rb', line 67
def transform_values!(&)
@h.transform_values!(&)
end
|
74
75
76
|
# File 'lib/llm/object.rb', line 74
def [](k)
@h[SINGLETON.key(@h, k)]
end
|
#[]=(k, v)
This method returns an undefined value.
82
83
84
85
86
87
88
|
# File 'lib/llm/object.rb', line 82
def []=(k, v)
if frozen?
raise ::FrozenError, "can't modify frozen object"
else
@h[k.to_s] = v
end
end
|
#to_json ⇒ String
92
93
94
|
# File 'lib/llm/object.rb', line 92
def to_json(...)
LLM.json.dump(to_h, ...)
end
|
#empty? ⇒ Boolean
98
99
100
|
# File 'lib/llm/object.rb', line 98
def empty?
@h.empty?
end
|
#to_h ⇒ Hash
104
105
106
|
# File 'lib/llm/object.rb', line 104
def to_h
@h.dup
end
|
#to_hash ⇒ Hash
110
111
112
|
# File 'lib/llm/object.rb', line 110
def to_hash
@h.transform_keys(&:to_sym)
end
|
#keys ⇒ Array<String>
116
117
118
|
# File 'lib/llm/object.rb', line 116
def keys
@h.keys
end
|
#values ⇒ Array
122
123
124
|
# File 'lib/llm/object.rb', line 122
def values
@h.values
end
|
#key?(k = UNDEFINED) ⇒ Boolean
Also known as:
has_key?
129
130
131
132
|
# File 'lib/llm/object.rb', line 129
def key?(k = UNDEFINED)
return SINGLETON.get(@h, :key?) if k.equal?(UNDEFINED)
@h.key?(SINGLETON.key(@h, k))
end
|
#fetch(k = UNDEFINED, *args, &b) ⇒ Object
138
139
140
141
142
|
# File 'lib/llm/object.rb', line 138
def fetch(k = UNDEFINED, *args, &b)
return SINGLETON.get(@h, :fetch) if k.equal?(UNDEFINED)
key = SINGLETON.key(@h, k)
@h.fetch(key || k, *args, &b)
end
|
#merge(other = UNDEFINED) ⇒ LLM::Object
Returns a new LLM::Object
149
150
151
152
153
154
|
# File 'lib/llm/object.rb', line 149
def merge(other = UNDEFINED)
return SINGLETON.get(@h, :merge) if other.equal?(UNDEFINED)
other = ::Hash.try_convert(other)
raise TypeError, "#{other} cannot be coerced into a Hash" unless other
SINGLETON.from @h.merge(other)
end
|
#merge!(other = UNDEFINED) ⇒ LLM::Object
161
162
163
164
165
166
167
168
169
170
171
|
# File 'lib/llm/object.rb', line 161
def merge!(other = UNDEFINED)
if frozen?
raise ::FrozenError, "can't modify frozen object"
else
return SINGLETON.get(@h, :merge!) if other.equal?(UNDEFINED)
other = ::Hash.try_convert(other)
raise TypeError, "#{other} cannot be coerced into a Hash" unless other
@h.merge!(other)
self
end
end
|
#delete(k = UNDEFINED)
This method returns an undefined value.
177
178
179
180
181
182
183
184
|
# File 'lib/llm/object.rb', line 177
def delete(k = UNDEFINED)
if frozen?
raise ::FrozenError, "can't modify frozen object"
else
return SINGLETON.get(@h, :delete) if k.equal?(UNDEFINED)
@h.delete(SINGLETON.key(@h, k))
end
end
|
#size ⇒ Integer
Also known as:
length
188
189
190
|
# File 'lib/llm/object.rb', line 188
def size
@h.size
end
|
#each_pair {|| ... } ⇒ Object
195
196
197
|
# File 'lib/llm/object.rb', line 195
def each_pair(&)
@h.each(&)
end
|
#dig(*args) ⇒ Object?
201
202
203
204
|
# File 'lib/llm/object.rb', line 201
def dig(*args)
return SINGLETON.get(@h, :dig) if args.empty?
@h.dig(*args)
end
|
#slice(*args) ⇒ Hash
208
209
210
211
|
# File 'lib/llm/object.rb', line 208
def slice(*args)
return SINGLETON.get(@h, :slice) if args.empty?
@h.slice(*args)
end
|