redisize 0.1.5 → 0.2.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: 354b2f1ac302400888ebd67b99b59177d615c685273b21ae4feb4ab3b1c6582f
4
- data.tar.gz: 9475c6209ffb1c840bc1da7754dd996e100796e2d37979da9eeee479e97d311e
3
+ metadata.gz: 71df00d9c001e09d06c90fc75ea318e50330dd6cebf2d804b0fd91ed329bb11b
4
+ data.tar.gz: caa72c28e9750012bccd1fc59caedfd27b5eb10bba75c38ba1f325e7e1527c94
5
5
  SHA512:
6
- metadata.gz: 692729405db96ff8132a4ec0d2eb9bd655000cf5ccb1d39d791c66c2f258f92f2b2f2ae885c8716b5fdebdc322af6478b76119e47f9e2c3c69f09dd0b81678eb
7
- data.tar.gz: 17fc2e30bc58f236675a439424c3c7b17a894861febd7595a6d267c9a7c7520cce877ae67a8f94d3ff6c4d8201bdaf3d5cd7d2f480c111787436f88b8c14ae60
6
+ metadata.gz: 17a578a95fdeb9a8c70fbea3d73c0642fa04ea37de713b8f386ad9b26e2ebd5802e2d66430f5e1698a16f227467bdf01abc6d7fd2cdf1b2479e3fadfd8acc515
7
+ data.tar.gz: fddd7a97b499514ac78a21168dfb5af6e21b10d5f28cdcc3001ec081812b565670fb8571e3a358e8759dae7bc929ab72825f16b437bae6398b8bfcb85ab3bc8f
@@ -0,0 +1,20 @@
1
+ module Redisize
2
+ module Support
3
+ module Enumerable
4
+ def self.included kls
5
+ kls.class_eval do
6
+ def redisize
7
+ case self
8
+ when Hash
9
+ redisize_hash { map { |x| x.jsonize } }
10
+ when Array
11
+ redisize_array { map { |x| x.jsonize } }
12
+ else
13
+ x.jsonize
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -1,3 +1,3 @@
1
1
  module Redisize
2
- VERSION = "0.1.5"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/redisize.rb CHANGED
@@ -16,6 +16,9 @@ module Redisize
16
16
  module ORM
17
17
  autoload :ActiveRecord, File.join(__dir__, 'redisize/orm/active_record.rb')
18
18
  end
19
+ module Support
20
+ autoload :Enumerable, File.join(__dir__, 'redisize/support/enumerable.rb')
21
+ end
19
22
 
20
23
  class << self
21
24
  attr_writer :adapter
@@ -30,6 +33,10 @@ module Redisize
30
33
  ActiveRecord: 'active_record'
31
34
  }
32
35
 
36
+ SUPPORTS = {
37
+ Enumerable: 'enumerable'
38
+ }
39
+
33
40
  LOGS = {
34
41
  Rails: 'rails'
35
42
  }
@@ -42,10 +49,11 @@ module Redisize
42
49
  def included kls
43
50
  kls.extend(Redisize::ClassMethods)
44
51
 
45
- # init orm, log, and cache
52
+ # init orm, log, cache, and support classes
46
53
  init_orm_for(kls)
47
54
  init_log_for(kls)
48
55
  init_cache_for(kls)
56
+ init_support_for(kls)
49
57
  end
50
58
 
51
59
  def init_orm_for kls
@@ -57,6 +65,17 @@ module Redisize
57
65
  end
58
66
  end
59
67
 
68
+ def init_support_for kls
69
+ sup = Object.constants.reduce(nil) { |res, anc| res || SUPPORTS.keys.find {|re| /#{re}/ =~ anc.to_s } }
70
+
71
+ if sup
72
+ require("redisize/support/#{SUPPORTS[sup]}")
73
+ cls = sup.to_s.constantize
74
+ cls.include(Redisize::Support.const_get(sup))
75
+ cls.include(Redisize::ClassMethods)
76
+ end
77
+ end
78
+
60
79
  def init_log_for kls
61
80
  log = Object.constants.reduce(nil) { |res, anc| res || LOGS.keys.find {|re| /#{re}/ =~ anc.to_s } }
62
81
 
@@ -75,6 +94,7 @@ module Redisize
75
94
  require("redisize/cache/#{CACHES[cache]}")
76
95
  Redisize.include(Redisize::Cache.const_get(cache))
77
96
  Redisize.extend(Redisize::Cache.const_get(cache))
97
+ Enumerable.include(Redisize::Cache.const_get(cache))
78
98
  kls.extend(Redisize::Cache.const_get(cache))
79
99
  end
80
100
  end
@@ -185,9 +205,9 @@ module Redisize
185
205
  assign_reverse_key(metakey, key)
186
206
  end
187
207
 
188
- # +redisize_sql_metas+ updates all the meta keys for the result value
208
+ # +redisize_array_metas+ updates all the meta keys for the result array value
189
209
  #
190
- def redisize_sql_metas key, attres
210
+ def redisize_array_metas key, attres
191
211
  model_name = key[1]
192
212
  primary_key = key[2]
193
213
 
@@ -296,15 +316,48 @@ module Redisize
296
316
  end
297
317
  end
298
318
 
319
+ def primary_key_with key_name_in = nil
320
+ key_name_in || self.respond_to?(:primary_key) && self.primary_key || case self
321
+ when ActiveRecord
322
+ self.map.first&.class&.primary_key
323
+ when ActiveRecord::Scoping, ActiveRecord::Reflection, ActiveRecord::Associations
324
+ self.values.map.first&.class&.primary_key
325
+ end
326
+ end
327
+
299
328
  # self -> model class
300
329
  def redisize_sql &block
301
330
  key = ["sql", self.name, self.primary_key, self.all.to_sql]
302
331
 
332
+ redisize_by_key(key, &block)
333
+ end
334
+
335
+ # self -> model class
336
+ def redisize_hash key_name_in = nil, &block
337
+ key_name = primary_key_with(key_name_in)
338
+
339
+ key = ["by_key", *self.values.map do |x|
340
+ key_name && x.respond_to?(key_name) ? x.send(key_name).to_s : x.to_s
341
+ end ]
342
+
343
+ redisize_by_key(key, &block)
344
+ end
345
+
346
+ # self -> model class
347
+ def redisize_array key_name_in = nil, &block
348
+ key_name = primary_key_with(key_name_in)
349
+
350
+ key = ["by_key", *self.map { |x| key_name ? x.send(key_name).to_s : x.to_s } ]
351
+
352
+ redisize_by_key(key, &block)
353
+ end
354
+
355
+ def redisize_by_key key, &block
303
356
  # binding.pry
304
357
  redisize_cache_fetch(key, expires_in: 1.day) do
305
358
  value = block.call
306
359
 
307
- Redisize.enqueue(:redisize_sql_metas, key, value)
360
+ Redisize.enqueue(:redisize_array_metas, key, value)
308
361
 
309
362
  value
310
363
  end
data/redisize.gemspec CHANGED
@@ -8,15 +8,15 @@ Gem::Specification.new do |spec|
8
8
 
9
9
  spec.summary = %q{Make json record cacheable to redis}
10
10
  spec.description = %q{Make json record cacheable to redis via various adapters like Resque, Sidekiq, etc}
11
- spec.homepage = "https://github.com/majioa/redisize"
11
+ spec.homepage = "https://codeberg.org/majioa/redisize"
12
12
  spec.license = "MIT"
13
13
  spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
14
 
15
15
  spec.metadata["allowed_push_host"] = "https://rubygems.org"
16
16
 
17
17
  spec.metadata["homepage_uri"] = spec.homepage
18
- spec.metadata["source_code_uri"] = "https://github.com/majioa/redisize"
19
- spec.metadata["changelog_uri"] = "https://github.com/majioa/redisize/CHANGELOG.md"
18
+ spec.metadata["source_code_uri"] = "https://codeberg.org/majioa/redisize"
19
+ spec.metadata["changelog_uri"] = "https://codeberg.org/majioa/redisize/CHANGELOG.md"
20
20
 
21
21
  # Specify which files should be added to the gem when it is released.
22
22
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redisize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel «Malo» Skrylev
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-12-31 00:00:00.000000000 Z
11
+ date: 2024-05-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry
@@ -48,16 +48,17 @@ files:
48
48
  - lib/redisize/cache/rails.rb
49
49
  - lib/redisize/log/rails.rb
50
50
  - lib/redisize/orm/active_record.rb
51
+ - lib/redisize/support/enumerable.rb
51
52
  - lib/redisize/version.rb
52
53
  - redisize.gemspec
53
- homepage: https://github.com/majioa/redisize
54
+ homepage: https://codeberg.org/majioa/redisize
54
55
  licenses:
55
56
  - MIT
56
57
  metadata:
57
58
  allowed_push_host: https://rubygems.org
58
- homepage_uri: https://github.com/majioa/redisize
59
- source_code_uri: https://github.com/majioa/redisize
60
- changelog_uri: https://github.com/majioa/redisize/CHANGELOG.md
59
+ homepage_uri: https://codeberg.org/majioa/redisize
60
+ source_code_uri: https://codeberg.org/majioa/redisize
61
+ changelog_uri: https://codeberg.org/majioa/redisize/CHANGELOG.md
61
62
  post_install_message:
62
63
  rdoc_options: []
63
64
  require_paths: