redis_hash 0.9.5 → 0.10.0
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 +4 -4
- data/lib/redis_hash/base.rb +2 -0
- data/lib/redis_hash/concerns/adapter.rb +2 -0
- data/lib/redis_hash/concerns/expiration.rb +5 -0
- data/lib/redis_hash/concerns/insertions.rb +3 -0
- data/lib/redis_hash/concerns/schema.rb +38 -0
- data/lib/redis_hash/custom_matchers/allow_key.rb +24 -0
- data/lib/redis_hash/custom_matchers.rb +3 -0
- data/lib/redis_hash/spec_helper.rb +3 -0
- data/lib/redis_hash/version.rb +1 -1
- data/lib/redis_hash.rb +3 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 29ab1dc565944c85efab09cdee351cb3d16ab9f703c26df92a2f0adb2e120f46
|
4
|
+
data.tar.gz: 393055cc71ae67b3a35320338cb15c4c9072a59ab129052dd01fcdcc3cbe95d2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0fb0884e4b582715cdfc3f516f8c4d698828d3b5399c09823d5a30afa0ab5b60c0c0b5c354c88ba895501de1d03d85d2c91806a70e2ba4ebcf4ac59b9437115f
|
7
|
+
data.tar.gz: adc1a0ace6cd162ac08d9159b2a68e74a46503ecd8634bfc65f4c70ec7704a237982e2c28eb8f94c0fd5db1e771818507ea35692c7156b276af2e912250b2a6b
|
data/lib/redis_hash/base.rb
CHANGED
@@ -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
|
@@ -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
|
data/lib/redis_hash/version.rb
CHANGED
data/lib/redis_hash.rb
CHANGED
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.
|
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-
|
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:
|