Module: Ollama::DTO

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 for various command and data structures used in communicating with the Ollama API.

Examples:

Using DTO functionality in a command class

class MyCommand
  include Ollama::DTO
  attr_reader :name, :value
  def initialize(name:, value:)
    @name, @value = name, value
  end
end

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



24
25
26
27
# File 'lib/ollama/dto.rb', line 24

def self.included(base)
  base.extend(ClassMethods)
  base.instance_variable_set(:@attributes, Set.new)
end

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.

Parameters:

  • other (Object)

    the object to compare against

Returns:

  • (TrueClass, FalseClass)

    true if both objects have identical JSON representations, false otherwise



154
155
156
157
158
# File 'lib/ollama/dto.rb', line 154

def ==(other)
  return false unless other.is_a?(self.class)

  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.

Parameters:

  • obj (Object)

    the object to be converted to an array

Returns:

  • (Array, nil)

    an array containing the object or its elements, or nil if the input is nil



116
117
118
119
120
121
122
123
124
# File 'lib/ollama/dto.rb', line 116

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.

Parameters:

  • obj (Object)

    the object to be converted

Returns:

  • (Array<Hash>, nil)

    an array of hashes if the conversion was possible, or nil otherwise



87
88
89
90
91
92
93
# File 'lib/ollama/dto.rb', line 87

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.

Parameters:

  • obj (Object)

    the object to be converted to a hash

Returns:

  • (Hash, nil)

    the hash representation of the object or nil if the object does not respond to to_hash



103
104
105
# File 'lib/ollama/dto.rb', line 103

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.

Parameters:

  • _ignored (Array)

    ignored arguments

Returns:

  • (Hash)

    a hash containing the object’s non-nil and non-empty attributes



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/ollama/dto.rb', line 134

def as_json(*_ignored)
  self.class.attributes.each_with_object({}) do |attr, hash|
    value = send(attr)
    next if value.nil?

    # Check if it's an empty collection (responds to size and size is 0)
    next if value.respond_to?(:size) && value.empty?

    hash[attr] = value
  end
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.

Returns:

  • (TrueClass, FalseClass)

    true if the object has no attributes, false otherwise



170
171
172
# File 'lib/ollama/dto.rb', line 170

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.

Parameters:

  • args (Array)

    pass-through args

Returns:

  • (String)

    a JSON string representation of the object



183
184
185
# File 'lib/ollama/dto.rb', line 183

def to_json(*args)
  as_json.to_json(*args)
end