kredis 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of kredis might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b8c81a934c4d3b042045d49c96450e86c681529411fe5de467698d213f66bf21
4
- data.tar.gz: b5d81f606b35fa29ebccd67940c4fd8d6f01c59d5957b0f3fb5aabf06c0f3896
3
+ metadata.gz: 57c373219f474818bb85a7da4ef3a07bd4910cd685704b54549969c364ee5993
4
+ data.tar.gz: f220ce448caa279514e249bc7c2f744c7276946d5f5af5a9899a9f6e14d3b5be
5
5
  SHA512:
6
- metadata.gz: 8dff036d7c5dfecb9e755d991419622cc2456a7eb4f79ea7a1a317156fc5ba87525b339f92726a19062c43716c23dce707ab1ec80253852dc79fed7a48cfade3
7
- data.tar.gz: 64928a2e0d7c9ada0a51515dc8405339f053b5579f0cd57f109a52a38e23461a9be164db72c5fbb0eb0af8ff8fee3fb813e21c468b0d3e90c4a6a75747d7b777
6
+ metadata.gz: ef637b80601d21e815317201ab426da35a56926a2d069303cf1244e5a51f4ab0f80f7ef6e47a566d306248bbc2006c8a0dcdfd6990e0abf3057b5474ba7b6fc0
7
+ data.tar.gz: 99e4587242b125cfc78deff59e410055f9fefbfce726942ce1192e6065a6e08c1cfd0d83d409221c54418803b4405643ce8f873de8fbb0ef0c781ba882625c0e
@@ -4,11 +4,17 @@ require "kredis/connections"
4
4
  require "kredis/types"
5
5
  require "kredis/attributes"
6
6
  require "kredis/namespace"
7
+ require "kredis/logger"
7
8
 
8
9
  module Kredis
9
10
  include Connections
10
11
  include Namespace
11
12
  include Types
13
+ include Logger
12
14
 
13
15
  extend self
16
+
17
+ def redis(config: :shared)
18
+ configured_for(config)
19
+ end
14
20
  end
@@ -2,38 +2,50 @@ module Kredis::Attributes
2
2
  extend ActiveSupport::Concern
3
3
 
4
4
  class_methods do
5
- def kredis_list(name, config: :shared)
5
+ def kredis_proxy(name, key: nil, config: :shared)
6
+ ivar_symbol = :"@#{name}_kredis_proxy"
7
+
8
+ define_method(name) do
9
+ if instance_variable_defined?(ivar_symbol)
10
+ instance_variable_get(ivar_symbol)
11
+ else
12
+ instance_variable_set(ivar_symbol, Kredis.proxy(kredis_key_evaluated(key) || kredis_key_for_attribute(name), config: config))
13
+ end
14
+ end
15
+ end
16
+
17
+ def kredis_list(name, key: nil, config: :shared)
6
18
  ivar_symbol = :"@#{name}_kredis_list"
7
19
 
8
20
  define_method(name) do
9
21
  if instance_variable_defined?(ivar_symbol)
10
22
  instance_variable_get(ivar_symbol)
11
23
  else
12
- instance_variable_set(ivar_symbol, Kredis.list(kredis_key_for_attribute(name), config: config))
24
+ instance_variable_set(ivar_symbol, Kredis.list(kredis_key_evaluated(key) || kredis_key_for_attribute(name), config: config))
13
25
  end
14
26
  end
15
27
  end
16
28
 
17
- def kredis_unique_list(name, limit: nil, config: :shared)
29
+ def kredis_unique_list(name, limit: nil, key: nil, config: :shared)
18
30
  ivar_symbol = :"@#{name}_kredis_unique_list"
19
31
 
20
32
  define_method(name) do
21
33
  if instance_variable_defined?(ivar_symbol)
22
34
  instance_variable_get(ivar_symbol)
23
35
  else
24
- instance_variable_set(ivar_symbol, Kredis.unique_list(kredis_key_for_attribute(name), limit: limit, config: config))
36
+ instance_variable_set(ivar_symbol, Kredis.unique_list(kredis_key_evaluated(key) || kredis_key_for_attribute(name), limit: limit, config: config))
25
37
  end
