Skip to content

Commit

Permalink
fix(node_hash): quack like a hash
Browse files Browse the repository at this point in the history
instead of inheriting one
  • Loading branch information
stakach committed Apr 11, 2024
1 parent d94f140 commit 0fa47ef
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: redis_service_manager
version: 3.1.0
version: 3.1.1

dependencies:
ulid:
Expand Down
27 changes: 26 additions & 1 deletion src/redis_service_manager/node_hash.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
require "redis-cluster"

class RedisServiceManager
class NodeHash < Hash(String, String)
class NodeHash
include Enumerable({String, String})
include Iterable({String, String})

def initialize(@hash_key : String, @redis : Redis::Client)
super()
end
Expand All @@ -27,6 +30,22 @@ class RedisServiceManager
entry ? entry.to_s : yield key
end

def fetch(key, default)
fetch(key) { default }
end

def [](key, & : String -> String)
fetch(key) { yield }
end

def [](key)
fetch(key) { raise KeyError.new "Missing hash key: #{key.inspect}" }
end

def []?(key)
fetch(key, nil)
end

def delete(key, &)
key = key.to_s
value = self[key]?
Expand All @@ -37,6 +56,10 @@ class RedisServiceManager
yield key
end

def delete(key)
delete(key) { nil }
end

def keys
redis.hkeys(hash_key).map(&.to_s).sort!
end
Expand Down Expand Up @@ -65,5 +88,7 @@ class RedisServiceManager
end
self
end

delegate each, to: to_h
end
end

0 comments on commit 0fa47ef

Please sign in to comment.