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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9ad9e1749e0b964178ce78eb8825cc64feddc347d6ac2952b5992cc97b5f4212
4
- data.tar.gz: 107c164a1ba9442bf7ab5fa59c03400502c2e31b8ce2e7429e2cf2992e38fcbc
3
+ metadata.gz: 38d59eb398601918420c427ca2608c32d88e272948f39f502c831e92be2d6017
4
+ data.tar.gz: 1762510ed02e71fdfc67fcfe6f41ba4db83f766231481490e0172a4ea0c91f5b
5
5
  SHA512:
6
- metadata.gz: 2297514000fb2ee38d534084285d16c3768a9795869e0af2955e134c6f08114f77128689d8aa8fd95fa775c7cba8732a9f34917a0b886d3286b07aa6a36f3f4e
7
- data.tar.gz: ce33c77289242c3f1d2341491c644226dad7b14fe709544c2f706d5e37c02c9649a59902f8eab9898e532eea89a0d84905b278bc299b64c217d9a53861d487c5
6
+ metadata.gz: a58d5c272f4beb88c10a2b8186f6664843da73ee67a920a706258b1d3fd0c719fe7dcfd27d1f97485304f743c2cef86b8d5ed8924d59c69d162edf389fb0ab34
7
+ data.tar.gz: 1d587f2b2c7043e1137537ae0eb44fd84e1643876d8ca30d1704dc41c45b0739fe6612c71db97d14f32de3950e52fee289fcec34969e57505b7aebb5403264e3
@@ -12,7 +12,7 @@ jobs:
12
12
  minimal: [ false, true ]
13
13
  name: Ruby ${{ matrix.ruby }} tests, minimal=${{ matrix.minimal }}
14
14
  steps:
15
- - uses: actions/checkout@v3
15
+ - uses: actions/checkout@v4
16
16
  - name: Set up Ruby
17
17
  uses: ruby/setup-ruby@v1
18
18
  with:
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Unreleased
2
2
 
3
+ # v0.27.1
4
+
5
+ - TestHelper.unload_fixture: handle models without data.
6
+
3
7
  # v0.27.0
4
8
 
5
9
  - 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)](http://travis-ci.org/byroot/frozen_record)
4
- [![Gem Version](https://badge.fury.io/rb/frozen_record.svg)](http://badge.fury.io/rb/frozen_record)
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
- You can also specify a custom backend. Backends are classes that know how to
46
- load records from a static file. By default FrozenRecord expects an YAML file,
47
- but this option can be changed per model:
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 supported query interfaces
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 "test_helper"
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 "countries have a valid name" do
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] ||= model_class.base_path
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
- model_class.base_path = old_base_path
30
- model_class.load_records(force: true)
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}"
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FrozenRecord
4
- VERSION = '0.27.0'
4
+ VERSION = '0.27.1'
5
5
  end
@@ -0,0 +1,3 @@
1
+ ---
2
+ - id: 1
3
+ name: Some continent
@@ -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.0
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: 2023-03-08 00:00:00.000000000 Z
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.4.6
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