Module: Ollama::DTO::ClassMethods

Defined in:
lib/ollama/dto.rb

Overview

Class-level helpers for DTO attribute tracking.

Instance Method Summary collapse

Instance Method Details

#attr_accessor(*names) ⇒ Object

The attr_accessor method extends the functionality of the standard attr_accessor by also registering the declared attributes in the class’s attributes set.

Parameters:

  • names (Array<Symbol>)

    one or more attribute names to be declared as readable and registered



72
73
74
75
# File 'lib/ollama/dto.rb', line 72

def attr_accessor(*names)
  super
  attributes.merge(names.map(&:to_sym))
end

#attr_reader(*names) ⇒ Object

The attr_reader method extends the functionality of the standard attr_reader by also registering the declared attributes in the class’s attributes set.

Parameters:

  • names (Array<Symbol>)

    one or more attribute names to be declared as readable and registered



61
62
63
64
# File 'lib/ollama/dto.rb', line 61

def attr_reader(*names)
  super
  attributes.merge(names.map(&:to_sym))
end

#attributesSet

The attributes accessor reads and writes the attributes instance variable.

Returns:

  • (Set)

    the set of attributes stored in the instance variable



34
35
36
# File 'lib/ollama/dto.rb', line 34

def attributes
  @attributes ||= Set.new
end

#attributes=(value) ⇒ Object



38
39
40
# File 'lib/ollama/dto.rb', line 38

def attributes=(value)
  @attributes = value
end

#from_hash(hash) ⇒ self

The from_hash method creates a new instance of the class by converting a hash into keyword arguments.

This method is typically used to instantiate objects from JSON data or other hash-based sources, transforming the hash keys to symbols and passing them as keyword arguments to the constructor.

Parameters:

  • hash (Hash)

    a hash containing the attributes for the new instance

Returns:

  • (self)

    a new instance of the class initialized with the hash data



51
52
53
# File 'lib/ollama/dto.rb', line 51

def from_hash(hash)
  new(**hash.transform_keys(&:to_sym))
end