11require "active_support/core_ext/hash"
22
33class Kredis ::Types ::Hash < Kredis ::Types ::Proxying
4- ZERO_FIELDS_ADDED = 0
4+ prepend Kredis :: DefaultValues
55
6- proxying :hset , :hdel , :hgetall , :del , :exists? , :multi , :callnx
6+ proxying :hget , : hset, :hmget , : hdel, :hgetall , :hkeys , :hvals , :del , :exists?
77
88 attr_accessor :typed
99
1010 def []( key )
11- string_to_type ( entries [ key ] , typed )
11+ string_to_type ( hget ( key ) , typed )
1212 end
1313
1414 def []=( key , value )
1515 update key => value
1616 end
1717
18-
1918 def update ( **entries )
20- return ZERO_FIELDS_ADDED if entries . flatten . blank?
21-
22- init_default_in_multi do
23- hset entries . transform_values { |val | type_to_string ( val , typed ) }
24- end
19+ hset entries . transform_values { |val | type_to_string ( val , typed ) } if entries . flatten . any?
2520 end
2621
2722 def values_at ( *keys )
28- strings_to_types ( entries . values_at ( * keys ) || [ ] , typed )
23+ strings_to_types ( hmget ( keys ) || [ ] , typed )
2924 end
3025
3126 def delete ( *keys )
32- return ZERO_FIELDS_ADDED if keys . flatten . blank?
33-
34- init_default_in_multi { hdel keys }
27+ hdel keys if keys . flatten . any?
3528 end
3629
3730 def remove
@@ -40,20 +33,20 @@ def remove
4033 alias clear remove
4134
4235 def entries
43- ( hgetall . presence || default || { } ) . transform_values { |val | string_to_type ( val , typed ) } . with_indifferent_access
36+ ( hgetall || { } ) . transform_values { |val | string_to_type ( val , typed ) } . with_indifferent_access
4437 end
4538 alias to_h entries
4639
4740 def keys
48- entries . keys || [ ]
41+ hkeys || [ ]
4942 end
5043
5144 def values
52- strings_to_types ( entries . values || [ ] , typed )
45+ strings_to_types ( hvals || [ ] , typed )
5346 end
5447
5548 private
56- def set_default ( entries )
57- callnx ( :hset , entries . transform_values { | val | type_to_string ( val , typed ) } )
49+ def set_default
50+ update ** default
5851 end
5952end
0 commit comments