frozen_record 0.27.0 → 0.27.1
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 +4 -0
- data/README.md +27 -9
- 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/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: 38d59eb398601918420c427ca2608c32d88e272948f39f502c831e92be2d6017
|
4
|
+
data.tar.gz: 1762510ed02e71fdfc67fcfe6f41ba4db83f766231481490e0172a4ea0c91f5b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a58d5c272f4beb88c10a2b8186f6664843da73ee67a920a706258b1d3fd0c719fe7dcfd27d1f97485304f743c2cef86b8d5ed8924d59c69d162edf389fb0ab34
|
7
|
+
data.tar.gz: 1d587f2b2c7043e1137537ae0eb44fd84e1643876d8ca30d1704dc41c45b0739fe6612c71db97d14f32de3950e52fee289fcec34969e57505b7aebb5403264e3
|
data/.github/workflows/main.yml
CHANGED
data/CHANGELOG.md
CHANGED
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
|
|
@@ -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/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.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:
|
11
|
+
date: 2024-02-19 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.3
|
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
|