frozen_record 0.19.2 → 0.20.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/frozen_record.gemspec +2 -0
- data/lib/frozen_record.rb +2 -1
- data/lib/frozen_record/base.rb +2 -2
- data/lib/frozen_record/compact.rb +5 -3
- data/lib/frozen_record/scope.rb +3 -2
- data/lib/frozen_record/version.rb +1 -1
- data/spec/scope_spec.rb +5 -0
- data/spec/support/country.rb +4 -0
- metadata +17 -6
- data/lib/frozen_record/deduplication.rb +0 -57
- data/spec/deduplication_spec.rb +0 -31
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4c98f13b693b4c7bceae48a308722c3681a678033855f40995a10fd82c0faee8
|
4
|
+
data.tar.gz: ff4ef784946b6fc9f067caf8e26d50433c3c396716ead1c953dd324528861ab2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e25090babf81d5ffa18a26acecf5af68d3424866b848508838ded880196d2178f994ce586be9bee8d63db71b3c84159ed2b4fac2c7e90f26414b9fbff4c69598
|
7
|
+
data.tar.gz: 384749f211db239a1d3964c16d5c23d4af4261ee052d6c0fa16f15f0d6a0ce5710df898b1f4e290c9cfa77c553d214668ab383ca33b09fbb82a03266db7a0324
|
data/frozen_record.gemspec
CHANGED
@@ -20,6 +20,8 @@ Gem::Specification.new do |spec|
|
|
20
20
|
spec.required_ruby_version = '>= 2.5'
|
21
21
|
|
22
22
|
spec.add_runtime_dependency 'activemodel'
|
23
|
+
spec.add_runtime_dependency 'dedup'
|
24
|
+
|
23
25
|
spec.add_development_dependency 'bundler'
|
24
26
|
spec.add_development_dependency 'rake'
|
25
27
|
spec.add_development_dependency 'rspec'
|
data/lib/frozen_record.rb
CHANGED
@@ -5,12 +5,13 @@ require 'set'
|
|
5
5
|
require 'active_support/all'
|
6
6
|
require 'active_model'
|
7
7
|
|
8
|
+
require 'dedup'
|
9
|
+
|
8
10
|
require 'frozen_record/version'
|
9
11
|
require 'frozen_record/scope'
|
10
12
|
require 'frozen_record/index'
|
11
13
|
require 'frozen_record/base'
|
12
14
|
require 'frozen_record/compact'
|
13
|
-
require 'frozen_record/deduplication'
|
14
15
|
|
15
16
|
module FrozenRecord
|
16
17
|
RecordNotFound = Class.new(StandardError)
|
data/lib/frozen_record/base.rb
CHANGED
@@ -52,7 +52,7 @@ module FrozenRecord
|
|
52
52
|
alias_method :set_default_attributes, :default_attributes=
|
53
53
|
private :set_default_attributes
|
54
54
|
def default_attributes=(default_attributes)
|
55
|
-
set_default_attributes(
|
55
|
+
set_default_attributes(Dedup.deep_intern!(default_attributes.stringify_keys))
|
56
56
|
end
|
57
57
|
|
58
58
|
alias_method :set_primary_key, :primary_key=
|
@@ -147,7 +147,7 @@ module FrozenRecord
|
|
147
147
|
@records ||= begin
|
148
148
|
records = backend.load(file_path)
|
149
149
|
records.each { |r| assign_defaults!(r) }
|
150
|
-
records =
|
150
|
+
records = Dedup.deep_intern!(records)
|
151
151
|
@attributes = list_attributes(records).freeze
|
152
152
|
define_attribute_methods(@attributes.to_a)
|
153
153
|
records = records.map { |r| load(r) }.freeze
|
@@ -14,11 +14,13 @@ module FrozenRecord
|
|
14
14
|
@records ||= begin
|
15
15
|
records = backend.load(file_path)
|
16
16
|
records.each { |r| assign_defaults!(r) }
|
17
|
-
records =
|
17
|
+
records = Dedup.deep_intern!(records)
|
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
|
|
@@ -65,7 +67,7 @@ module FrozenRecord
|
|
65
67
|
|
66
68
|
def attributes=(attributes)
|
67
69
|
self.class.attributes.each do |attr|
|
68
|
-
instance_variable_set(self.class._attributes_cache[attr],
|
70
|
+
instance_variable_set(self.class._attributes_cache[attr], Dedup.deep_intern!(attributes[attr]))
|
69
71
|
end
|
70
72
|
end
|
71
73
|
|
data/lib/frozen_record/scope.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
module FrozenRecord
|
4
4
|
class Scope
|
5
|
-
|
5
|
+
DISALLOWED_ARRAY_METHODS = [
|
6
6
|
:compact!, :flatten!, :reject!, :reverse!, :rotate!, :map!,
|
7
7
|
:shuffle!, :slice!, :sort!, :sort_by!, :delete_if,
|
8
8
|
:keep_if, :pop, :shift, :delete_at, :compact
|
@@ -227,13 +227,14 @@ module FrozenRecord
|
|
227
227
|
super
|
228
228
|
end
|
229
229
|
end
|
230
|
+
ruby2_keywords :method_missing if respond_to?(:ruby2_keywords, true)
|
230
231
|
|
231
232
|
def delegate_to_class(*args, &block)
|
232
233
|
scoping { @klass.public_send(*args, &block) }
|
233
234
|
end
|
234
235
|
|
235
236
|
def array_delegable?(method)
|
236
|
-
Array.method_defined?(method) &&
|
237
|
+
Array.method_defined?(method) && DISALLOWED_ARRAY_METHODS.exclude?(method)
|
237
238
|
end
|
238
239
|
|
239
240
|
def where!(criterias)
|
data/spec/scope_spec.rb
CHANGED
@@ -193,6 +193,11 @@ describe 'querying' do
|
|
193
193
|
expect(countries.length).to be == 0
|
194
194
|
end
|
195
195
|
|
196
|
+
it 'is chainable with methods of the form `def method(*args, **kargs)' do
|
197
|
+
countries = Country.republics.continent_and_capital('Europe', capital: 'Paris')
|
198
|
+
expect(countries.length).to be == 1
|
199
|
+
end
|
200
|
+
|
196
201
|
it 'can be used with arrays' do
|
197
202
|
countries = Country.where(id: [1,2])
|
198
203
|
expect(countries.length).to be == 2
|
data/spec/support/country.rb
CHANGED
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.
|
4
|
+
version: 0.20.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: 2020-
|
11
|
+
date: 2020-10-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: dedup
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: bundler
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -118,13 +132,11 @@ files:
|
|
118
132
|
- lib/frozen_record/backends/yaml.rb
|
119
133
|
- lib/frozen_record/base.rb
|
120
134
|
- lib/frozen_record/compact.rb
|
121
|
-
- lib/frozen_record/deduplication.rb
|
122
135
|
- lib/frozen_record/index.rb
|
123
136
|
- lib/frozen_record/railtie.rb
|
124
137
|
- lib/frozen_record/scope.rb
|
125
138
|
- lib/frozen_record/test_helper.rb
|
126
139
|
- lib/frozen_record/version.rb
|
127
|
-
- spec/deduplication_spec.rb
|
128
140
|
- spec/fixtures/animals.json
|
129
141
|
- spec/fixtures/cars.yml
|
130
142
|
- spec/fixtures/countries.yml.erb
|
@@ -156,12 +168,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
156
168
|
- !ruby/object:Gem::Version
|
157
169
|
version: '0'
|
158
170
|
requirements: []
|
159
|
-
rubygems_version: 3.
|
171
|
+
rubygems_version: 3.1.2
|
160
172
|
signing_key:
|
161
173
|
specification_version: 4
|
162
174
|
summary: ActiveRecord like interface to read only access and query static YAML files
|
163
175
|
test_files:
|
164
|
-
- spec/deduplication_spec.rb
|
165
176
|
- spec/fixtures/animals.json
|
166
177
|
- spec/fixtures/cars.yml
|
167
178
|
- spec/fixtures/countries.yml.erb
|
@@ -1,57 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'active_support/core_ext/object/duplicable'
|
4
|
-
|
5
|
-
module FrozenRecord
|
6
|
-
module Deduplication
|
7
|
-
extend self
|
8
|
-
|
9
|
-
# We deduplicate data in place because it is assumed it directly
|
10
|
-
# comes from the parser, and won't be held by anyone.
|
11
|
-
#
|
12
|
-
# Frozen Hashes and Arrays are ignored because they are likely
|
13
|
-
# the result of the use of YAML anchor. Meaning we already deduplicated
|
14
|
-
# them.
|
15
|
-
if RUBY_VERSION >= '2.7'
|
16
|
-
def deep_deduplicate!(data)
|
17
|
-
case data
|
18
|
-
when Hash
|
19
|
-
return data if data.frozen?
|
20
|
-
data.transform_keys! { |k| deep_deduplicate!(k) }
|
21
|
-
data.transform_values! { |v| deep_deduplicate!(v) }
|
22
|
-
data.freeze
|
23
|
-
when Array
|
24
|
-
return data if data.frozen?
|
25
|
-
data.map! { |d| deep_deduplicate!(d) }.freeze
|
26
|
-
when String
|
27
|
-
-data
|
28
|
-
else
|
29
|
-
data.duplicable? ? data.freeze : data
|
30
|
-
end
|
31
|
-
end
|
32
|
-
else
|
33
|
-
def deep_deduplicate!(data)
|
34
|
-
case data
|
35
|
-
when Hash
|
36
|
-
return data if data.frozen?
|
37
|
-
data.transform_keys! { |k| deep_deduplicate!(k) }
|
38
|
-
data.transform_values! { |v| deep_deduplicate!(v) }
|
39
|
-
data.freeze
|
40
|
-
when Array
|
41
|
-
return data if data.frozen?
|
42
|
-
data.map! { |d| deep_deduplicate!(d) }.freeze
|
43
|
-
when String
|
44
|
-
# String#-@ doesn't deduplicate the string if it's tainted.
|
45
|
-
# So in such case we need to untaint it first.
|
46
|
-
if data.tainted?
|
47
|
-
-(+data).untaint
|
48
|
-
else
|
49
|
-
-data
|
50
|
-
end
|
51
|
-
else
|
52
|
-
data.duplicable? ? data.freeze : data
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
data/spec/deduplication_spec.rb
DELETED
@@ -1,31 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe 'deduplication' do
|
4
|
-
|
5
|
-
it 'deduplicate string values' do
|
6
|
-
pending("Strings can't be deduplicated before Ruby 2.5") if RUBY_VERSION < '2.5'
|
7
|
-
|
8
|
-
records = [
|
9
|
-
{ 'name' => 'George'.dup },
|
10
|
-
]
|
11
|
-
|
12
|
-
expect(records[0]['name']).to_not equal 'George'.freeze
|
13
|
-
FrozenRecord::Deduplication.deep_deduplicate!(records)
|
14
|
-
expect(records[0]['name']).to equal 'George'.freeze
|
15
|
-
end
|
16
|
-
|
17
|
-
it 'handles duplicated references' do
|
18
|
-
# This simulates the YAML anchor behavior
|
19
|
-
tags = { 'foo' => 'bar' }
|
20
|
-
records = [
|
21
|
-
{ 'name' => 'George', 'tags' => tags },
|
22
|
-
{ 'name' => 'Peter', 'tags' => tags },
|
23
|
-
]
|
24
|
-
|
25
|
-
expect(records[0]['tags']).to_not be_frozen
|
26
|
-
FrozenRecord::Deduplication.deep_deduplicate!(records)
|
27
|
-
expect(records[0]['tags']).to be_frozen
|
28
|
-
expect(records[0]['tags']).to equal records[1]['tags']
|
29
|
-
end
|
30
|
-
|
31
|
-
end
|