redis_hash 0.9.5 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9571da3226484c40dc459a9a76684fdc07a9aaf63f8df568f86432449945af81
4
- data.tar.gz: cba522bee6a8988426c079987138baf393b99324eaa6cf2158782193b491b980
3
+ metadata.gz: 29ab1dc565944c85efab09cdee351cb3d16ab9f703c26df92a2f0adb2e120f46
4
+ data.tar.gz: 393055cc71ae67b3a35320338cb15c4c9072a59ab129052dd01fcdcc3cbe95d2
5
5
  SHA512:
6
- metadata.gz: 13662938025f2d3bdf43e23641db06a236876f3493a7e3d33b4654588bec29da0d534a915dc321ff3d32b9582d04b2a642fc753cba8c37837724b332482e07da
7
- data.tar.gz: 2ef6a6bf131abdbf33a408846bf57da7b5cb9aa7e9b7600106c41659fdb73dadf2d6cbde69b21c2b7ea81b469a18e875d7e4197c7b06eebd539d57cb49093d11
6
+ metadata.gz: 0fb0884e4b582715cdfc3f516f8c4d698828d3b5399c09823d5a30afa0ab5b60c0c0b5c354c88ba895501de1d03d85d2c91806a70e2ba4ebcf4ac59b9437115f
7
+ data.tar.gz: adc1a0ace6cd162ac08d9159b2a68e74a46503ecd8634bfc65f4c70ec7704a237982e2c28eb8f94c0fd5db1e771818507ea35692c7156b276af2e912250b2a6b
@@ -4,6 +4,7 @@ require_relative "concerns/adapter"
4
4
  require_relative "concerns/default"
5
5
  require_relative "concerns/callbacks"
6
6
  require_relative "concerns/core"
7
+ require_relative "concerns/schema"
7
8
  require_relative "concerns/identity"
8
9
  require_relative "concerns/accessors"
9
10
  require_relative "concerns/comparisons"
@@ -22,6 +23,7 @@ module RedisHash
22
23
  include RedisHash::Default
23
24
  include RedisHash::Callbacks
24
25
  include RedisHash::Core
26
+ include RedisHash::Schema
25
27
  include RedisHash::Identity
26
28
  include RedisHash::Accessors
27
29
  include RedisHash::Comparisons
@@ -10,6 +10,8 @@ module RedisHash
10
10
 
11
11
  delegate :default_redis, :default_redis_key, :default_redis_ttl, to: :class
12
12
 
13
+ delegate :pipelined, :multi, :exec, to: :redis
14
+
13
15
  private
14
16
 
15
17
  def initialize_redis(redis, redis_key, redis_ttl)
@@ -33,5 +33,10 @@ module RedisHash
33
33
  def ttl
34
34
  redis.ttl(redis_key)
35
35
  end
36
+
37
+ def persist
38
+ @redis_ttl = nil
39
+ redis.persist(redis_key)
40
+ end
36
41
  end
37
42
  end
@@ -11,6 +11,7 @@ module RedisHash
11
11
  end
12
12
 
13
13
  def merge!(other_hash)
14
+ assert_keys_allowed(other_hash.keys)
14
15
  run_callbacks(:insertion) { hmset(*other_hash.to_a.unshift(redis_key)) }
15
16
 
16
17
  self
@@ -18,11 +19,13 @@ module RedisHash
18
19
  alias_method :update, :merge!
19
20
 
20
21
  def store(field, value)
22
+ assert_keys_allowed(field)
21
23
  run_callbacks(:insertion) { hset(redis_key, field, value) }
22
24
  end
23
25
  alias_method :[]=, :store
24
26
 
25
27
  def setnx!(field, value)
28
+ assert_keys_allowed(field)
26
29
  run_callbacks(:insertion) do
27
30
  success = hsetnx(redis_key, field, value)
28
31
 
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Schema allow for the enforcement of a whitelist on the keys of the Hash.
4
+ module RedisHash
5
+ module Schema
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ class_attribute :_allowed_keys, instance_writer: false, default: []
10
+
11
+ delegate :_allowed_keys, to: :class
12
+
13
+ private
14
+
15
+ def assert_keys_allowed(*keys)
16
+ return true if _allowed_keys.empty?
17
+
18
+ impermissible = keys.flatten.map(&:to_sym) - _allowed_keys
19
+ return true if impermissible.empty?
20
+
21
+ raise ArgumentError, "Impermissible #{"key".pluralize(impermissible.length)}: #{impermissible.join(", ")}"
22
+ end
23
+ end
24
+
25
+ class_methods do
26
+ def inherited(base)
27
+ base._allowed_keys = _allowed_keys.dup
28
+ super
29
+ end
30
+
31
+ private
32
+
33
+ def allow_keys(*keys)
34
+ _allowed_keys.push(*keys.flatten.map(&:to_sym))
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ # RSpec matcher that tests usage of `.allow_keys`
4
+ #
5
+ # class Example < RedisHash
6
+ # allow_keys :foo, :bar
7
+ # end
8
+ #
9
+ # RSpec.describe Example, type: :redis_hash do
10
+ # subject { described_class }
11
+ #
12
+ # it { is_expected.to allow_key :foo }
13
+ # it { is_expected.to allow_key :bar }
14
+ # end
15
+
16
+ RSpec::Matchers.define :allow_key do |key|
17
+ match { expect(test_subject._allowed_keys).to include key }
18
+ description { "allow key #{key}" }
19
+ failure_message { "expected #{test_subject} to allow key #{key}" }
20
+
21
+ def test_subject
22
+ subject.is_a?(Class) ? subject : subject.class
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "custom_matchers/allow_key"
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "custom_matchers"
@@ -1,4 +1,4 @@
1
1
  module RedisHash
2
2
  # This constant is managed by spicerack
3
- VERSION = "0.9.5"
3
+ VERSION = "0.10.0"
4
4
  end
data/lib/redis_hash.rb CHANGED
@@ -1,4 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support"
4
+ require "active_support/inflector"
2
5
 
3
6
  require "redis"
4
7
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis_hash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.5
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Garside
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-05-20 00:00:00.000000000 Z
11
+ date: 2019-05-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -65,6 +65,10 @@ files:
65
65
  - lib/redis_hash/concerns/insertions.rb
66
66
  - lib/redis_hash/concerns/mutations.rb
67
67
  - lib/redis_hash/concerns/predicates.rb
68
+ - lib/redis_hash/concerns/schema.rb
69
+ - lib/redis_hash/custom_matchers.rb
70
+ - lib/redis_hash/custom_matchers/allow_key.rb
71
+ - lib/redis_hash/spec_helper.rb
68
72
  - lib/redis_hash/version.rb
69
73
  homepage: https://github.com/Freshly/spicerack/tree/master/redis_hash
70
74
  licenses: