frozen_record 0.14.0 → 0.15.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
- SHA1:
3
- metadata.gz: aca3d3a4084fea57cbcbc396e95d35ee24750b44
4
- data.tar.gz: 5fe3827d178da90300e05c49062e1c1259bc366d
2
+ SHA256:
3
+ metadata.gz: ce0b59f2960394e86f2278a1c574680bfc27491bead98f9991ba70aa9a90766e
4
+ data.tar.gz: c194c8fa251cc4b9e6be6a41869ad383e4aa0ebd54021ddb36b2b285257dc347
5
5
  SHA512:
6
- metadata.gz: 6afd96b57e571297ebd78bc218c0cfe8e06249be474231a50a3f573ad74f2ee8236bc289265362d67067769e7313acd7633ff721bf1349b868d23f917c94c2bc
7
- data.tar.gz: cfd9d2c57b1238c8df69d311a738cc31d90138523b451f7cabc6bcf7a0c54f553740d47fbf10c92a9f80065e2661c9618d00f97db6975d259262dbcceb2bd84e
6
+ metadata.gz: 6bf7ce5df1f3a4c2dd67e4b391fd26bb3499ec572bf4de600bdf50728292959cef2330844d19e3deebec714183da7931802bb735fd5c2d0281541235f07fcb3d
7
+ data.tar.gz: 18b96a420a7f74e12c57362b856609b685e60923e4eb39c83d1d0623321f8dade128d53f6435008431589d22f218200c9d3c42a497ead3406013714208a09cdb
data/README.md CHANGED
@@ -95,10 +95,12 @@ Country.
95
95
 
96
96
  ### Scopes
97
97
 
98
- While the `scope :symbol, lambda` syntax is not supported, the class methods way is:
98
+ Basic `scope :symbol, lambda` syntax is now supported in addition to class method syntax.
99
99
 
100
100
  ```ruby
101
101
  class Country
102
+ scope :european, -> { where(continent: 'Europe' ) }
103
+
102
104
  def self.republics
103
105
  where(king: nil)
104
106
  end
@@ -108,7 +110,7 @@ class Country
108
110
  end
109
111
  end
110
112
 
111
- Country.republics.part_of_nato.order(id: :desc)
113
+ Country.european.republics.part_of_nato.order(id: :desc)
112
114
  ```
113
115
 
114
116
  ### Supported query methods
@@ -181,7 +183,7 @@ class CountryTest < ActiveSupport::TestCase
181
183
  teardown do
182
184
  FrozenRecord::TestHelper.unload_fixtures
183
185
  end
184
-
186
+
185
187
  test "countries have a valid name" do
186
188
  # ...
187
189
  ```
@@ -106,6 +106,13 @@ module FrozenRecord
106
106
  end
107
107
  end
108
108
 
109
+ def scope(name, body)
110
+ unless body.respond_to?(:call)
111
+ raise ArgumentError, "The scope body needs to be callable."
112
+ end
113
+ singleton_class.send(:define_method, name) { |*args| body.call(*args) }
114
+ end
115
+
109
116
  private
110
117
 
111
118
  def file_changed?
@@ -122,8 +122,28 @@ module FrozenRecord
122
122
  array_delegable?(method_name) || @klass.respond_to?(method_name) || super
123
123
  end
124
124
 
125
+ def hash
126
+ comparable_attributes.hash
127
+ end
128
+
129
+ def ==(other)
130
+ self.class === other &&
131
+ comparable_attributes == other.comparable_attributes
132
+ end
133
+
125
134
  protected
126
135
 
136
+ def comparable_attributes
137
+ @comparable_attributes ||= {
138
+ klass: @klass,
139
+ where_values: @where_values.uniq.sort,
140
+ where_not_values: @where_not_values.uniq.sort,
141
+ order_values: @order_values.uniq,
142
+ limit: @limit,
143
+ offset: @offset,
144
+ }
145
+ end
146
+
127
147
  def scoping
128
148
  previous, @klass.current_scope = @klass.current_scope, self
129
149
  yield
@@ -136,6 +156,7 @@ module FrozenRecord
136
156
  end
137
157
 
138
158
  def clear_cache!
159
+ @comparable_attributes = nil
139
160
  @results = nil
140
161
  @matches = nil
141
162
  self
@@ -230,6 +251,7 @@ module FrozenRecord
230
251
  end
231
252
 
232
253
  private
254
+
233
255
  def compare_value(actual, requested)
234
256
  return actual == requested unless requested.is_a?(Array) || requested.is_a?(Range)
235
257
  requested.include?(actual)
@@ -1,3 +1,3 @@
1
1
  module FrozenRecord
2
- VERSION = '0.14.0'
2
+ VERSION = '0.15.0'
3
3
  end
@@ -8,6 +8,7 @@
8
8
  updated_at: 2014-02-24T19:08:06-05:00
9
9
  nato: true
10
10
  king: Elisabeth II
11
+ continent: North America
11
12
 
12
13
  - id: 2
13
14
  name: France
@@ -16,6 +17,9 @@
16
17
  population: 65.7
17
18
  founded_on: 486-01-01
18
19
  updated_at: 2014-02-12T19:02:03-02:00
20
+ nato: true
21
+ continent: Europe
22
+
19
23
 
20
24
  - id: 3
21
25
  name: Austria
@@ -24,3 +28,4 @@
24
28
  population: 8.462
25
29
  founded_on: 1156-01-01
26
30
  updated_at: 2014-02-12T19:02:03-02:00
31
+ continent: Europe
@@ -71,6 +71,15 @@ describe FrozenRecord::Base do
71
71
 
72
72
  end
73
73
 
74
+ describe '.scope' do
75
+ it 'defines a scope method' do
76
+
77
+ Country.scope :north_american, -> { Country.where(continent: 'North America') }
78
+ expect(Country).to respond_to(:north_american)
79
+ expect(Country.north_american.first.name).to be == 'Canada'
80
+ end
81
+ end
82
+
74
83
  describe '#load_records' do
75
84
 
76
85
  it 'processes erb by default' do
@@ -124,6 +133,7 @@ describe FrozenRecord::Base do
124
133
  'updated_at' => Time.parse('2014-02-24T19:08:06-05:00'),
125
134
  'king' => 'Elisabeth II',
126
135
  'nato' => true,
136
+ 'continent' => 'North America',
127
137
  }
128
138
  end
129
139
 
data/spec/scope_spec.rb CHANGED
@@ -417,6 +417,23 @@ describe 'querying' do
417
417
 
418
418
  end
419
419
 
420
+ describe '#==' do
421
+ it 'returns true when two scopes share the same hashed attributes' do
422
+ scope_a = Country.republics.nato
423
+ scope_b = Country.republics.nato
424
+ expect(scope_a.object_id).not_to be == scope_b.object_id
425
+ expect(scope_a).to be == scope_b
426
+ end
427
+
428
+ it 'returns true when the same scope has be rechained' do
429
+ scope_a = Country.nato.republics.nato.republics
430
+ scope_b = Country.republics.nato
431
+ expect(scope_a.instance_variable_get(:@where_values)).to be == [[:nato, true ], [:king, nil ], [:nato, true], [:king, nil]]
432
+ expect(scope_b.instance_variable_get(:@where_values)).to be == [[:king, nil ], [:nato, true]]
433
+ expect(scope_a).to be == scope_b
434
+ end
435
+ end
436
+
420
437
  describe 'class methods delegation' do
421
438
 
422
439
  it 'can be called from a scope' do
@@ -4,6 +4,10 @@ class Country < FrozenRecord::Base
4
4
  where(king: nil)
5
5
  end
6
6
 
7
+ def self.nato
8
+ where(nato: true)
9
+ end
10
+
7
11
  def reverse_name
8
12
  name.reverse
9
13
  end
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.14.0
4
+ version: 0.15.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: 2019-04-02 00:00:00.000000000 Z
11
+ date: 2019-08-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -146,8 +146,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
146
146
  - !ruby/object:Gem::Version
147
147
  version: '0'
148
148
  requirements: []
149
- rubyforge_project:
150
- rubygems_version: 2.5.1
149
+ rubygems_version: 3.0.4
151
150
  signing_key:
152
151
  specification_version: 4
153
152
  summary: ActiveRecord like interface to read only access and query static YAML files