Module: Familia::Horreum::ManagementMethods

Includes:
RelatedFieldsManagement
Defined in:
lib/familia/horreum/management.rb

Overview

ManagementMethods - Class-level methods for Horreum model management

This module is extended into classes that include Familia::Horreum, providing class methods for database operations and object management (e.g., Customer.create, Customer.find_by_id)

Key features:

  • Includes RelatedFieldsManagement for DataType field handling
  • Provides utility methods for working with Database objects

Instance Method Summary collapse

Methods included from RelatedFieldsManagement

#attach_class_related_field, #attach_instance_related_field

Instance Method Details

#all(suffix = nil) ⇒ Object



301
302
303
304
305
# File 'lib/familia/horreum/management.rb', line 301

def all(suffix = nil)
  suffix ||= self.suffix
  # objects that could not be parsed will be nil
  keys(suffix).filter_map { |k| find_by_key(k) }
end

#any?(filter = '*') ⇒ Boolean

Returns:

  • (Boolean)


307
308
309
# File 'lib/familia/horreum/management.rb', line 307

def any?(filter = '*')
  matching_keys_count(filter).positive?
end

#config_nameString

Converts the class name into a string that can be used to look up configuration values. This is particularly useful when mapping familia models with specific database numbers in the configuration.

Familia::Horreum::DefinitionMethods#config_name

Examples:

V2::Session.config_name => 'session'

Returns:

  • (String)

    The underscored class name as a string



87
88
89
90
91
# File 'lib/familia/horreum/management.rb', line 87

def config_name
  return nil if name.nil?

  name.demodularize.snake_case
end

#create! {|hobj| ... } ⇒ Object

Note:

The behavior of this method depends on the implementation of #new,

exists?, and #save in the class and its superclasses.

Creates and persists a new instance of the class.

This method serves as a factory method for creating and persisting new instances of the class. It combines object instantiation, existence checking, and persistence in a single operation.

The method is flexible in accepting both positional and keyword arguments:

  • Positional arguments (*args) are passed directly to the constructor.
  • Keyword arguments (**kwargs) are passed as a hash to the constructor.

After instantiation, the method checks if an object with the same identifier already exists. If it does, a Familia::RecordExistsError exception is raised to prevent overwriting existing data.

Finally, the method saves the new instance returns it.

Examples:

Creating an object with keyword arguments

User.create(name: "John", age: 30)

Creating an object with positional and keyword arguments (not recommended)

Product.create("SKU123", name: "Widget", price: 9.99)

Parameters:

  • args (Array)

    Variable number of positional arguments to be passed to the constructor.

  • kwargs (Hash)

    Keyword arguments to be passed to the constructor.

Yields:

  • (hobj)

Returns:

  • (Object)

    The newly created and persisted instance.

Raises:

See Also:



55
56
57
58
59
60
61
62
63
64
# File 'lib/familia/horreum/management.rb', line 55

def create!(...)
  hobj = new(...)
  hobj.save_if_not_exists!

  # If a block is given, yield the created object
  # This allows for additional operations on successful creation
  yield hobj if block_given?

  hobj
end

#dbkey(identifier, suffix = self.suffix) ⇒ Object

+identifier+ can be a value or an Array of values used to create the index. We don't enforce a default suffix; that's left up to the instance. The suffix is used to differentiate between different types of objects.

+suffix+ If a nil value is explicitly passed in, it won't appear in the redis key that's returned. If no suffix is passed in, the class' suffix is used as the default (via the class method self.suffix). It's an important distinction b/c passing in an explicitly nil is how DataType objects at the class level are created without the global default 'object' suffix. See DataType#dbkey "parent_class?" for more details.



292
293
294
295
296
297
298
299
# File 'lib/familia/horreum/management.rb', line 292

def dbkey(identifier, suffix = self.suffix)
  if identifier.to_s.empty?
    raise NoIdentifier, "#{self} requires non-empty identifier, got: #{identifier.inspect}"
  end

  identifier &&= identifier.to_s
  Familia.dbkey(prefix, identifier, suffix)
end

#destroy!(identifier, suffix = nil) ⇒ Boolean

Destroys an object in Database with the given identifier.

