Module: Ollama::DTO
- Extended by:
- Tins::Concern
- Included in:
- Commands::Chat, Commands::Copy, Commands::Create, Commands::Delete, Commands::Embed, Commands::Embeddings, Commands::Generate, Commands::Pull, Commands::Push, Commands::Show, Message, Options, Tool, Tool::Function, Tool::Function::Parameters, Tool::Function::Parameters::Property
- Defined in:
- lib/ollama/dto.rb
Overview
A module that provides a foundation for data transfer objects (DTOs) within the Ollama library.
The DTO module includes common functionality for converting objects to and from JSON, handling attribute management, and providing utility methods for processing arrays and hashes. It serves as a base class for various command and data structures used in communicating with the Ollama API.
Instance Method Summary collapse
-
#==(other) ⇒ TrueClass, FalseClass
The == method compares two objects for equality based on their JSON representation.
-
#as_array(obj) ⇒ Array?
The as_array method converts an object into an array representation.
-
#as_array_of_hashes(obj) ⇒ Array<Hash>?
The as_array_of_hashes method converts an object into an array of hashes.
-
#as_hash(obj) ⇒ Hash?
The as_hash method converts an object to a hash representation.
-
#as_json(*ignored) ⇒ Hash
(also: #to_hash)
The as_json method converts the object's attributes into a JSON-compatible hash.
-
#empty? ⇒ TrueClass, FalseClass
The empty? method checks whether the object has any attributes defined.
-
#to_json(*args) ⇒ String
The to_json method converts the object's JSON representation into a JSON string format.
Instance Method Details
#==(other) ⇒ TrueClass, FalseClass
The == method compares two objects for equality based on their JSON representation.
This method checks if the JSON representation of the current object is equal to the JSON representation of another object.
representations, false otherwise
130 131 132 |
# File 'lib/ollama/dto.rb', line 130 def ==(other) as_json == other.as_json end |
#as_array(obj) ⇒ Array?
The as_array method converts an object into an array representation.
If the object is nil, it returns nil. If the object responds to to_ary, it calls to_ary and returns the result. Otherwise, it wraps the object in an array and returns it.
nil if the input is nil
98 99 100 101 102 103 104 105 106 |
# File 'lib/ollama/dto.rb', line 98 def as_array(obj) if obj.nil? obj elsif obj.respond_to?(:to_ary) obj.to_ary else [ obj ] end end |
#as_array_of_hashes(obj) ⇒ Array<Hash>?
The as_array_of_hashes method converts an object into an array of hashes.
If the object responds to to_hash, it wraps the result in an array. If the object responds to to_ary, it maps each element to a hash and returns the resulting array.
possible, or nil otherwise
67 68 69 70 71 72 73 |
# File 'lib/ollama/dto.rb', line 67 def as_array_of_hashes(obj) if obj.respond_to?(:to_hash) [ obj.to_hash ] elsif obj.respond_to?(:to_ary) obj.to_ary.map(&:to_hash) end end |
#as_hash(obj) ⇒ Hash?
The as_hash method converts an object to a hash representation.
If the object responds to to_hash, it returns the result of that method call. If the object does not respond to to_hash, it returns nil.
object does not respond to to_hash
84 85 86 |
# File 'lib/ollama/dto.rb', line 84 def as_hash(obj) obj&.to_hash end |
#as_json(*ignored) ⇒ Hash Also known as: to_hash
The as_json method converts the object's attributes into a JSON-compatible hash.
This method gathers all defined attributes of the object and constructs a hash representation, excluding any nil values or empty collections.
116 117 118 119 |
# File 'lib/ollama/dto.rb', line 116 def as_json(*ignored) self.class.attributes.each_with_object({}) { |a, h| h[a] = send(a) }. reject { _2.nil? || _2.ask_and_send(:size) == 0 } end |
#empty? ⇒ TrueClass, FalseClass
The empty? method checks whether the object has any attributes defined.
This method determines if the object contains no attributes by checking if its hash representation is empty. It is typically used to verify if an object, such as a DTO, has been initialized with any values.
false otherwise
144 145 146 |
# File 'lib/ollama/dto.rb', line 144 def empty? to_hash.empty? end |
#to_json(*args) ⇒ String
The to_json method converts the object's JSON representation into a JSON string format.
This method utilizes the object's existing as_json representation and applies the standard JSON serialization to produce a formatted JSON string output.
157 158 159 |
# File 'lib/ollama/dto.rb', line 157 def to_json(*args) as_json.to_json(*args) end |