module Ollama::DTO
def ==(other)
-
(TrueClass, FalseClass)- true if both objects have identical JSON
Parameters:
-
other(Object) -- the object to compare against
def ==(other) as_json == other.as_json end
def as_array(obj)
-
(Array, nil)- an array containing the object or its elements, or
Parameters:
-
obj(Object) -- the object to be converted to an array
def as_array(obj) if obj.nil? obj elsif obj.respond_to?(:to_ary) obj.to_ary else [ obj ] end end
def as_array_of_hashes(obj)
-
(Array- an array of hashes if the conversion was, nil)
Parameters:
-
obj(Object) -- the object to be converted
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
def as_hash(obj)
-
(Hash, nil)- the hash representation of the object or nil if the
Parameters:
-
obj(Object) -- the object to be converted to a hash
def as_hash(obj) obj&.to_hash end
def as_json(*ignored)
-
(Hash)- a hash containing the object's non-nil and non-empty attributes
Parameters:
-
ignored(Array) -- ignored arguments
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
def attr_accessor(*names)
-
names(Array) -- one or more attribute names to be declared
def attr_accessor(*names) super attributes.merge(names.map(&:to_sym)) end
def attr_reader(*names)
-
names(Array) -- one or more attribute names to be declared
def attr_reader(*names) super attributes.merge(names.map(&:to_sym)) end
def empty?
-
(TrueClass, FalseClass)- true if the object has no attributes,
def empty? to_hash.empty? end
def from_hash(hash)
-
(self)- a new instance of the class initialized with the hash data
Parameters:
-
hash(Hash) -- a hash containing the attributes for the new instance
def from_hash(hash) new(**hash.transform_keys(&:to_sym)) end
def to_json(*args)
-
(String)- a JSON string representation of the object
Parameters:
-
args(Array) -- pass-through args
def to_json(*args) as_json.to_json(*args) end