This method is part of Familia's high-level object lifecycle management. While delete! operates directly on dbkeys, destroy! operates at the object level and is used for ORM-style operations. Use destroy! when removing complete objects from the system, and delete! when working directly with dbkeys.

Examples:

User.destroy!(123)  # Removes user:123:object from Valkey/Redis

Parameters:

  • identifier (String, Integer)

    The unique identifier for the object to destroy.

  • suffix (Symbol, nil) (defaults to: nil)

    The suffix to use in the dbkey (default: class suffix).

Returns:

  • (Boolean)

    true if the object was successfully destroyed, false otherwise.



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/familia/horreum/management.rb', line 235

def destroy!(identifier, suffix = nil)
  suffix ||= self.suffix
  return MultiResult.new(false, []) if identifier.to_s.empty?

  objkey = dbkey identifier, suffix

  # Execute all deletion operations within a transaction
  transaction do |conn|
    # Clean up related fields first to avoid orphaned keys
    if relations?
      Familia.trace :DESTROY_RELATIONS!, nil, "#{self} has relations: #{related_fields.keys}" if Familia.debug?

      # Create a temporary instance to access related fields.
      # Pass identifier in constructor so init() sees it and can set dependent fields.
      identifier_field_name = self.identifier_field
      temp_instance = identifier_field_name ? new(identifier_field_name => identifier.to_s) : new

      related_fields.each do |name, _definition|
        obj = temp_instance.send(name)
        Familia.trace :DESTROY_RELATION!, name, "Deleting related field #{name} (#{obj.dbkey})" if Familia.debug?
        conn.del(obj.dbkey)
      end
    end

    # Delete the main object key
    ret = conn.del(objkey)
    Familia.trace :DESTROY!, nil, "#{objkey} #{ret.inspect}" if Familia.debug?
  end
end

#exists?(identifier, suffix = nil) ⇒ Boolean

Checks if an object with the given identifier exists in the database.

This method constructs the full dbkey using the provided identifier and suffix, then checks if the key exists in the database.

Examples:

User.exists?(123)  # Returns true if user:123:object exists in Valkey/Redis

Parameters:

  • identifier (String, Integer)

    The unique identifier for the object.

  • suffix (Symbol, nil) (defaults to: nil)

    The suffix to use in the dbkey (default: class suffix).

Returns:

  • (Boolean)

    true if the object exists, false otherwise.

Raises:



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/familia/horreum/management.rb', line 205

def exists?(identifier, suffix = nil)
  raise NoIdentifier, 'Empty identifier' if identifier.to_s.empty?

  suffix ||= self.suffix

  objkey = dbkey identifier, suffix

  ret = dbclient.exists objkey
  Familia.trace :EXISTS, nil, "#{objkey} #{ret.inspect}" if Familia.debug?

  # Handle Redis::Future objects during transactions
  return ret if ret.is_a?(Redis::Future)

  ret.positive? # differs from Valkey API but I think it's okay bc `exists?` is a predicate method.
end

#familia_nameObject

Familia::Horreum::DefinitionMethods#familia_name

Examples:

V2::Session.config_name => 'Session'



97
98
99
100
101
# File 'lib/familia/horreum/management.rb', line 97

def familia_name
  return nil if name.nil?

  name.demodularize
end

#find_by_dbkey(objkey) ⇒ Object? Also known as: find_by_key

Retrieves and instantiates an object from Database using the full object key.

This method performs a two-step process to safely retrieve and instantiate objects:

  1. It first checks if the key exists in the database. This is crucial because:

    • It provides a definitive answer about the object's existence.
    • It prevents ambiguity that could arise from hgetall returning an empty hash for non-existent keys, which could lead to the creation of "empty" objects.
  2. If the key exists, it retrieves the object's data and instantiates it.

This approach ensures that we only attempt to instantiate objects that actually exist in Valkey/Redis, improving reliability and simplifying debugging.

Examples:

User.find_by_key("user:123")  # Returns a User instance if it exists,
nil otherwise

Parameters:

  • objkey (String)

    The full dbkey for the object.

Returns:

  • (Object, nil)

    An instance of the class if the key exists, nil otherwise.

Raises:

  • (ArgumentError)

    If the provided key is empty.



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/familia/horreum/management.rb', line 131

