frozen_record 0.19.0 → 0.19.5

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: e1006ddbdc9f21010a819e6085800b05fc549c9b513c75431b329f20d46e055a
4
- data.tar.gz: b3f65c04430375bf7fe2be7ecd233515e9a8b4a1dc7ebc29a430fa4934fad8be
3
+ metadata.gz: 9a5344be7378de1fb30f456b1a44b455dce2cec5d5db848058e8f3a137eb0ae7
4
+ data.tar.gz: a8ed04973c5c1edd7600d180c8fc827bdb9312ec0a7e602d408373eb86ff2749
5
5
  SHA512:
6
- metadata.gz: db63b827feb659e1228812947551db15ef777b33c71b38d3d9c77aa304d148b2e23eaaa6075e3dda90bd759abd9774635030bd01947b6d7b61fa01a53114601c
7
- data.tar.gz: a11f92b0a5ff35033c348b69735821969f33a93fdca861f58b3dd4c5a58263da643ce6efe6c9f7f473e754615e91c94712dce7901f7d49b2893bd3a808b9bc7b
6
+ metadata.gz: 8ec04aa72dfbee08525e8f4d6bea2413e7c84142f0f3bb8f6b8dc57538be10dcedfb07a7fd12f0c2992f292fb91ea492c8b6a21ab431470de551647b218b5dc2
7
+ data.tar.gz: 03ac7a0e64dd6caa6f519611096130c4bb33ac77467792b4c48d9a368f31abb91a22ed4a0d370e94e76b1a63b91aaec06b3bee1d38fcc9761edc47ac4beabe16
@@ -21,7 +21,8 @@ module FrozenRecord
21
21
  FALSY_VALUES = [false, nil, 0, -''].to_set
22
22
 
23
23
  class_attribute :base_path, :primary_key, :backend, :auto_reloading, :default_attributes, instance_accessor: false
24
- class_attribute :index_definitions, instance_accessor: false, default: {}.freeze
24
+ class_attribute :index_definitions, instance_accessor: false
25
+ self.index_definitions = {}.freeze
25
26
 
26
27
  self.primary_key = 'id'
27
28
 
@@ -18,7 +18,9 @@ module FrozenRecord
18
18
  @attributes = list_attributes(records).freeze
19
19
  build_attributes_cache
20
20
  define_attribute_methods(@attributes.to_a)
21
- records.map { |r| load(r) }.freeze
21
+ records = records.map { |r| load(r) }.freeze
22
+ index_definitions.values.each { |index| index.build(records) }
23
+ records
22
24
  end
23
25
  end
24
26
 
@@ -24,7 +24,7 @@ module FrozenRecord
24
24
  return data if data.frozen?
25
25
  data.map! { |d| deep_deduplicate!(d) }.freeze
26
26
  when String
27
- -data
27
+ -data.freeze
28
28
  else
29
29
  data.duplicable? ? data.freeze : data
30
30
  end
@@ -44,9 +44,9 @@ module FrozenRecord
44
44
  # String#-@ doesn't deduplicate the string if it's tainted.
45
45
  # So in such case we need to untaint it first.
46
46
  if data.tainted?
47
- -(+data).untaint
47
+ -(+data).untaint.freeze
48
48
  else
49
- -data
49
+ -data.freeze
50
50
  end
51
51
  else
52
52
  data.duplicable? ? data.freeze : data
@@ -20,6 +20,19 @@ module FrozenRecord
20
20
  end
21
21
 
22
22
  def query(value)
23
+ case value
24
+ when Array, Range
25
+ lookup_multi(value)
26
+ else
27
+ lookup(value)
28
+ end
29
+ end
30
+
31
+ def lookup_multi(values)
32
+ values.flat_map { |v| lookup(v) }
33
+ end
34
+
35
+ def lookup(value)
23
36
  @index.fetch(value, EMPTY_ARRAY)
24
37
  end
25
38
 
@@ -42,13 +55,19 @@ module FrozenRecord
42
55
  true
43
56
  end
44
57
 
45
- def query(value)
58
+ def lookup_multi(values)
59
+ results = @index.values_at(*values)
60
+ results.compact!
61
+ results
62
+ end
63
+
64
+ def lookup(value)
46
65
  record = @index[value]
47
66
  record ? [record] : EMPTY_ARRAY
48
67
  end
49
68
 
50
69
  def build(records)
51
- @index = records.to_h { |r| [r[attribute], r] }
70
+ @index = records.each_with_object({}) { |r, index| index[r[attribute]] = r }
52
71
  if @index.size != records.size
53
72
  raise AttributeNonUnique, "#{model}##{attribute.inspect} is not unique."
54
73
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FrozenRecord
4
- VERSION = '0.19.0'
4
+ VERSION = '0.19.5'
5
5
  end
@@ -207,6 +207,14 @@ describe 'querying' do
207
207
  countries = Country.where(name: 'France', continent: 'Europe')
208
208
  expect(countries.length).to be == 1
209
209
  end
210
+
211
+ it 'can use indices with inclusion query' do
212
+ countries = Country.where(continent: ['Europe', 'North America'])
213
+ expect(countries.length).to be == 3
214
+
215
+ countries = Country.where(name: ['France', 'Canada'])
216
+ expect(countries.length).to be == 2
217
+ end
210
218
  end
211
219
 
212
220
  describe '.where.not' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: frozen_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.19.0
4
+ version: 0.19.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jean Boussier
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-15 00:00:00.000000000 Z
11
+ date: 2020-08-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel