Class: LLM::Object
- Inherits:
-
BasicObject
- Extended by:
- Builder
- Includes:
- Enumerable, Kernel
- 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.
Defined Under Namespace
Modules: Builder, Kernel
Constant Summary
Constants included
from Kernel
Kernel::TypeError
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Builder
from
Methods included from Kernel
#class, #extend, #inspect, #instance_of?, #kind_of?, #method, #object_id, #pretty_print, #raise, #respond_to?, #respond_to_missing?, #tap
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
212
213
214
215
216
217
218
219
220
|
# File 'lib/llm/object.rb', line 212
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?
204
205
206
207
|
# File 'lib/llm/object.rb', line 204
def ==(other)
return false unless other.respond_to?(:to_h)
to_h == other.to_h || to_hash == other.to_h
end
|
74
75
76
|
# File 'lib/llm/object.rb', line 74
def [](k)
@h[SINGLETON.key(@h, k)]
end
|
#[]=(k, v) ⇒ void
This method returns an undefined value.
82
83
84
|
# File 'lib/llm/object.rb', line 82
def []=(k, v)
@h[k.to_s] = v
end
|
#delete(k = UNDEFINED) ⇒ void
This method returns an undefined value.
169
170
171
172
|
# File 'lib/llm/object.rb', line 169
def delete(k = UNDEFINED)
return SINGLETON.get(@h, :delete) if k.equal?(UNDEFINED)
@h.delete(SINGLETON.key(@h, k))
end
|
#dig(*args) ⇒ Object?
189
190
191
192
|
# File 'lib/llm/object.rb', line 189
def dig(*args)
return SINGLETON.get(@h, :dig) if args.empty?
@h.dig(*args)
end
|
#each {|k, v| ... } ⇒ void
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
|
#each_pair {|| ... } ⇒ Object
183
184
185
|
# File 'lib/llm/object.rb', line 183
def each_pair(&)
@h.each(&)
end
|
#empty? ⇒ Boolean
94
95
96
|
# File 'lib/llm/object.rb', line 94
def empty?
@h.empty?
end
|
#fetch(k = UNDEFINED, *args, &b) ⇒ Object
134
135
136
137
138
|
# File 'lib/llm/object.rb', line 134
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
|
#key?(k = UNDEFINED) ⇒ Boolean
Also known as:
has_key?
125
126
127
128
|
# File 'lib/llm/object.rb', line 125
def key?(k = UNDEFINED)
return SINGLETON.get(@h, :key?) if k.equal?(UNDEFINED)
@h.key?(SINGLETON.key(@h, k))
end
|
#keys ⇒ Array<String>
112
113
114
|
# File 'lib/llm/object.rb', line 112
def keys
@h.keys
end
|
#merge(other = UNDEFINED) ⇒ LLM::Object
Returns a new LLM::Object
145
146
147
148
149
150
|
# File 'lib/llm/object.rb', line 145
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
157
158
159
160
161
162
163
|
# File 'lib/llm/object.rb', line 157
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
@h.merge!(other)
self
end
|
#size ⇒ Integer
Also known as:
length
176
177
178
|
# File 'lib/llm/object.rb', line 176
def size
@h.size
end
|
#slice(*args) ⇒ Hash
196
197
198
199
|
# File 'lib/llm/object.rb', line 196
def slice(*args)
return SINGLETON.get(@h, :slice) if args.empty?
@h.slice(*args)
end
|
#to_h ⇒ Hash
100
101
102
|
# File 'lib/llm/object.rb', line 100
def to_h
@h.dup
end
|
#to_hash ⇒ Hash
106
107
108
|
# File 'lib/llm/object.rb', line 106
def to_hash
@h.transform_keys(&:to_sym)
end
|
#to_json ⇒ String
88
89
90
|
# File 'lib/llm/object.rb', line 88
def to_json(...)
LLM.json.dump(to_h, ...)
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
|
#values ⇒ Array
118
119
120
|
# File 'lib/llm/object.rb', line 118
def values
@h.values
end
|