def find_by_dbkey(objkey)
  raise ArgumentError, 'Empty key' if objkey.to_s.empty?

  # We use a lower-level method here b/c we're working with the
  # full key and not just the identifier.
  does_exist = dbclient.exists(objkey).positive?

  Familia.ld "[find_by_key] #{self} from key #{objkey} (exists: #{does_exist})"
  Familia.trace :FIND_BY_DBKEY_KEY, nil, objkey

  # This is the reason for calling exists first. We want to definitively
  # and without any ambiguity know if the object exists in the database. If it
  # doesn't, we return nil. If it does, we proceed to load the object.
  # Otherwise, hgetall will return an empty hash, which will be passed to
  # the constructor, which will then be annoying to debug.
  return unless does_exist

  obj = dbclient.hgetall(objkey) # horreum objects are persisted as database hashes
  Familia.trace :FIND_BY_DBKEY_INSPECT, nil, "#{objkey}: #{obj.inspect}"

  # Create instance and deserialize fields using existing helper method
  # This avoids duplicating deserialization logic and keeps field-by-field processing
  instance = allocate
  instance.send(:initialize_relatives)
  instance.send(:initialize_with_keyword_args_deserialize_value, **obj)
  instance
end

#find_by_identifier(identifier, suffix = nil) ⇒ Object? Also known as: find_by_id

Retrieves and instantiates an object from Database using its identifier.

This method constructs the full dbkey using the provided identifier and suffix, then delegates to find_by_key for the actual retrieval and instantiation.

It's a higher-level method that abstracts away the key construction, making it easier to retrieve objects when you only have their identifier.

Examples:

User.find_by_id(123)  # Equivalent to User.find_by_key("user:123:object")

Parameters:

  • identifier (String, Integer)

    The unique identifier for the object.

  • suffix (Symbol) (defaults to: nil)

    The suffix to use in the dbkey (default: :object).

Returns:

  • (Object, nil)

    An instance of the class if found, nil otherwise.



179
180
181
182
183
184
185
186
187
188
# File 'lib/familia/horreum/management.rb', line 179

def find_by_identifier(identifier, suffix = nil)
  suffix ||= self.suffix
  return nil if identifier.to_s.empty?

  objkey = dbkey(identifier, suffix)

  Familia.ld "[find_by_id] #{self} from key #{objkey})"
  Familia.trace :FIND_BY_ID, nil, objkey if Familia.debug?
  find_by_dbkey objkey
end

#find_keys(suffix = '*') ⇒ Array<String>

Finds all keys in Database matching the given suffix pattern.

This method searches for all dbkeys that match the given suffix pattern. It uses the class's dbkey method to construct the search pattern.

Examples:

User.find  # Returns all keys matching user:*:object
User.find('active')  # Returns all keys matching user:*:active

Parameters:

  • suffix (String) (defaults to: '*')

    The suffix pattern to match (default: '*').

Returns:

  • (Array<String>)

    An array of matching dbkeys.



277
278
279
# File 'lib/familia/horreum/management.rb', line 277

def find_keys(suffix = '*')
  dbclient.keys(dbkey('*', suffix)) || []
end

#matching_keys_count(filter = '*') ⇒ Integer Also known as: size, length

Returns the number of dbkeys matching the given filter pattern

Parameters:

  • filter (String) (defaults to: '*')

    dbkey pattern to match (default: '*')

Returns:

  • (Integer)

    Number of matching keys



315
316
317
# File 'lib/familia/horreum/management.rb', line 315

def matching_keys_count(filter = '*')
  dbclient.keys(dbkey(filter)).compact.size
end

#multigetObject



66
67
68
# File 'lib/familia/horreum/management.rb', line 66

def multiget(...)
  rawmultiget(...).filter_map { |json| Familia::JsonSerializer.parse(json) }
end

#rawmultiget(*hids) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/familia/horreum/management.rb', line 70

def rawmultiget(*hids)
  hids.collect! { |hobjid| dbkey(hobjid) }
  return [] if hids.compact.empty?

  Familia.trace :MULTIGET, nil, "#{hids.size}: #{hids}" if Familia.debug?
  dbclient.mget(*hids)
end