Skip to content

Commit 38176a8

Browse files
committed
[#V1] Standardizes delete!, destroy!, & remove methods
Standardizes method naming across Redis type implementations to improve clarity and consistency * Replaces various delete/remove methods with a unified `remove` method * Clarifies the distinction between `delete!` and `destroy!` methods * Enhances documentation for object lifecycle management methods
1 parent 29caae6 commit 38176a8

File tree

8 files changed

+41
-36
lines changed

8 files changed

+41
-36
lines changed

lib/familia/horreum/class_methods.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,10 @@ def exists?(identifier, suffix = nil)
399399
# @param suffix [Symbol, nil] The suffix to use in the Redis key (default: class suffix).
400400
# @return [Boolean] true if the object was successfully destroyed, false otherwise.
401401
#
402-
# This method constructs the full Redis key using the provided identifier and suffix,
403-
# then removes the corresponding key from Redis.
402+
# This method is part of Familia's high-level object lifecycle management. While `delete!`
403+
# operates directly on Redis keys, `destroy!` operates at the object level and is used for
404+
# ORM-style operations. Use `destroy!` when removing complete objects from the system, and
405+
# `delete!` when working directly with Redis keys.
404406
#
405407
# @example
406408
# User.destroy!(123) # Removes user:123:object from Redis

lib/familia/horreum/commands.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,11 @@ def realttl
5858
redis.ttl rediskey
5959
end
6060

61-
# Deletes a field from the hash stored at the Redis key.
61+
# Removes a field from the hash stored at the Redis key.
6262
#
63-
# @param field [String] The field to delete from the hash.
63+
# @param field [String] The field to remove from the hash.
6464
# @return [Integer] The number of fields that were removed from the hash (0 or 1).
65-
# @note This method is destructive, as indicated by the bang (!).
66-
def hdel!(field)
65+
def remove_field(field)
6766
Familia.trace :HDEL, redis, field, caller(1..1) if Familia.debug?
6867
redis.hdel rediskey, field
6968
end
@@ -162,12 +161,13 @@ def key?(field)
162161
end
163162
alias has_key? key?
164163

164+
# Deletes the entire Redis key
165+
# @return [Boolean] true if the key was deleted, false otherwise
165166
def delete!
166167
Familia.trace :DELETE!, redis, redisuri, caller(1..1) if Familia.debug?
167168
ret = redis.del rediskey
168169
ret.positive?
169170
end
170-
protected :delete!
171171

172172
end
173173

lib/familia/horreum/serialization.rb

+5
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,11 @@ def commit_fields update_expiration: true
253253
# rocky.destroy!
254254
# # => *poof* Rocky is no more. A moment of silence, please.
255255
#
256+
# This method is part of Familia's high-level object lifecycle management. While `delete!`
257+
# operates directly on Redis keys, `destroy!` operates at the object level and is used for
258+
# ORM-style operations. Use `destroy!` when removing complete objects from the system, and
259+
# `delete!` when working directly with Redis keys.
260+
#
256261
# @note If debugging is enabled, this method will leave a trace of its
257262
# destructive path, like breadcrumbs for future data archaeologists.
258263
#

lib/familia/redistype/commands.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ def type
2222
redis.type rediskey
2323
end
2424

25+
# Deletes the entire Redis key
26+
# @return [Boolean] true if the key was deleted, false otherwise
2527
def delete!
2628
redis.del rediskey
2729
end
28-
alias clear delete!
29-
alias del delete!
3030

3131
def exists?
3232
redis.exists(rediskey) && !size.zero?

lib/familia/redistype/types/hashkey.rb

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# frozen_string_literal: true
2-
31
module Familia
42
class HashKey < RedisType
53
def size
@@ -49,14 +47,13 @@ def keys
4947
end
5048

5149
def values
52-
elements = redis.hvals(rediskey)
53-
multi_from_redis(*elements)
50+
redis.hvals(rediskey).map { |v| load v }
5451
end
5552

5653
def hgetall
57-
# TODO: Use from_redis. Also alias `all` is confusing with
58-
# Onetime::Customer.all which returns all customers.
59-
redis.hgetall rediskey
54+
redis.hgetall(rediskey).each_with_object({}) do |(k,v), ret|
55+
ret[k] = from_redis v
56+
end
6057
end
6158
alias all hgetall
6259

@@ -67,12 +64,12 @@ def key?(field)
6764
alias include? key?
6865
alias member? key?
6966

70-
def delete(field)
67+
# Removes a field from the hash
68+
# @param field [String] The field to remove
69+
# @return [Integer] The number of fields that were removed (0 or 1)
70+
def remove(field)
7171
redis.hdel rediskey, field
7272
end
73-
alias remove delete
74-
alias rem delete
75-
alias del delete
7673

7774
def increment(field, by = 1)
7875
redis.hincrby(rediskey, field, by).to_i

lib/familia/redistype/types/list.rb

+6-5
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,13 @@ def [](idx, count = nil)
5757
end
5858
alias slice []
5959

60-
def delete(v, count = 0)
61-
redis.lrem rediskey, count, to_redis(v)
60+
# Removes elements equal to value from the list
61+
# @param value The value to remove
62+
# @param count [Integer] Number of elements to remove (0 means all)
63+
# @return [Integer] The number of removed elements
64+
def remove(value, count = 0)
65+
redis.lrem rediskey, count, to_redis(value)
6266
end
63-
alias remove delete
64-
alias rem delete
65-
alias del delete
6667

6768
def range(sidx = 0, eidx = -1)
6869
elements = rangeraw sidx, eidx

lib/familia/redistype/types/sorted_set.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -201,18 +201,18 @@ def decrement(val, by = 1)
201201
alias decr decrement
202202
alias decrby decrement
203203

204-
def delete(val)
205-
Familia.trace :DELETE, redis, "#{val}<#{val.class}>", caller(1..1) if Familia.debug?
204+
# Removes a member from the sorted set
205+
# @param value The value to remove from the sorted set
206+
# @return [Integer] The number of members that were removed (0 or 1)
207+
def remove(value)
208+
Familia.trace :DELETE, redis, "#{value}<#{value.class}>", caller(1..1) if Familia.debug?
206209
# We use `strict_values: false` here to allow for the deletion of values
207210
# that are in the sorted set. If it's a horreum object, the value is
208211
# the identifier and not a serialized version of the object. So either
209212
# the value exists in the sorted set or it doesn't -- we don't need to
210213
# raise an error if it's not found.
211-
redis.zrem rediskey, to_redis(val, strict_values: false)
214+
redis.zrem rediskey, to_redis(value, strict_values: false)
212215
end
213-
alias remove delete
214-
alias rem delete
215-
alias del delete
216216

217217
def at(idx)
218218
range(idx, idx).first

lib/familia/redistype/types/unsorted_set.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ def member?(val)
7070
end
7171
alias include? member?
7272

73-
def delete(val)
74-
redis.srem rediskey, to_redis(val)
73+
# Removes a member from the set
74+
# @param value The value to remove from the set
75+
# @return [Integer] The number of members that were removed (0 or 1)
76+
def remove(value)
77+
redis.srem rediskey, to_redis(value)
7578
end
76-
alias remove delete
77-
alias rem delete
78-
alias del delete
7979

8080
def intersection *setkeys
8181
# TODO

0 commit comments

Comments
 (0)