kredis 0.3.0 → 0.3.1
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 +4 -4
- data/lib/kredis/attributes.rb +4 -0
- data/lib/kredis/type/datetime.rb +15 -0
- data/lib/kredis/type/json.rb +19 -0
- data/lib/kredis/type_casting.rb +22 -34
- data/lib/kredis/types/enum.rb +2 -0
- data/lib/kredis/types/hash.rb +3 -4
- data/lib/kredis/types/list.rb +3 -3
- data/lib/kredis/types/scalar.rb +2 -2
- data/lib/kredis/types/set.rb +3 -3
- data/lib/kredis/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f26f56df1898716dc3a37d357857dcf9418709d826a5efdbf7d46f03bac29fcb
|
4
|
+
data.tar.gz: efb14c911678c9205c54aa06030371a8b0a12944bb93b9e4ba2d83d259321a10
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ea3c23f42567b578efe70cf266c13202b1c8f51284baabaec040091268ec7b6804939892cf99883dd7601c380efd3eb3ec76092945b89d77b0e412c1f4749b35
|
7
|
+
data.tar.gz: 3b44e4b1d721374be329e567623e1a0d40918173897ca0b688f1bc4c9d2452791ffce2361c95d51f82fd9b726c1a6be7abb636fd5d4907e334227ab9051cedd8
|
data/lib/kredis/attributes.rb
CHANGED
@@ -30,6 +30,10 @@ module Kredis::Attributes
|
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
|
+
def kredis_float(name, key: nil, config: :shared, after_change: nil)
|
34
|
+
kredis_connection_with __method__, name, key, config: config, after_change: after_change
|
35
|
+
end
|
36
|
+
|
33
37
|
def kredis_enum(name, key: nil, values:, default:, config: :shared, after_change: nil)
|
34
38
|
kredis_connection_with __method__, name, key, values: values, default: default, config: config, after_change: after_change
|
35
39
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Kredis
|
4
|
+
module Type
|
5
|
+
class Json < ActiveModel::Type::Value
|
6
|
+
def type
|
7
|
+
:json
|
8
|
+
end
|
9
|
+
|
10
|
+
def cast_value(value)
|
11
|
+
JSON.load(value)
|
12
|
+
end
|
13
|
+
|
14
|
+
def serialize(value)
|
15
|
+
JSON.dump(value)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/kredis/type_casting.rb
CHANGED
@@ -1,47 +1,35 @@
|
|
1
1
|
require "json"
|
2
|
+
require "active_model/type"
|
3
|
+
require "kredis/type/json"
|
4
|
+
require "kredis/type/datetime"
|
2
5
|
|
3
6
|
module Kredis::TypeCasting
|
4
7
|
class InvalidType < StandardError; end
|
5
8
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
when Time, DateTime, ActiveSupport::TimeWithZone
|
21
|
-
value.iso8601(9)
|
22
|
-
when Hash
|
23
|
-
JSON.dump(value)
|
24
|
-
else
|
25
|
-
value
|
26
|
-
end
|
9
|
+
TYPES = {
|
10
|
+
string: ActiveModel::Type::String.new,
|
11
|
+
integer: ActiveModel::Type::Integer.new,
|
12
|
+
decimal: ActiveModel::Type::Decimal.new,
|
13
|
+
float: ActiveModel::Type::Float.new,
|
14
|
+
boolean: ActiveModel::Type::Boolean.new,
|
15
|
+
datetime: Kredis::Type::DateTime.new,
|
16
|
+
json: Kredis::Type::Json.new
|
17
|
+
}
|
18
|
+
|
19
|
+
def type_to_string(value, type)
|
20
|
+
raise InvalidType if type && !TYPES.key?(type)
|
21
|
+
|
22
|
+
TYPES[type || :string].serialize(value)
|
27
23
|
end
|
28
24
|
|
29
25
|
def string_to_type(value, type)
|
30
|
-
raise InvalidType if type && !
|
31
|
-
|
32
|
-
|
33
|
-
when nil, :string then value
|
34
|
-
when :integer then value.to_i
|
35
|
-
when :decimal then value.to_d
|
36
|
-
when :float then value.to_f
|
37
|
-
when :boolean then value == "t" ? true : false
|
38
|
-
when :datetime then Time.iso8601(value)
|
39
|
-
when :json then JSON.load(value)
|
40
|
-
end if value.present?
|
26
|
+
raise InvalidType if type && !TYPES.key?(type)
|
27
|
+
|
28
|
+
TYPES[type || :string].cast(value)
|
41
29
|
end
|
42
30
|
|
43
|
-
def types_to_strings(values)
|
44
|
-
Array(values).flatten.map { |value| type_to_string(value) }
|
31
|
+
def types_to_strings(values, type)
|
32
|
+
Array(values).flatten.map { |value| type_to_string(value, type) }
|
45
33
|
end
|
46
34
|
|
47
35
|
def strings_to_types(values, type)
|
data/lib/kredis/types/enum.rb
CHANGED
data/lib/kredis/types/hash.rb
CHANGED
@@ -13,9 +13,8 @@ class Kredis::Types::Hash < Kredis::Types::Proxying
|
|
13
13
|
update key => value
|
14
14
|
end
|
15
15
|
|
16
|
-
|
17
16
|
def update(**entries)
|
18
|
-
hset
|
17
|
+
hset entries.transform_values{ |val| type_to_string(val, typed) } if entries.flatten.any?
|
19
18
|
end
|
20
19
|
|
21
20
|
def values_at(*keys)
|
@@ -23,11 +22,11 @@ class Kredis::Types::Hash < Kredis::Types::Proxying
|
|
23
22
|
end
|
24
23
|
|
25
24
|
def delete(*keys)
|
26
|
-
hdel
|
25
|
+
hdel keys if keys.flatten.any?
|
27
26
|
end
|
28
27
|
|
29
28
|
def remove
|
30
|
-
del
|
29
|
+
del
|
31
30
|
end
|
32
31
|
|
33
32
|
def entries
|
data/lib/kredis/types/list.rb
CHANGED
@@ -9,15 +9,15 @@ class Kredis::Types::List < Kredis::Types::Proxying
|
|
9
9
|
alias to_a elements
|
10
10
|
|
11
11
|
def remove(*elements)
|
12
|
-
types_to_strings(elements).each { |element| lrem 0, element }
|
12
|
+
types_to_strings(elements, typed).each { |element| lrem 0, element }
|
13
13
|
end
|
14
14
|
|
15
15
|
def prepend(*elements)
|
16
|
-
lpush types_to_strings(elements) if elements.flatten.any?
|
16
|
+
lpush types_to_strings(elements, typed) if elements.flatten.any?
|
17
17
|
end
|
18
18
|
|
19
19
|
def append(*elements)
|
20
|
-
rpush types_to_strings(elements) if elements.flatten.any?
|
20
|
+
rpush types_to_strings(elements, typed) if elements.flatten.any?
|
21
21
|
end
|
22
22
|
alias << append
|
23
23
|
end
|
data/lib/kredis/types/scalar.rb
CHANGED
@@ -4,12 +4,12 @@ class Kredis::Types::Scalar < Kredis::Types::Proxying
|
|
4
4
|
attr_accessor :typed, :default, :expires_in
|
5
5
|
|
6
6
|
def value=(value)
|
7
|
-
set type_to_string(value), ex: expires_in
|
7
|
+
set type_to_string(value, typed), ex: expires_in
|
8
8
|
end
|
9
9
|
|
10
10
|
def value
|
11
11
|
value_after_casting = string_to_type(get, typed)
|
12
|
-
|
12
|
+
|
13
13
|
if value_after_casting.nil?
|
14
14
|
default
|
15
15
|
else
|
data/lib/kredis/types/set.rb
CHANGED
@@ -9,12 +9,12 @@ class Kredis::Types::Set < Kredis::Types::Proxying
|
|
9
9
|
alias to_a members
|
10
10
|
|
11
11
|
def add(*members)
|
12
|
-
sadd types_to_strings(members) if members.flatten.any?
|
12
|
+
sadd types_to_strings(members, typed) if members.flatten.any?
|
13
13
|
end
|
14
14
|
alias << add
|
15
15
|
|
16
16
|
def remove(*members)
|
17
|
-
srem types_to_strings(members) if members.flatten.any?
|
17
|
+
srem types_to_strings(members, typed) if members.flatten.any?
|
18
18
|
end
|
19
19
|
|
20
20
|
def replace(*members)
|
@@ -25,7 +25,7 @@ class Kredis::Types::Set < Kredis::Types::Proxying
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def include?(member)
|
28
|
-
sismember type_to_string(member)
|
28
|
+
sismember type_to_string(member, typed)
|
29
29
|
end
|
30
30
|
|
31
31
|
def size
|
data/lib/kredis/version.rb
CHANGED
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.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kasper Timm Hansen
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2021-09-
|
12
|
+
date: 2021-09-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -68,6 +68,8 @@ files:
|
|
68
68
|
- lib/kredis/migration.rb
|
69
69
|
- lib/kredis/namespace.rb
|
70
70
|
- lib/kredis/railtie.rb
|
71
|
+
- lib/kredis/type/datetime.rb
|
72
|
+
- lib/kredis/type/json.rb
|
71
73
|
- lib/kredis/type_casting.rb
|
72
74
|
- lib/kredis/types.rb
|
73
75
|
- lib/kredis/types/callbacks_proxy.rb
|