26
38
  end
27
39
  end
28
40
 
29
- def kredis_flag(name, config: :shared)
41
+ def kredis_flag(name, key: nil, config: :shared)
30
42
  ivar_symbol = :"@#{name}_kredis_flag"
31
43
 
32
44
  define_method(name) do
33
45
  if instance_variable_defined?(ivar_symbol)
34
46
  instance_variable_get(ivar_symbol)
35
47
  else
36
- instance_variable_set(ivar_symbol, Kredis.flag(kredis_key_for_attribute(name), config: config))
48
+ instance_variable_set(ivar_symbol, Kredis.flag(kredis_key_evaluated(key) || kredis_key_for_attribute(name), config: config))
37
49
  end
38
50
  end
39
51
 
@@ -41,10 +53,41 @@ module Kredis::Attributes
41
53
  instance_variable_defined?(ivar_symbol) && instance_variable_get(ivar_symbol).marked?
42
54
  end
43
55
  end
56
+
57
+ def kredis_string(name, key: nil, config: :shared)
58
+ ivar_symbol = :"@#{name}_kredis_string"
59
+
60
+ define_method(name) do
61
+ if instance_variable_defined?(ivar_symbol)
62
+ instance_variable_get(ivar_symbol)
63
+ else
64
+ instance_variable_set(ivar_symbol, Kredis.string(kredis_key_evaluated(key) || kredis_key_for_attribute(name), config: config))
65
+ end
66
+ end
67
+ end
68
+
69
+ def kredis_integer(name, key: nil, config: :shared)
70
+ ivar_symbol = :"@#{name}_kredis_integer"
71
+
72
+ define_method(name) do
73
+ if instance_variable_defined?(ivar_symbol)
74
+ instance_variable_get(ivar_symbol)
75
+ else
76
+ instance_variable_set(ivar_symbol, Kredis.integer(kredis_key_evaluated(key) || kredis_key_for_attribute(name), config: config))
77
+ end
78
+ end
79
+ end
44
80
  end
45
81
 
46
82
  private
47
- def kredis_key_for_attribute(name)
83
+ def kredis_key_evaluated(key)
84
+ case key
85
+ when String then key
86
+ when Proc then key.call(self)
87
+ end
88
+ end
89
+
90
+ def kredis_key_for_attribute(name, key: nil)
48
91
  "#{self.class.name.tableize.gsub("/", ":")}:#{id}:#{name}"
49
92
  end
50
93
  end
@@ -0,0 +1,5 @@
1
+ require "redis"
2
+
3
+ module Kredis::Logger
4
+ mattr_accessor :logger
5
+ end
@@ -12,6 +12,10 @@ module Kredis
12
12
  end
13
13
  end
14
14
 
15
+ initializer "kredis.logger" do
16
+ Kredis.logger = Rails.logger
17
+ end
18
+
15
19
  initializer "kredis.configurator" do
16
20
  Kredis.configurator = Rails.application
17
21
  end
@@ -1,6 +1,6 @@
1
1
  module Kredis::Types
2
- def keyed(key, config: :shared)
3
- Kredis::Proxy.new configured_for(config), namespaced_key(key)
2
+ def proxy(key, config: :shared)
3
+ Kredis::Types::Proxy.new configured_for(config), namespaced_key(key)
4
4
  end
5
5
 
6
6
  def list(key, config: :shared)
@@ -19,14 +19,24 @@ module Kredis::Types
19
19
  Flag.new configured_for(config), namespaced_key(key)
20
20
  end
21
21
 
22
+ def string(key, config: :shared)
23
+ Kredis::Types::String.new configured_for(config), namespaced_key(key)
24
+ end
25
+
26
+ def integer(key, config: :shared)
27
+ Kredis::Types::Integer.new configured_for(config), namespaced_key(key)
28
+ end
29
+
22
30
  def mutex(key, expires_in: nil, config: :shared)
23
31
  Mutex.new configured_for(config), namespaced_key(key), expires_in: expires_in
24
32
  end
25
33
  end
26
34
 
27
- require "kredis/proxy"
35
+ require "kredis/types/proxy"
28
36
  require "kredis/types/list"
29
37
  require "kredis/types/unique_list"
30
38
  require "kredis/types/counter"
31
39
  require "kredis/types/flag"
40
+ require "kredis/types/string"
41
+ require "kredis/types/integer"
32
42
  require "kredis/types/mutex"
@@ -1,4 +1,4 @@
1
- class Kredis::Types::Counter < Kredis::Proxy
1
+ class Kredis::Types::Counter < Kredis::Types::Proxy
2
2
  def initialize(redis, key, expires_in: nil)
3
3
  @expires_in = expires_in
4
4
  super redis, key
@@ -1,4 +1,4 @@
1
- class Kredis::Types::Flag < Kredis::Proxy
1
+ class Kredis::Types::Flag < Kredis::Types::Proxy
2
2
  def mark
3
3
  set 1
4
4
  end
@@ -0,0 +1,9 @@
1
+ class Kredis::Types::Integer < Kredis::Types::String
2
+ def value
3
+ get&.to_i
4
+ end
5
+
6
+ def to_s
7
+ value.to_s
8
+ end
9
+ end
@@ -1,4 +1,4 @@
1
- class Kredis::Types::List < Kredis::Proxy
1
+ class Kredis::Types::List < Kredis::Types::Proxy
2
2
  def elements
3
3
  lrange(0, -1) || []
4
4
  end
@@ -1,4 +1,4 @@
1
- class Kredis::Types::Mutex < Kredis::Proxy
1
+ class Kredis::Types::Mutex < Kredis::Types::Proxy
2
2
  def initialize(redis, key, expires_in: nil)
3
3
  @expires_in = expires_in
4
4
  super redis, key
@@ -1,4 +1,4 @@
1
- class Kredis::Proxy
1
+ class Kredis::Types::Proxy
2
2
  def initialize(redis, key)
3
3
  @redis, @key = redis, key
4
4
  end
@@ -8,6 +8,7 @@ class Kredis::Proxy
8
8
  end
9
9
 
10
10
  def method_missing(method, *args, **kwargs)
11
+ Kredis.logger&.debug "[Kredis] #{method} #{@key} #{args.inspect if args.compact_blank.any?} #{kwargs.inspect if kwargs.compact_blank.any?}".chomp
11
12
  @redis.public_send method, @key, *args, **kwargs
12
13
  end
13
14
  end
@@ -0,0 +1,21 @@
1
+ class Kredis::Types::String < Kredis::Types::Proxy
2
+ def assign=(value)
3
+ set value
4
+ end
5
+
6
+ def assigned?
7
+ exists?
8
+ end
9
+
10
+ def value
11
+ get
12
+ end
13
+
14
+ def to_s
15
+ value
16
+ end
17
+
18
+ def clear
19
+ del
20
+ end
21
+ end
@@ -1,3 +1,3 @@
1
1
  module Kredis
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kredis
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kasper Timm Hansen
@@ -50,14 +50,17 @@ files:
50
50
  - lib/kredis.rb
51
51
  - lib/kredis/attributes.rb
52
52
  - lib/kredis/connections.rb
53
+ - lib/kredis/logger.rb
53
54
  - lib/kredis/namespace.rb
54
- - lib/kredis/proxy.rb
55
55
  - lib/kredis/railtie.rb
56
56
  - lib/kredis/types.rb
57
57
  - lib/kredis/types/counter.rb
58
58
  - lib/kredis/types/flag.rb
59
+ - lib/kredis/types/integer.rb
59
60
  - lib/kredis/types/list.rb
60
61
  - lib/kredis/types/mutex.rb
62
+ - lib/kredis/types/proxy.rb
63
+ - lib/kredis/types/string.rb
61
64
  - lib/kredis/types/unique_list.rb
62
65
  - lib/kredis/version.rb
63
66
  homepage: https://github.com/rails/kredis