frozen_record 0.20.1 → 0.21.0

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: 4c98f13b693b4c7bceae48a308722c3681a678033855f40995a10fd82c0faee8
4
- data.tar.gz: ff4ef784946b6fc9f067caf8e26d50433c3c396716ead1c953dd324528861ab2
3
+ metadata.gz: b998592ed0453f220a0679b3ee2aea49d1d5381a3a7c4d6e9286434e6e3b6c27
4
+ data.tar.gz: 3f430857f5b5626951360d2c28402c58b5751cb1ef2805f139558d600b7e1e1c
5
5
  SHA512:
6
- metadata.gz: e25090babf81d5ffa18a26acecf5af68d3424866b848508838ded880196d2178f994ce586be9bee8d63db71b3c84159ed2b4fac2c7e90f26414b9fbff4c69598
7
- data.tar.gz: 384749f211db239a1d3964c16d5c23d4af4261ee052d6c0fa16f15f0d6a0ce5710df898b1f4e290c9cfa77c553d214668ab383ca33b09fbb82a03266db7a0324
6
+ metadata.gz: cbc8307030df2b393d69499c7be236ef62349ad6f74b7fc9de914260748fd0919e293204b30ba31e377acf157acb90e9371d4ed50a77da955f60222694f74892
7
+ data.tar.gz: f211b9298137e71e098e841658341f0cceb3dd902f7974a5a9a7c2233173bd1016037832ba744f8966beb9045fa9c93068365ad5d9b7d64f9b68631e1b840ca1
@@ -0,0 +1,29 @@
1
+ name: CI
2
+
3
+ on: [push, pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ strategy:
9
+ matrix:
10
+ ruby: [ '2.5', '2.6', '2.7', '3.0' ]
11
+ name: Ruby ${{ matrix.ruby }} tests
12
+ steps:
13
+ - uses: actions/checkout@v2
14
+ - name: Setup Ruby
15
+ uses: ruby/setup-ruby@v1
16
+ with:
17
+ ruby-version: ${{ matrix.ruby }}
18
+ - uses: actions/cache@v2
19
+ with:
20
+ path: vendor/bundle
21
+ key: ${{ runner.os }}-${{ matrix.ruby }}-gems-${{ hashFiles('Gemfile', 'frozen_record.gemspec') }}
22
+ restore-keys: |
23
+ ${{ runner.os }}-${{ matrix.ruby }}-gems-
24
+ - name: Bundle install
25
+ run: |
26
+ gem install bundler
27
+ bundle install --jobs 4 --retry 3 --path=vendor/bundle
28
+ - name: Run tests
29
+ run: bundle exec rake
@@ -22,7 +22,6 @@ Gem::Specification.new do |spec|
22
22
  spec.add_runtime_dependency 'activemodel'
23
23
  spec.add_runtime_dependency 'dedup'
24
24
 
25
- spec.add_development_dependency 'bundler'
26
25
  spec.add_development_dependency 'rake'
27
26
  spec.add_development_dependency 'rspec'
28
27
  spec.add_development_dependency 'pry'
@@ -1,30 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'yaml'
4
- require 'set'
5
- require 'active_support/all'
6
- require 'active_model'
7
-
8
- require 'dedup'
9
-
10
- require 'frozen_record/version'
11
- require 'frozen_record/scope'
12
- require 'frozen_record/index'
13
- require 'frozen_record/base'
14
- require 'frozen_record/compact'
15
-
16
- module FrozenRecord
17
- RecordNotFound = Class.new(StandardError)
18
-
19
- class << self
20
- attr_accessor :deprecated_yaml_erb_backend
21
-
22
- def eager_load!
23
- Base.descendants.each(&:eager_load!)
24
- end
25
- end
26
-
27
- self.deprecated_yaml_erb_backend = true
28
- end
29
-
3
+ require 'frozen_record/minimal'
4
+ require 'frozen_record/serialization'
30
5
  require 'frozen_record/railtie' if defined?(Rails)
@@ -9,9 +9,26 @@ module FrozenRecord
9
9
  "#{model_name.underscore.pluralize}.json"
10
10
  end
11
11
 
12
- def load(file_path)
13
- json_data = File.read(file_path)
14
- JSON.parse(json_data) || []
12
+ if JSON.respond_to?(:load_file)
13
+ supports_freeze = begin
14
+ JSON.load_file(File.expand_path('../empty.json', __FILE__), freeze: true)
15
+ rescue ArgumentError
16
+ false
17
+ end
18
+
19
+ if supports_freeze
20
+ def load(file_path)
21
+ JSON.load_file(file_path, freeze: true) || Dedup::EMPTY_ARRAY
22
+ end
23
+ else
24
+ def load(file_path)
25
+ Dedup.deep_intern!(JSON.load_file(file_path) || Dedup::EMPTY_ARRAY)
26
+ end
27
+ end
28
+ else
29
+ def load(file_path)
30
+ Dedup.deep_intern!(JSON.parse(File.read(file_path)) || Dedup::EMPTY_ARRAY)
31
+ end
15
32
  end
16
33
  end
17
34
  end
@@ -34,15 +34,41 @@ module FrozenRecord
34
34
  end
35
35
  end
36
36
 
37
- YAML.load(yml_data) || []
37
+ load_string(yml_data)
38
38
  else
39
39
  if file_path.end_with?('.erb')
40
- YAML.load(ERB.new(File.read(file_path)).result) || []
40
+ load_string(ERB.new(File.read(file_path)).result)
41
41
  else
42
- YAML.load_file(file_path) || []
42
+ load_file(file_path)
43
43
  end
44
44
  end
45
45
  end
46
+
47
+ private
48
+
49
+ supports_freeze = begin
50
+ YAML.load_file(File.expand_path('../empty.json', __FILE__), freeze: true)
51
+ rescue ArgumentError
52
+ false
53
+ end
54
+
55
+ if supports_freeze
56
+ def load_file(path)
57
+ YAML.load_file(path, freeze: true) || Dedup::EMPTY_ARRAY
58
+ end
59
+
60
+ def load_string(yaml)
61
+ YAML.load(yaml, freeze: true) || Dedup::EMPTY_ARRAY
62
+ end
63
+ else
64
+ def load_file(path)
65
+ Dedup.deep_intern!(YAML.load_file(path) || Dedup::EMPTY_ARRAY)
66
+ end
67
+
68
+ def load_string(yaml)
69
+ Dedup.deep_intern!(YAML.load(yaml) || Dedup::EMPTY_ARRAY)
70
+ end
71
+ end
46
72
  end
47
73
  end
48
74
  end
@@ -1,9 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'set'
4
3
  require 'active_support/descendants_tracker'
5
4
  require 'frozen_record/backends'
6
- require 'objspace'
7
5
 
8
6
  module FrozenRecord
9
7
  class Base
@@ -11,11 +9,6 @@ module FrozenRecord
11
9
  extend ActiveModel::Naming
12
10
  include ActiveModel::Conversion
13
11
  include ActiveModel::AttributeMethods
14
- include ActiveModel::Serializers::JSON
15
-
16
- if defined? ActiveModel::Serializers::Xml
17
- include ActiveModel::Serializers::Xml
18
- end
19
12
 
20
13
  FIND_BY_PATTERN = /\Afind_by_(\w+)(!?)/
21
14
  FALSY_VALUES = [false, nil, 0, -''].to_set
@@ -52,7 +45,7 @@ module FrozenRecord
52
45
  alias_method :set_default_attributes, :default_attributes=
53
46
  private :set_default_attributes
54
47
  def default_attributes=(default_attributes)
55
- set_default_attributes(Dedup.deep_intern!(default_attributes.stringify_keys))
48
+ set_default_attributes(Dedup.deep_intern!(default_attributes.transform_keys(&:to_s)))
56
49
  end
57
50
 
58
51
  alias_method :set_primary_key, :primary_key=
@@ -146,8 +139,9 @@ module FrozenRecord
146
139
 
147
140
  @records ||= begin
148
141
  records = backend.load(file_path)
149
- records.each { |r| assign_defaults!(r) }
150
- records = Dedup.deep_intern!(records)
142
+ if default_attributes
143
+ records = records.map { |r| assign_defaults!(r.dup).freeze }.freeze
144
+ end
151
145
  @attributes = list_attributes(records).freeze
152
146
  define_attribute_methods(@attributes.to_a)
153
147
  records = records.map { |r| load(r) }.freeze
@@ -164,7 +158,7 @@ module FrozenRecord
164
158
  private :load
165
159
 
166
160
  def new(attrs = {})
167
- load(assign_defaults!(attrs.stringify_keys))
161
+ load(assign_defaults!(attrs.transform_keys(&:to_s)))
168
162
  end
169
163
 
170
164
  private
@@ -13,8 +13,9 @@ module FrozenRecord
13
13
 
14
14
  @records ||= begin
15
15
  records = backend.load(file_path)
16
- records.each { |r| assign_defaults!(r) }
17
- records = Dedup.deep_intern!(records)
16
+ if default_attributes
17
+ records = records.map { |r| assign_defaults!(r.dup).freeze }.freeze
18
+ end
18
19
  @attributes = list_attributes(records).freeze
19
20
  build_attributes_cache
20
21
  define_attribute_methods(@attributes.to_a)
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yaml'
4
+ require 'set'
5
+ require 'active_model'
6
+
7
+ require 'dedup'
8
+
9
+ require 'frozen_record/version'
10
+ require 'frozen_record/scope'
11
+ require 'frozen_record/index'
12
+ require 'frozen_record/base'
13
+ require 'frozen_record/compact'
14
+
15
+ module FrozenRecord
16
+ RecordNotFound = Class.new(StandardError)
17
+
18
+ class << self
19
+ attr_accessor :deprecated_yaml_erb_backend
20
+
21
+ def eager_load!
22
+ Base.descendants.each(&:eager_load!)
23
+ end
24
+ end
25
+
26
+ self.deprecated_yaml_erb_backend = true
27
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'frozen_record/base'
4
+
5
+ module FrozenRecord
6
+ class Base
7
+ include ActiveModel::Serializers::JSON
8
+
9
+ if defined? ActiveModel::Serializers::Xml
10
+ include ActiveModel::Serializers::Xml
11
+ end
12
+ end
13
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FrozenRecord
4
- VERSION = '0.20.1'
4
+ VERSION = '0.21.0'
5
5
  end
@@ -10,6 +10,7 @@ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
10
10
  ]
