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
181
182
183
184
185
186
187
188
189
|
# File 'lib/llm/object.rb', line 181
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
66
67
68
|
# File 'lib/llm/object.rb', line 66
def [](k)
@h[SINGLETON.key(@h, k)]
end
|
#[]=(k, v) ⇒ void
This method returns an undefined value.
74
75
76
|
# File 'lib/llm/object.rb', line 74
def []=(k, v)
@h[k.to_s] = v
end
|
#delete(k = UNDEFINED) ⇒ void
This method returns an undefined value.
147
148
149
150
|
# File 'lib/llm/object.rb', line 147
def delete(k = UNDEFINED)
return SINGLETON.get(@h, :delete) if k.equal?(UNDEFINED)
@h.delete(SINGLETON.key(@h, k))
end
|
#dig(*args) ⇒ Object?
167
168
169
170
|
# File 'lib/llm/object.rb', line 167
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
161
162
163
|
# File 'lib/llm/object.rb', line 161
def each_pair(&)
@h.each(&)
end
|
#empty? ⇒ Boolean
86
87
88
|
# File 'lib/llm/object.rb', line 86
def empty?
@h.empty?
end
|
#fetch(k = UNDEFINED, *args, &b) ⇒ Object
126
127
128
129
|
# File 'lib/llm/object.rb', line 126
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?
117
118
119
120
|
# File 'lib/llm/object.rb', line 117
def key?(k = UNDEFINED)
return SINGLETON.get(@h, :key?) if k.equal?(UNDEFINED)
@h.key?(SINGLETON.key(@h, k))
end
|
#keys ⇒ Array<String>
104
105
106
|
# File 'lib/llm/object.rb', line 104
def keys
@h.keys
end
|
#merge(other = UNDEFINED) ⇒ LLM::Object
Returns a new LLM::Object
136
137
138
139
140
141
|
# File 'lib/llm/object.rb', line 136
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
|
#size ⇒ Integer
Also known as:
length
154
155
156
|
# File 'lib/llm/object.rb', line 154
def size
@h.size
end
|
#slice(*args) ⇒ Hash
174
175
176
177
|
# File 'lib/llm/object.rb', line 174
def slice(*args)
return SINGLETON.get(@h, :slice) if args.empty?
@h.slice(*args)
end
|
#to_h ⇒ Hash
92
93
94
|
# File 'lib/llm/object.rb', line 92
def to_h
@h.dup
end
|
#to_hash ⇒ Hash
98
99
100
|
# File 'lib/llm/object.rb', line 98
def to_hash
@h.transform_keys(&:to_sym)
end
|
#to_json ⇒ String
80
81
82
|
# File 'lib/llm/object.rb', line 80
def to_json(...)
LLM.json.dump(to_h, ...)
end
|
#values ⇒ Array
110
111
112
|
# File 'lib/llm/object.rb', line 110
def values
@h.values
end
|