frozen_record 0.27.0 → 0.27.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/main.yml +1 -1
- data/CHANGELOG.md +8 -0
- data/README.md +27 -9
- data/lib/frozen_record/base.rb +1 -1
- data/lib/frozen_record/test_helper.rb +18 -4
- data/lib/frozen_record/version.rb +1 -1
- data/spec/fixtures/test_helper/only_in_tests.yml.erb +3 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/test_helper_spec.rb +12 -0
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e47fc697be74381e4202860b94854c7634c3dbf689cf32acbe57d5c15bed9f83
|
4
|
+
data.tar.gz: 3e78652cd4102592513af618648f555bd936a96c97a71a9cc9af6a33bebd0d74
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d04577bfa1b97b5a0ada49555147cac4ce31c39187e10acf2e0367c80d00a224f0a5d2ec36d350775d92417fb92b6bfc67fa99c859a01628844805121c9a91e4
|
7
|
+
data.tar.gz: bfeed5194bd51706fae613df484de29f8629a314947e96c4abc6b65b1ce7b2eab26d950760c78368daa752b6ae8e1692d21d7383f493ac5a972f3f24049018a3
|
data/.github/workflows/main.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
# Unreleased
|
2
2
|
|
3
|
+
# v0.27.2
|
4
|
+
|
5
|
+
- Fix a Ruby warning that was emitted when `frozen_record` is loaded.
|
6
|
+
|
7
|
+
# v0.27.1
|
8
|
+
|
9
|
+
- TestHelper.unload_fixture: handle models without data.
|
10
|
+
|
3
11
|
# v0.27.0
|
4
12
|
|
5
13
|
- Allow to define some richer attibute types, somewhat akin to Active Record `serialize` attributes. See the README for more information.
|
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# FrozenRecord
|
2
2
|
|
3
|
-
[![Build Status](https://secure.travis-ci.org/byroot/frozen_record.svg)](
|
4
|
-
[![Gem Version](https://badge.fury.io/rb/frozen_record.svg)](
|
3
|
+
[![Build Status](https://secure.travis-ci.org/byroot/frozen_record.svg)](https://travis-ci.org/byroot/frozen_record)
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/frozen_record.svg)](https://badge.fury.io/rb/frozen_record)
|
5
5
|
|
6
6
|
Active Record-like interface for **read only** access to static data files of reasonable size.
|
7
7
|
|
@@ -42,9 +42,27 @@ class Country < FrozenRecord::Base
|
|
42
42
|
end
|
43
43
|
```
|
44
44
|
|
45
|
-
|
46
|
-
|
47
|
-
|
45
|
+
FrozenRecord has two built-in backends, for JSON and YAML.
|
46
|
+
Backends are classes that know how to load records from a static file.
|
47
|
+
|
48
|
+
The default backend is YAML and it expects a file that looks like this:
|
49
|
+
|
50
|
+
```yaml
|
51
|
+
- id: 'se'
|
52
|
+
name: 'Sweden'
|
53
|
+
region: 'Europe'
|
54
|
+
language: 'Swedish'
|
55
|
+
population: 10420000
|
56
|
+
- id: 'de'
|
57
|
+
name: 'Germany'
|
58
|
+
region: 'Europe'
|
59
|
+
language: 'German'
|
60
|
+
population: 83200000
|
61
|
+
|
62
|
+
# …
|
63
|
+
```
|
64
|
+
|
65
|
+
You can also specify a custom backend:
|
48
66
|
|
49
67
|
```ruby
|
50
68
|
class Country < FrozenRecord::Base
|
@@ -74,18 +92,18 @@ end
|
|
74
92
|
|
75
93
|
FrozenRecord aim to replicate only modern Active Record querying interface, and only the non "string typed" ones.
|
76
94
|
|
77
|
-
e.g
|
78
95
|
```ruby
|
79
96
|
# Supported query interfaces
|
80
97
|
Country.
|
81
98
|
where(region: 'Europe').
|
82
99
|
where.not(language: 'English').
|
100
|
+
where(population: 10_000_000..).
|
83
101
|
order(id: :desc).
|
84
102
|
limit(10).
|
85
103
|
offset(2).
|
86
104
|
pluck(:name)
|
87
105
|
|
88
|
-
# Non
|
106
|
+
# Non-supported query interfaces
|
89
107
|
Country.
|
90
108
|
where('region = "Europe" AND language != "English"').
|
91
109
|
order('id DESC')
|
@@ -217,7 +235,7 @@ FrozenRecord::TestHelper.unload_fixtures
|
|
217
235
|
Here's a Rails-specific example:
|
218
236
|
|
219
237
|
```ruby
|
220
|
-
require
|
238
|
+
require 'test_helper'
|
221
239
|
require 'frozen_record/test_helper'
|
222
240
|
|
223
241
|
class CountryTest < ActiveSupport::TestCase
|
@@ -230,7 +248,7 @@ class CountryTest < ActiveSupport::TestCase
|
|
230
248
|
FrozenRecord::TestHelper.unload_fixtures
|
231
249
|
end
|
232
250
|
|
233
|
-
test
|
251
|
+
test 'countries have a valid name' do
|
234
252
|
# ...
|
235
253
|
```
|
236
254
|
|
data/lib/frozen_record/base.rb
CHANGED
@@ -12,7 +12,7 @@ module FrozenRecord
|
|
12
12
|
|
13
13
|
return if @cache.key?(model_class)
|
14
14
|
|
15
|
-
@cache[model_class]
|
15
|
+
@cache[model_class] = base_path_if_file_present(model_class)
|
16
16
|
|
17
17
|
model_class.base_path = alternate_base_path
|
18
18
|
model_class.load_records(force: true)
|
@@ -26,9 +26,10 @@ module FrozenRecord
|
|
26
26
|
return unless @cache.key?(model_class)
|
27
27
|
|
28
28
|
old_base_path = @cache[model_class]
|
29
|
-
|
30
|
-
|
31
|
-
|
29
|
+
if old_base_path
|
30
|
+
model_class.base_path = old_base_path
|
31
|
+
model_class.load_records(force: true)
|
32
|
+
end
|
32
33
|
@cache.delete(model_class)
|
33
34
|
end
|
34
35
|
|
@@ -40,6 +41,19 @@ module FrozenRecord
|
|
40
41
|
|
41
42
|
private
|
42
43
|
|
44
|
+
# Checks for the existence of the file for the frozen_record in the default directory.
|
45
|
+
# Returns the base_path if the file is present, otherwise nil.
|
46
|
+
# Some tests define specific test classes that do ONLY exist in the alternate directory.
|
47
|
+
# As `unload_fixture(s)` tries to force load the default file, it would raise an error for
|
48
|
+
# the "test only" fixtures. The nil value in the cache handles that case gracefully.
|
49
|
+
def base_path_if_file_present(model_class)
|
50
|
+
if File.exist?(model_class.file_path)
|
51
|
+
model_class.base_path
|
52
|
+
else
|
53
|
+
nil
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
43
57
|
def ensure_model_class_is_frozenrecord(model_class)
|
44
58
|
unless model_class < FrozenRecord::Base
|
45
59
|
raise ArgumentError, "Model class (#{model_class}) does not inherit from #{FrozenRecord::Base}"
|
data/spec/spec_helper.rb
CHANGED
data/spec/test_helper_spec.rb
CHANGED
@@ -49,6 +49,18 @@ describe 'test fixture loading' do
|
|
49
49
|
expect(Continent.count).to be == 1
|
50
50
|
expect(Country.count).to be == 3
|
51
51
|
end
|
52
|
+
|
53
|
+
context "when the test fixture does not exist in normal base path" do
|
54
|
+
class OnlyInTest < FrozenRecord::Base; end
|
55
|
+
before do
|
56
|
+
test_fixtures_base_path = File.join(File.dirname(__FILE__), 'fixtures', 'test_helper')
|
57
|
+
FrozenRecord::TestHelper.load_fixture(OnlyInTest, test_fixtures_base_path)
|
58
|
+
end
|
59
|
+
it 'unload fixture gracefully recovers from an ' do
|
60
|
+
|
61
|
+
expect { FrozenRecord::TestHelper.unload_fixture(OnlyInTest) }.not_to raise_error
|
62
|
+
end
|
63
|
+
end
|
52
64
|
end
|
53
65
|
|
54
66
|
describe '.unload_fixtures' 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.27.
|
4
|
+
version: 0.27.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jean Boussier
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-07-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -95,6 +95,7 @@ files:
|
|
95
95
|
- spec/fixtures/prices.yml.erb
|
96
96
|
- spec/fixtures/test_helper/continents.yml.erb
|
97
97
|
- spec/fixtures/test_helper/countries.yml.erb
|
98
|
+
- spec/fixtures/test_helper/only_in_tests.yml.erb
|
98
99
|
- spec/frozen_record_spec.rb
|
99
100
|
- spec/scope_spec.rb
|
100
101
|
- spec/spec_helper.rb
|
@@ -124,7 +125,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
124
125
|
- !ruby/object:Gem::Version
|
125
126
|
version: '0'
|
126
127
|
requirements: []
|
127
|
-
rubygems_version: 3.
|
128
|
+
rubygems_version: 3.5.9
|
128
129
|
signing_key:
|
129
130
|
specification_version: 4
|
130
131
|
summary: ActiveRecord like interface to read only access and query static YAML files
|
@@ -136,6 +137,7 @@ test_files:
|
|
136
137
|
- spec/fixtures/prices.yml.erb
|
137
138
|
- spec/fixtures/test_helper/continents.yml.erb
|
138
139
|
- spec/fixtures/test_helper/countries.yml.erb
|
140
|
+
- spec/fixtures/test_helper/only_in_tests.yml.erb
|
139
141
|
- spec/frozen_record_spec.rb
|
140
142
|
- spec/scope_spec.rb
|
141
143
|
- spec/spec_helper.rb
|