frozen_record 0.26.0 → 0.26.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: 558032391d508fe0e51074f70abb81395189376a610c694cf6b43a64bbf2b91a
4
- data.tar.gz: e8bd4b3bb7e2dafb6d3854176b7c98e6f87a3148790dd5d5f0de0ec55b95ef48
3
+ metadata.gz: 4e4a597e4419873da69776c887c5ef35c812f785493cd9597d3d3ced891578ab
4
+ data.tar.gz: d46b86085ceeee261b39a281aab1c62a2551e872ad1f4696e8a7325b7cfa9a7a
5
5
  SHA512:
6
- metadata.gz: 725c9abc9be65de3c1db5643e0df340c34b9b72f50e4a650d0f0a51a27160f6f7db6bcdbb33cab77d7fac3d9e7c6558fe22fd04081b43b8da4c6f7ba2887a3e4
7
- data.tar.gz: a1f7e30104161efb60b5a6fbb89c1270f6656557c4f8bfb549a18ec012fdc87be73223e2d13d1efc73d18c3c8e2a0c905f9b9d0bb30907dcd465b659801f41fe
6
+ metadata.gz: 8e68a5dfb1d888de4f6dc72bb42ffdc1c08af87229b7bd0dcf107d1da9b48ef5b948adea5652b132cb497a1a8c15b513aa7ba8beea7c863b06f19de14ae02865
7
+ data.tar.gz: f787b5072efd3997fb41942c1d9b6014ee872d6faec12a4c5a77ef489a7039ed980987bbf4102ed7d9c5cd2b296e49c1bd6a25f578559969d200d875d6cdf877
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Unreleased
2
2
 
3
+ # v0.26.1
4
+
5
+ - Optimized single attribute lookup.
6
+
3
7
  # v0.26.0
4
8
 
5
9
  - Drop dependency on `dedup` gem.
data/benchmark/querying CHANGED
@@ -23,6 +23,13 @@ class LargeCountry < Country
23
23
  end
24
24
  end
25
25
 
26
+ LargeCountry.eager_load!
27
+
28
+ puts "=== unique index lookup ==="
29
+ Benchmark.ips do |x|
30
+ x.report('pk lookup') { LargeCountry.find_by(name: "Canada1") }
31
+ end
32
+
26
33
  puts "=== simple scalar match ==="
27
34
  Benchmark.ips do |x|
28
35
  x.report('simple') { LargeCountry.nato.size }
@@ -107,7 +107,7 @@ module FrozenRecord
107
107
  store[:scope] = scope
108
108
  end
109
109
 
110
- delegate :each, :find, :find_each, :find_by_id, :find_by, :find_by!, :where, :first, :first!, :last, :last!,
110
+ delegate :each, :find_each, :where, :first, :first!, :last, :last!,
111
111
  :pluck, :ids, :order, :limit, :offset, :minimum, :maximum, :average, :sum, :count,
112
112
  to: :current_scope
113
113
 
@@ -123,6 +123,31 @@ module FrozenRecord
123
123
  end
124
124
  end
125
125
 
126
+ def find_by_id(id)
127
+ find_by(primary_key => id)
128
+ end
129
+
130
+ def find(id)
131
+ raise RecordNotFound, "Can't lookup record without ID" unless id
132
+ find_by(primary_key => id) or raise RecordNotFound, "Couldn't find a record with ID = #{id.inspect}"
133
+ end
134
+
135
+ def find_by(criterias)
136
+ if criterias.size == 1
137
+ criterias.each do |attribute, value|
138
+ attribute = attribute.to_s
139
+ if index = index_definitions[attribute]
140
+ return index.lookup(value).first
141
+ end
142
+ end
143
+ end
144
+ current_scope.find_by(criterias)
145
+ end
146
+
147
+ def find_by!(criterias)
148
+ find_by(criterias) or raise RecordNotFound, "No record matched"
149
+ end
150
+
126
151
  def add_index(attribute, unique: false)
127
152
  index = unique ? UniqueIndex.new(self, attribute) : Index.new(self, attribute)
128
153
  self.index_definitions = index_definitions.merge(index.attribute => index).freeze
@@ -34,12 +34,17 @@ module FrozenRecord
34
34
  end
35
35
 
36
36
  def find_by_id(id)
37
- matching_records.find { |r| r.id == id }
37
+ find_by(@klass.primary_key => id)
38
38
  end
39
39
 
40
40
  def find(id)
41
41
  raise RecordNotFound, "Can't lookup record without ID" unless id
42
- find_by_id(id) or raise RecordNotFound, "Couldn't find a record with ID = #{id.inspect}"
42
+
43
+ scope = self
44
+ if @limit || @offset
45
+ scope = limit(nil).offset(nil)
46
+ end
47
+ scope.find_by_id(id) or raise RecordNotFound, "Couldn't find a record with ID = #{id.inspect}"
43
48
  end
44
49
 
45
50
  def find_by(criterias)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FrozenRecord
4
- VERSION = '0.26.0'
4
+ VERSION = '0.26.1'
5
5
  end
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.26.0
4
+ version: 0.26.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jean Boussier
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-07-12 00:00:00.000000000 Z
11
+ date: 2022-08-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel