rubocop-redis 0.1.5 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dc23288a737431b58136458dcd511758b722f6f3939fefc617abcbd64c7e5fa6
4
- data.tar.gz: dec4f8b93b223cc068c733f65287f02e2169ec69b90bb672b6f8a9dbe362c627
3
+ metadata.gz: 819ed1dc44a32d761db56ef63fff4c104deab950463ab50eca791ae2c566b94e
4
+ data.tar.gz: d1934717d789352034216cda7d24d5d3daf4e27b33bf6e9b3e9eeb1c3efc91c5
5
5
  SHA512:
6
- metadata.gz: df8684490628860d3cd491bbb84f9d2cc9d1b6da56a68ed7f1f87d056131424db367be9ff2d6043b9e626a7ea66bbfeb9a523a32d46cfe7491c5fc0386911eef
7
- data.tar.gz: aac88b4db49efa476fa52917cf906ce13fe6892bc9ae7e24eb8314189fcd12dba32e9a17f49869cf433785913642d44e47092c22d7433e0610ec4f87d764f141
6
+ metadata.gz: 5dcee7ad178036bb28f08abf1d41834833471642aa4ed7e414052a20f694ca5d00a4fdd363f494565fef3051d70876aee9fae24b04510f26424ad5a4905d259e
7
+ data.tar.gz: 33b33abc9a8d42ba38752a7380dd48bb702aa9ccef3e00a8089f9f922073058d5813a0cd66f8090dd0dad53e0f1c130a0473baad0fa51c230c645ac744fc5707
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # rubocop-redis
2
2
 
3
+ [![test](https://github.com/9sako6/rubocop-redis/actions/workflows/test.yml/badge.svg)](https://github.com/9sako6/rubocop-redis/actions/workflows/test.yml)
4
+
3
5
  An extension of RuboCop for [redis/redis-rb](https://github.com/redis/redis-rb) Redis client.
4
6
 
5
7
  ## Documentation
data/config/default.yml CHANGED
@@ -5,3 +5,9 @@ Redis/NotKeys:
5
5
  Enabled: true
6
6
  VersionAdded: "0.1"
7
7
  Safe: false
8
+
9
+ Redis/Setex:
10
+ Description: "Use `setex`"
11
+ Enabled: true
12
+ VersionAdded: "0.1"
13
+ Safe: true
@@ -38,3 +38,32 @@ loop do
38
38
  end
39
39
  all_keys
40
40
  ----
41
+
42
+ == Redis/Setex
43
+
44
+ |===
45
+ | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
46
+
47
+ | Enabled
48
+ | Yes
49
+ | No
50
+ | 0.1
51
+ | -
52
+ |===
53
+
54
+ Use `setex` method instead of calling both `set` and `expire` methods in a transaction.
55
+ See https://redis.io/commands/setex/ for details.
56
+
57
+ === Examples
58
+
59
+ [source,ruby]
60
+ ----
61
+ # bad
62
+ redis.multi do
63
+ redis.set(key, value)
64
+ redis.expire(key, 120)
65
+ end
66
+
67
+ # good
68
+ redis.setex(key, 120, value)
69
+ ----
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Redis
6
+ # Use `setex` method instead of calling both `set` and `expire` methods in a transaction.
7
+ # See https://redis.io/commands/setex/ for details.
8
+ #
9
+ # @example
10
+ # # bad
11
+ # redis.multi do
12
+ # redis.set(key, value)
13
+ # redis.expire(key, 120)
14
+ # end
15
+ #
16
+ # # good
17
+ # redis.setex(key, 120, value)
18
+ #
19
+ class Setex < Base
20
+ MSG = "Use `setex` method instead of calling both `set` and `expire` methods in a transaction."
21
+
22
+ def_node_matcher :multi_block?, <<~PATTERN
23
+ (block $(send ... :multi) args (begin $...))
24
+ PATTERN
25
+
26
+ def_node_matcher :match_set_call?, <<~PATTERN
27
+ (send ($...) :set ...)
28
+ PATTERN
29
+
30
+ def_node_matcher :match_expire_call?, <<~PATTERN
31
+ (send ($...) :expire ...)
32
+ PATTERN
33
+
34
+ RESTRICT_ON_SEND = %i[sets expire].freeze
35
+
36
+ def on_block(node)
37
+ return unless multi_block?(node)
38
+ return unless match_set_and_expire_in_multi_block?(node)
39
+
40
+ add_offense(node)
41
+ end
42
+
43
+ private
44
+
45
+ def match_set_and_expire_in_multi_block?(node)
46
+ _, statements = multi_block?(node)
47
+ result = false
48
+ called_set = false
49
+
50
+ statements.each do |statement|
51
+ called_set = true if match_set_call?(statement)
52
+
53
+ result = true if called_set && match_expire_call?(statement)
54
+ end
55
+
56
+ result
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -1,3 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'redis/not_keys'
4
+ require_relative 'redis/setex'
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Rubocop
4
4
  module Redis
5
- VERSION = "0.1.5"
5
+ VERSION = "0.1.6"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-redis
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - 9sako6
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-09-09 00:00:00.000000000 Z
11
+ date: 2022-09-28 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: An extension of RuboCop for redis/redis gem.
14
14
  email:
@@ -28,6 +28,7 @@ files:
28
28
  - docs/modules/ROOT/pages/cops_redis.adoc
29
29
  - lib/rubocop-redis.rb
30
30
  - lib/rubocop/cop/redis/not_keys.rb
31
+ - lib/rubocop/cop/redis/setex.rb
31
32
  - lib/rubocop/cop/redis_cops.rb
32
33
  - lib/rubocop/redis.rb
33
34
  - lib/rubocop/redis/inject.rb