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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 678d0983bf3cb50abb7732dc3c3e4da93764173dbdf03ce367254dbdb9297ef2
4
- data.tar.gz: 7209773f020b0d1055171f30f6ff204786f3edf84e5938cd09f55789fd86e934
3
+ metadata.gz: f26f56df1898716dc3a37d357857dcf9418709d826a5efdbf7d46f03bac29fcb
4
+ data.tar.gz: efb14c911678c9205c54aa06030371a8b0a12944bb93b9e4ba2d83d259321a10
5
5
  SHA512:
6
- metadata.gz: 2aa0b22042910eaa558f1bf8e5fed47aa7385d16ba46f63024f9311760a4f2ee74c42dadb3f333388755d1862a04999ca83cc016bf023a3e1a48491f089c7e03
7
- data.tar.gz: ff37e6f461fc3117fd7d02116da43931550683581c6327a176f58d3f09ba06b2da538cd27976f01d7922a7ce7bf9768a8c03445b25e3dc62e635f3ac953093e0
6
+ metadata.gz: ea3c23f42567b578efe70cf266c13202b1c8f51284baabaec040091268ec7b6804939892cf99883dd7601c380efd3eb3ec76092945b89d77b0e412c1f4749b35
7
+ data.tar.gz: 3b44e4b1d721374be329e567623e1a0d40918173897ca0b688f1bc4c9d2452791ffce2361c95d51f82fd9b726c1a6be7abb636fd5d4907e334227ab9051cedd8
@@ -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,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kredis
4
+ module Type
5
+ class DateTime < ActiveModel::Type::DateTime
6
+ def serialize(value)
7
+ super&.iso8601(9)
8
+ end
9
+
10
+ def cast_value(value)
11
+ super&.to_datetime
12
+ end
13
+ end
14
+ end
15
+ 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
@@ -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
- VALID_TYPES = %i[ string integer decimal float boolean datetime json ]
7
-
8
- def type_to_string(value)
9
- case value
10
- when nil
11
- ""
12
- when Integer
13
- value.to_s
14
- when BigDecimal
15
- value.to_d
16
- when Float
17
- value.to_s
18
- when TrueClass, FalseClass
19
- value ? "t" : "f"
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 && !VALID_TYPES.include?(type)
31
-
32
- case type
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)
@@ -1,3 +1,5 @@
1
+ require "active_support/core_ext/object/inclusion"
2
+
1
3
  class Kredis::Types::Enum < Kredis::Types::Proxying
2
4
  proxying :set, :get, :del
3
5
 
@@ -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 types_to_strings(entries) if entries.flatten.any?
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 types_to_strings(keys) if keys.flatten.any?
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
@@ -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
@@ -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
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Kredis
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
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.3.0
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-04 00:00:00.000000000 Z
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