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
211
212
213
214
215
216
217
218
219
|
# File 'lib/llm/object.rb', line 211
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?
203
204
205
206
|
# File 'lib/llm/object.rb', line 203
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.
168
169
170
171
|
# File 'lib/llm/object.rb', line 168
def delete(k = UNDEFINED)
return SINGLETON.get(@h, :delete) if k.equal?(UNDEFINED)
@h.delete(SINGLETON.key(@h, k))
end
|
#dig(*args) ⇒ Object?
188
189
190
191
|
# File 'lib/llm/object.rb', line 188
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
182
183
184
|
# File 'lib/llm/object.rb', line 182
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
|
# File 'lib/llm/object.rb', line 134
def fetch(k = UNDEFINED, *args, &b)
return SINGLETON.get(@h, :fetch) if k.equal?(UNDEFINED)
@h.fetch(SINGLETON.key(@h, 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
144
145
146
147
148
149
|
# File 'lib/llm/object.rb', line 144
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
156
157
158
159
160
161
162
|
# File 'lib/llm/object.rb', line 156
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
175
176
177
|
# File 'lib/llm/object.rb', line 175
def size
@h.size
end
|
#slice(*args) ⇒ Hash
195
196
197
198
|
# File 'lib/llm/object.rb', line 195
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
|