11
11
  SimpleCov.start
12
12
 
13
+ require 'objspace'
13
14
  require 'frozen_record'
14
15
  require 'frozen_record/test_helper'
15
16
 
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.20.1
4
+ version: 0.21.0
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-10-19 00:00:00.000000000 Z
11
+ date: 2021-01-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -38,20 +38,6 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
- - !ruby/object:Gem::Dependency
42
- name: bundler
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
41
  - !ruby/object:Gem::Dependency
56
42
  name: rake
57
43
  requirement: !ruby/object:Gem::Requirement
@@ -115,9 +101,9 @@ executables: []
115
101
  extensions: []
116
102
  extra_rdoc_files: []
117
103
  files:
104
+ - ".github/workflows/main.yml"
118
105
  - ".gitignore"
119
106
  - ".rspec"
120
- - ".travis.yml"
121
107
  - Gemfile
122
108
  - LICENSE.txt
123
109
  - README.md
@@ -128,13 +114,16 @@ files:
128
114
  - frozen_record.gemspec
129
115
  - lib/frozen_record.rb
130
116
  - lib/frozen_record/backends.rb
117
+ - lib/frozen_record/backends/empty.json
131
118
  - lib/frozen_record/backends/json.rb
132
119
  - lib/frozen_record/backends/yaml.rb
133
120
  - lib/frozen_record/base.rb
134
121
  - lib/frozen_record/compact.rb
135
122
  - lib/frozen_record/index.rb
123
+ - lib/frozen_record/minimal.rb
136
124
  - lib/frozen_record/railtie.rb
137
125
  - lib/frozen_record/scope.rb
126
+ - lib/frozen_record/serialization.rb
138
127
  - lib/frozen_record/test_helper.rb
139
128
  - lib/frozen_record/version.rb
140
129
  - spec/fixtures/animals.json
@@ -168,7 +157,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
168
157
  - !ruby/object:Gem::Version
169
158
  version: '0'
170
159
  requirements: []
171
- rubygems_version: 3.1.2
160
+ rubygems_version: 3.1.4
172
161
  signing_key:
173
162
  specification_version: 4
174
163
  summary: ActiveRecord like interface to read only access and query static YAML files
@@ -1,5 +0,0 @@
1
- sudo: false
2
- rvm:
3
- - '2.5'
4
- - '2.6'
5
- - '2.7'