frozen_record 0.27.0 → 0.27.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9ad9e1749e0b964178ce78eb8825cc64feddc347d6ac2952b5992cc97b5f4212
4
- data.tar.gz: 107c164a1ba9442bf7ab5fa59c03400502c2e31b8ce2e7429e2cf2992e38fcbc
3
+ metadata.gz: d790a4c35575afe0294cdcaa88f0f75bdee110623f40bfddd1ea4edade4df7c0
4
+ data.tar.gz: 0c5e6cd922365269c4a320a92c7b196749ce2016f5a8bdeb887bdc191a83ebcf
5
5
  SHA512:
6
- metadata.gz: 2297514000fb2ee38d534084285d16c3768a9795869e0af2955e134c6f08114f77128689d8aa8fd95fa775c7cba8732a9f34917a0b886d3286b07aa6a36f3f4e
7
- data.tar.gz: ce33c77289242c3f1d2341491c644226dad7b14fe709544c2f706d5e37c02c9649a59902f8eab9898e532eea89a0d84905b278bc299b64c217d9a53861d487c5
6
+ metadata.gz: af254fb45c82cd57300fd613b513f1e423f102dc9dd3ddc5a2c99f0abd4b44cbd72b104b471795c8e1b9e91cda27d27760eea30fde2c682caef91b5ef1cc30e9
7
+ data.tar.gz: db4fd7fc5e5d5f00566a246b29ba8c0f337bfbf7043b6925ce7492ac7862372f4098c85f341b6b01672de47345055b10a9269f691fb2d9f0ee0623d2d8425799
@@ -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,21 @@
1
1
  # Unreleased
2
2
 
3
+ # v0.27.4
4
+
5
+ - Also fix compatibility with Rails 8.0.0alpha: https://github.com/rails/rails/pull/52717. Missed a few places.
6
+
7
+ # v0.27.3
8
+
9
+ - Fix compatibility with Rails 8.0.0alpha: https://github.com/rails/rails/pull/52717
10
+
11
+ # v0.27.2
12
+
13
+ - Fix a Ruby warning that was emitted when `frozen_record` is loaded.
14
+
15
+ # v0.27.1
16
+
17
+ - TestHelper.unload_fixture: handle models without data.
18
+
3
19
  # v0.27.0
4
20
 
5
21
  - 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
 
@@ -28,18 +28,14 @@ module FrozenRecord
28
28
  FIND_BY_PATTERN = /\Afind_by_(\w+)(!?)/
29
29
  FALSY_VALUES = [false, nil, 0, -''].to_set
30
30
 
31
- class_attribute :base_path, :primary_key, :backend, :auto_reloading, :default_attributes, instance_accessor: false
31
+ class_attribute :base_path, :_primary_key, :backend, :auto_reloading, :_default_attributes, instance_accessor: false
32
32
  class_attribute :index_definitions, instance_accessor: false
33
33
  class_attribute :attribute_deserializers, instance_accessor: false
34
34
  class_attribute :max_records_scan, instance_accessor: false
35
35
  self.index_definitions = {}.freeze
36
36
  self.attribute_deserializers = {}.freeze
37
37
 
38
- self.primary_key = 'id'
39
-
40
- self.backend = FrozenRecord::Backends::Yaml
41
-
42
- attribute_method_suffix -'?'
38
+ attribute_method_suffix '?'
43
39
 
44
40
  class ThreadSafeStorage
45
41
 
@@ -68,23 +64,20 @@ module FrozenRecord
68
64
  self.max_records_scan = previous_max_records_scan
69
65
  end
70
66
 
71
- alias_method :set_default_attributes, :default_attributes=
72
- private :set_default_attributes
67
+ def default_attributes
68
+ _default_attributes
69
+ end
70
+
73
71
  def default_attributes=(default_attributes)
74
- set_default_attributes(default_attributes.transform_keys(&:to_s))
72
+ self._default_attributes = default_attributes.transform_keys(&:to_s)
75
73
  end
76
74
 
77
- alias_method :set_primary_key, :primary_key=
78
- private :set_primary_key
79
- def primary_key=(primary_key)
80
- set_primary_key(-primary_key.to_s)
75
+ def primary_key
76
+ _primary_key
81
77
  end
82
78
 
83
- alias_method :set_base_path, :base_path=
84
- private :set_base_path
85
- def base_path=(base_path)
86
- @file_path = nil
87
- set_base_path(base_path)
79
+ def primary_key=(primary_key)
80
+ self._primary_key = -primary_key.to_s
88
81
  end
89
82
 
90
83
  attr_accessor :abstract_class
@@ -115,13 +108,11 @@ module FrozenRecord
115
108
 
116
109
  def file_path
117
110
  raise ArgumentError, "You must define `#{name}.base_path`" unless base_path
118
- @file_path ||= begin
119
- file_path = File.join(base_path, backend.filename(name))
120
- if !File.exist?(file_path) && File.exist?("#{file_path}.erb")
121
- "#{file_path}.erb"
122
- else
123
- file_path
124
- end
111
+ file_path = File.join(base_path, backend.filename(name))
112
+ if !File.exist?(file_path) && File.exist?("#{file_path}.erb")
113
+ "#{file_path}.erb"
114
+ else
115
+ file_path
125
116
  end
126
117
  end
127
118
 
@@ -283,6 +274,10 @@ module FrozenRecord
283
274
 
284
275
  end
285
276
 
277
+ self.primary_key = 'id'
278
+
279
+ self.backend = FrozenRecord::Backends::Yaml
280
+
286
281
  def initialize(attrs = {})
287
282
  @attributes = attrs.freeze
288
283
  end
@@ -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.4'
5
5
  end
@@ -0,0 +1,3 @@
1
+ ---
2
+ - id: 1
3
+ name: Some continent
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,5 @@
1
+ $VERBOSE = true
2
+
1
3
  lib = File.expand_path('../lib', __FILE__)
2
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
5
 
@@ -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.4
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-08-27 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.17
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