modis 2.0.0 → 2.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d72c95fa261834b9a837e642e63e3ef9094e6a00
4
- data.tar.gz: d43a8f952fc05fc725adc94354d77ef4d8318a98
3
+ metadata.gz: bb24ac739595677e1200448641dc4e7c35a26f1d
4
+ data.tar.gz: 428715902d2b7e335e16d7ee2b65aa58c3864b52
5
5
  SHA512:
6
- metadata.gz: 8cf44d0818eb8c163064cee228732b1d86811b08475c033f45b90a4b23cc8e0fc8a1fe030c6822c4ae1e69e7e0873d53a3f0d304572ad3d538ad3d6c18a7490d
7
- data.tar.gz: 1293bc123e53195e5cc5e44c4b77669a52927e5fd6ec3ee20c3523fc3f0ff875905b861a02be07c0f276e2571e951ba35d73d0bf1ef03b59da7139adf3594e00
6
+ metadata.gz: 7f34d83dd16b16bd113a116d060523c8f832593f604e318077be24d279d8a9f06ce269f86a3935f5fbe8e1a70d815075a54f69c9ee3f4d45eaa0fafc211a461e
7
+ data.tar.gz: a3451db68ba6943d667fb11470c9635dfb0c74ac9449b330125ffe7be4d9b0c3e94df459d8a2acb8ab0069a6fa80fadf4c83a8630898ef26c023857f34cfa473
data/README.md CHANGED
@@ -36,6 +36,19 @@ end
36
36
  MyModel.create!(:name => 'Ian', :age => 28)
37
37
  ```
38
38
 
39
+ ### all index
40
+
41
+ Modis, by default, creates an `all` index in redis in which it stores all the IDs for records created. As a result, a large amount of memory will be consumed if many ids are stored. The `all` index functionality can be turned off by using `enable_all_index`
42
+
43
+ ```ruby
44
+ class MyModel
45
+ include Modis::Model
46
+ enable_all_index false
47
+ end
48
+ ```
49
+
50
+ By disabling the `all` index functionality, the IDs of each record created won't be saved. As a side effect, using `all` finder method will raise a `IndexError` exception as we would not have enough information to fetch all records. See https://github.com/ileitch/modis/pull/7 for more context.
51
+
39
52
  ## Supported Features
40
53
 
41
54
  TODO.
@@ -13,6 +13,11 @@ module Modis
13
13
  end
14
14
 
15
15
  def all
16
+ unless all_index_enabled?
17
+ raise IndexError, "Unable to retrieve all records of #{name}, "\
18
+ "because you disabled all index. See :enable_all_index for more."
19
+ end
20
+
16
21
  records = Modis.with_connection do |redis|
17
22
  ids = redis.smembers(key_for(:all))
18
23
  redis.pipelined do
@@ -61,6 +61,14 @@ module Modis
61
61
  "#{sti_base_absolute_namespace}:#{id}"
62
62
  end
63
63
 
64
+ def enable_all_index(bool)
65
+ @use_all_index = bool
66
+ end
67
+
68
+ def all_index_enabled?
69
+ @use_all_index == true || @use_all_index.nil?
70
+ end
71
+
64
72
  def create(attrs)
65
73
  model = new(attrs)
66
74
  model.save
@@ -128,8 +136,10 @@ module Modis
128
136
  run_callbacks :destroy do
129
137
  redis.pipelined do
130
138
  remove_from_indexes(redis)
131
- redis.srem(self.class.key_for(:all), id)
132
- redis.srem(self.class.sti_base_key_for(:all), id) if self.class.sti_child?
139
+ if self.class.all_index_enabled?
140
+ redis.srem(self.class.key_for(:all), id)
141
+ redis.srem(self.class.sti_base_key_for(:all), id) if self.class.sti_child?
142
+ end
133
143
  redis.del(key)
134
144
  end
135
145
  end
@@ -198,8 +208,10 @@ module Modis
198
208
  future = attrs.any? ? redis.hmset(key, attrs) : :unchanged
199
209
 
200
210
  if new_record?
201
- redis.sadd(self.class.key_for(:all), id)
202
- redis.sadd(self.class.sti_base_key_for(:all), id) if self.class.sti_child?
211
+ if self.class.all_index_enabled?
212
+ redis.sadd(self.class.key_for(:all), id)
213
+ redis.sadd(self.class.sti_base_key_for(:all), id) if self.class.sti_child?
214
+ end
203
215
  add_to_indexes(redis)
204
216
  else
205
217
  update_indexes(redis)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Modis
4
- VERSION = '2.0.0'
4
+ VERSION = '2.1.0'
5
5
  end
@@ -22,6 +22,10 @@ module FindersSpec
22
22
 
23
23
  class Worker < Producer
24
24
  end
25
+
26
+ class UserNoAllIndex < User
27
+ enable_all_index false
28
+ end
25
29
  end
26
30
 
27
31
  describe Modis::Finder do
@@ -67,6 +71,12 @@ describe Modis::Finder do
67
71
  model.destroy
68
72
  expect(FindersSpec::User.all).to eq([])
69
73
  end
74
+
75
+ it 'throws error when enable_all_index option is set to false' do
76
+ FindersSpec::UserNoAllIndex.create!(name: 'Yana')
77
+ expect { FindersSpec::UserNoAllIndex.all }
78
+ .to raise_error(Modis::IndexError)
79
+ end
70
80
  end
71
81
 
72
82
  it 'identifies a found record as not being new' do
@@ -47,6 +47,10 @@ module PersistenceSpec
47
47
  called_callbacks << :test_before_save
48
48
  end
49
49
  end
50
+
51
+ class MockModelNoAllIndex < MockModel
52
+ enable_all_index false
53
+ end
50
54
  end
51
55
 
52
56
  describe Modis::Persistence do
@@ -296,4 +300,22 @@ describe Modis::Persistence do
296
300
  expect(model.update_attributes(name: nil)).to be false
297
301
  end
298
302
  end
303
+
304
+ describe 'key for all records' do
305
+ let(:all_key_name) { "#{PersistenceSpec::MockModel.absolute_namespace}:all" }
306
+
307
+ describe 'when :enable_all_index option is set to false' do
308
+ it 'does not save new record to the *:all key' do
309
+ model = PersistenceSpec::MockModel.create!(name: 'Sage')
310
+ expect(Redis.new.smembers(all_key_name).map(&:to_i)).to include(model.id)
311
+ end
312
+ end
313
+
314
+ describe 'when :enable_all_index option is set to false' do
315
+ it 'does not save new record to the *:all key' do
316
+ model = PersistenceSpec::MockModelNoAllIndex.create!(name: 'Alex')
317
+ expect(Redis.new.smembers(all_key_name).map(&:to_i)).to_not include(model.id)
318
+ end
319
+ end
320
+ end
299
321
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: modis
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ian Leitch
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-25 00:00:00.000000000 Z
11
+ date: 2017-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel