fmcache 0.1.0 → 0.1.1

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: 5937e1aa882967a35ee510c8ca9dda6abe03cd4b0a493aad50d75526a8dc24c4
4
- data.tar.gz: 25b66cbedff899c9e0565686e709fd6dc4211c2f3f42384fbfaa2e42607b9946
3
+ metadata.gz: ec627f276181ebe2b3545e82864946010a0f056221075829eb53a2fc3884575f
4
+ data.tar.gz: bcc99177ea4e1b935eff30925e2ccb6a3fde41b19034c1741f7c4d6109c506de
5
5
  SHA512:
6
- metadata.gz: fbc1e97480a2d4ce1dfe33dbf2b5b6e35560281309e4a4fdb927c458177287e5202cd12196420ed0fda551c491faee6ff38207358f715a067fde5347d3e3c7c3
7
- data.tar.gz: f8d1edeb81200ca8418d91356b072a5dfa0f3ee8edc63df5381ef986c2466a6cc14e883631e976d325b2ed28492e9081b8902184bdc12dcfcff98094d5a20189
6
+ metadata.gz: 3c6393c6abd0f841036bf1f4db7c33bf7328231c86373401de104bd388079df9b0907019cd7d46d53e027535d5e339362c06431c629c82813eeaac0aa2d4f464
7
+ data.tar.gz: ddda0f0ed14eddf7a528e1282cd54e126fc72dd641037fe0b4646813c911450ada586300d8b9e3188b3cf5045944b350023c009532484865e7a28f0fe5966c88
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fmcache (0.1.0)
4
+ fmcache (0.1.1)
5
5
  field_mask_parser (~> 0.4.1)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # FMCache
2
2
 
3
+ [![Build Status](https://travis-ci.org/south37/fmcache.svg?branch=master)](https://travis-ci.org/south37/fmcache)
4
+ [![Gem Version](https://badge.fury.io/rb/fmcache.svg)](https://badge.fury.io/rb/fmcache)
5
+
3
6
  Library for caching json masked by FieldMask
4
7
 
5
8
  ## Installation
@@ -5,6 +5,7 @@ require "fmcache/engine"
5
5
  require "fmcache/field"
6
6
  require "fmcache/helper"
7
7
  require "fmcache/incomplete_info"
8
+ require "fmcache/jsonizer"
8
9
  require "fmcache/version"
9
10
 
10
11
  module FMCache
@@ -3,12 +3,13 @@ module FMCache
3
3
  DEFAULT_TTL = 7 * 24 * 3600 # 7 days
4
4
 
5
5
  # @param [Redis | MockRRedis] client
6
- def initialize(client:, fm_parser:, ttl: DEFAULT_TTL, notifier: nil)
6
+ def initialize(client:, fm_parser:, ttl: DEFAULT_TTL, notifier: nil, json_serializer: nil)
7
7
  @client = Client.new(client, notifier)
8
8
  @fm_parser = wrap(fm_parser)
9
9
  @ttl = ttl
10
10
  @encoder = Encoder.new
11
11
  @decoder = Decoder.new(@fm_parser)
12
+ @jsonizer = Jsonizer.new(json_serializer)
12
13
  end
13
14
 
14
15
  attr_reader :client, :fm_parser, :encoder, :decoder
@@ -19,7 +20,7 @@ module FMCache
19
20
  def write(values:, field_mask:)
20
21
  normalize!(field_mask)
21
22
  h = encode(values, field_mask)
22
- client.set(values: Helper.jsonize(h), ttl: @ttl)
23
+ client.set(values: @jsonizer.jsonize(h), ttl: @ttl)
23
24
  end
24
25
 
25
26
  # @param [<Integer | String>] ids
@@ -32,7 +33,7 @@ module FMCache
32
33
  keys = Helper.to_keys(ids)
33
34
  fields = Helper.to_fields(field_mask).map(&:to_s)
34
35
  h = client.get(keys: keys, fields: fields)
35
- decode(merge(Helper.dejsonize(h), ids), field_mask)
36
+ decode(merge(@jsonizer.dejsonize(h), ids), field_mask)
36
37
  end
37
38
 
38
39
  # @param [<Integer | String>] ids
@@ -26,42 +26,6 @@ module FMCache
26
26
  "fmcache:#{id}"
27
27
  end
28
28
 
29
- # @param [{ String => { String => <Hash> } }] hash
30
- # @return [{ String => { String => String } }]
31
- def jsonize(hash)
32
- r = {}
33
- hash.each do |k, v|
34
- h = {}
35
- v.each do |kk, vv|
36
- h[kk] = vv.to_json
37
- end
38
- r[k] = h
39
- end
40
- r
41
- end
42
-
43
- # @param [{ String => { String => String } }] hash
44
- # @return [{ String => { String => <Hash> } }]
45
- def dejsonize(hash)
46
- r = {}
47
- hash.each do |k, v|
48
- h = {}
49
- v.each do |kk, vv|
50
- if vv.nil?
51
- h[kk] = nil
52
- else
53
- begin
54
- h[kk] = JSON.parse(vv, symbolize_names: true)
55
- rescue
56
- h[kk] = nil
57
- end
58
- end
59
- end
60
- r[k] = h
61
- end
62
- r
63
- end
64
-
65
29
  # @param [<Hash>] values
66
30
  # @param [<Integer>] ids
67
31
  # @return [<Hash>]
@@ -0,0 +1,46 @@
1
+ require "fmcache/jsonizer/default_json_serializer"
2
+
3
+ module FMCache
4
+ class Jsonizer
5
+ # @param [#dump#load | nil] json_serializer
6
+ def initialize(json_serializer)
7
+ @json_serializer = json_serializer || DefaultJsonSerializer
8
+ end
9
+
10
+ # @param [{ String => { String => <Hash> } }] hash
11
+ # @return [{ String => { String => String } }]
12
+ def jsonize(hash)
13
+ r = {}
14
+ hash.each do |k, v|
15
+ h = {}
16
+ v.each do |kk, vv|
17
+ h[kk] = @json_serializer.dump(vv)
18
+ end
19
+ r[k] = h
20
+ end
21
+ r
22
+ end
23
+
24
+ # @param [{ String => { String => String } }] hash
25
+ # @return [{ String => { String => <Hash> } }]
26
+ def dejsonize(hash)
27
+ r = {}
28
+ hash.each do |k, v|
29
+ h = {}
30
+ v.each do |kk, vv|
31
+ if vv.nil?
32
+ h[kk] = nil
33
+ else
34
+ begin
35
+ h[kk] = @json_serializer.load(vv)
36
+ rescue
37
+ h[kk] = nil
38
+ end
39
+ end
40
+ end
41
+ r[k] = h
42
+ end
43
+ r
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,19 @@
1
+ module FMCache
2
+ class Jsonizer
3
+ class DefaultJsonSerializer
4
+ class << self
5
+ # @param [Hash | Array] obj
6
+ # @return [String]
7
+ def dump(obj)
8
+ JSON.dump(obj)
9
+ end
10
+
11
+ # @param [String] json
12
+ # @return [Hash | Array]
13
+ def load(json)
14
+ JSON.parse(json, symbolize_names: true)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,3 +1,3 @@
1
1
  module FMCache
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fmcache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nao Minami
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-12-12 00:00:00.000000000 Z
11
+ date: 2018-12-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -154,6 +154,8 @@ files:
154
154
  - lib/fmcache/field.rb
155
155
  - lib/fmcache/helper.rb
156
156
  - lib/fmcache/incomplete_info.rb
157
+ - lib/fmcache/jsonizer.rb
158
+ - lib/fmcache/jsonizer/default_json_serializer.rb
157
159
  - lib/fmcache/version.rb
158
160
  homepage: https://github.com/south37/fmcache
159
161
  licenses: