Module: LLM::Utils
Overview
Shared utility methods used across the runtime.
Instance Method Summary collapse
-
#name_of(obj) ⇒ String?
Returns the Ruby module or class name for an object.
-
#normalize_base_path(path) ⇒ String
Normalizes an HTTP API base path.
-
#object_id(obj) ⇒ String
Renders the class-and-object-id portion of an inspect string.
-
#record?(obj) ⇒ Boolean
private
Returns true when
objis an ActiveRecord or Sequel model. -
#resolve_option(obj, option, resolve_symbol: true) ⇒ Object
Resolves a configured option against an object instance.
Instance Method Details
#name_of(obj) ⇒ String?
Returns the Ruby module or class name for an object.
This bypasses overridden #name implementations by binding
Module#name directly.
74 75 76 |
# File 'lib/llm/utils.rb', line 74 def name_of(obj) ::Module.instance_method(:name).bind(obj).call end |
#normalize_base_path(path) ⇒ String
Normalizes an HTTP API base path.
Blank paths normalize to an empty string. Non-empty paths are prefixed with a leading slash and stripped of trailing slashes.
59 60 61 62 63 64 |
# File 'lib/llm/utils.rb', line 59 def normalize_base_path(path) path = path.to_s.strip return "" if path.empty? || path == "/" path = "/#{path}" unless path.start_with?("/") path.sub(%r{/+\z}, "") end |
#object_id(obj) ⇒ String
Renders the class-and-object-id portion of an inspect string.
This returns strings like LLM::Tool:0x1234abcd, which can be
embedded into custom inspect output.
86 87 88 89 90 91 92 93 |
# File 'lib/llm/utils.rb', line 86 def object_id(obj) klass = if Class === obj name_of(obj) || name_of(obj.superclass) || obj.class.name else obj.class.name || obj.class.to_s end "#{klass}:0x#{obj.object_id.to_s(16)}" end |
#record?(obj) ⇒ Boolean
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.
Returns true when obj is an ActiveRecord or Sequel model.
46 47 48 49 |
# File 'lib/llm/utils.rb', line 46 def record?(obj) (defined?(::ActiveRecord::Base) and ::ActiveRecord::Base === obj) or (defined?(::Sequel::Model) and ::Sequel::Model === obj) end |
#resolve_option(obj, option, resolve_symbol: true) ⇒ Object
Resolves a configured option against an object instance.
Proc values are evaluated with instance_exec, symbol values are
optionally sent to the object as method calls, hashes are duplicated,
and all other values are returned as-is.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/llm/utils.rb', line 20 def resolve_option(obj, option, resolve_symbol: true) case option when Proc then obj.instance_exec(&option) when Symbol if resolve_symbol if obj.respond_to?(option, true) and record?(obj) obj.send(option) elsif obj.respond_to?(:record) and obj.record.respond_to?(option, true) obj.record.send(option) elsif obj.respond_to?(option, true) obj.send(option) else raise ArgumentError, "unable to resolve #{option}" end else option end when Hash then option.dup else option end end |