active_repository 0.3.6 → 0.3.7

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
  SHA1:
3
- metadata.gz: ff6e4e5fadd2500e1f73c3b4312a951dcb45e62c
4
- data.tar.gz: 95ff3cbc00194b0c0a8a43c777857e6bd88a0736
3
+ metadata.gz: e7508e60aba070d40b81e20055ffbba52c2c3ee3
4
+ data.tar.gz: c6565367f7ed1dafbeccd9d5916477b5555b35de
5
5
  SHA512:
6
- metadata.gz: ca450e5b9d32696ab4cffdf1ba1569ebf94265c116104fa1c23e084da35f6828f64af1149e5d313593c58a3fe546d6cc23a93ecd7329fd91770c2c8546fb4d5b
7
- data.tar.gz: 471e41743a6f70997f6f19729cd48288b285243199e25347c06e56a7cf15a74c3e86d33ad5473ba16ee235e7d86be77a0c9d7bf7fcaf2f78002d5349ed851d48
6
+ metadata.gz: b58a6448b9691f89993a61b389d6ecb1a03dc7340639661050feef60326f571c816fa6c9b67916d56c495eef3fc1f6df2f6156613d12c58f4fddff4eb6107989
7
+ data.tar.gz: 5c373400c319b8b5036024f52349c425dc45d1c38b473eaafac03045aa4e7918a7168503afa932fdbc9f4db8c64cb5f3403d86a0958346ec245c6155c9b4bb4b
@@ -30,12 +30,12 @@ Gem::Specification.new do |gem|
30
30
  ]
31
31
 
32
32
  gem.add_runtime_dependency(%q<active_hash>, [">= 1.2.3"])
33
- gem.add_runtime_dependency(%q<activemodel>, [">= 3.2"])
33
+ gem.add_runtime_dependency(%q<activemodel>, [">= 4.1.4"])
34
34
  gem.add_runtime_dependency(%q<sql_query_executor>, [">= 0.3.6"])
35
35
  gem.add_development_dependency(%q<pry>)
36
- gem.add_development_dependency(%q<rspec>, [">= 2.2.0"])
37
- gem.add_development_dependency(%q<activerecord>, [">= 3.2"])
38
- gem.add_development_dependency(%q<mongoid>, [">= 3.1"])
36
+ gem.add_development_dependency(%q<rspec>, [">= 2.14.1"])
37
+ gem.add_development_dependency(%q<activerecord>, [">= 4.1.4"])
38
+ gem.add_development_dependency(%q<mongoid>, [">= 4.0.0"])
39
39
  gem.add_development_dependency('rake', [">= 10.0.0"])
40
40
  gem.add_development_dependency('coveralls')
41
41
  gem.add_development_dependency(%q<sqlite3>) unless RUBY_PLATFORM == 'java'
@@ -0,0 +1,24 @@
1
+ module ActiveRepository
2
+ module Callback
3
+ class Base
4
+ def initialize(object, method, options={})
5
+ @object = object
6
+ @method = method
7
+ @options = options
8
+ end
9
+
10
+ def call
11
+ @object.send(@method) if can_run?
12
+ end
13
+
14
+ private
15
+ def can_run?
16
+ return @can_run if @can_run
17
+
18
+ if_option = @options[:if].nil? ? true : @object.send(@options[:if])
19
+ unless_option = @options[:unless].nil? ? false : @object.send(@options[:unless])
20
+ @can_run = if_option && !unless_option
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,28 @@
1
+ module ActiveRepository
2
+ module Callback
3
+ class Pool
4
+ def initialize
5
+ @pools = {}
6
+ end
7
+
8
+ def add(pool, callback)
9
+ return false unless can_add?(pool, callback)
10
+
11
+ @pools[pool] ||= []
12
+
13
+ @pools[pool] << callback
14
+
15
+ true
16
+ end
17
+
18
+ def get(pool='')
19
+ @pools[pool] || []
20
+ end
21
+
22
+ private
23
+ def can_add?(pool, callback)
24
+ pool && callback.is_a?(Base) && !get(pool).include?(callback)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -1,3 +1,4 @@
1
+ require 'pry'
1
2
  class ActiveRepository::ResultSet
2
3
  def initialize(klass, query={}, attributes={})
3
4
  @klass = klass
@@ -13,6 +14,18 @@ class ActiveRepository::ResultSet
13
14
  all.each(&block)
14
15
  end
15
16
 
17
+ def empty?
18
+ all.empty?
19
+ end
20
+
21
+ def any?
22
+ all.any?
23
+ end
24
+
25
+ def map(&block)
26
+ all.map(&block)
27
+ end
28
+
16
29
  def pluck(attribute)
17
30
  all.map(&attribute)
18
31
  end
@@ -52,7 +65,7 @@ class ActiveRepository::ResultSet
52
65
  end
53
66
 
54
67
  def where(query)
55
- @attributes = @attributes.merge(query) if query.is_a?(Hash)
68
+ @attributes = @attributes.merge(SqlQueryExecutor::Query::Normalizers::QueryNormalizer.attributes_from_query(query))
56
69
  query = join_query(query, 'and')
57
70
 
58
71
  ActiveRepository::ResultSet.new(@klass, query, @attributes)
@@ -2,12 +2,9 @@ module ActiveModel
2
2
  module Validations
3
3
  class UniquenessValidator < ActiveModel::EachValidator #:nodoc:
4
4
  def initialize(options)
5
- super(options.reverse_merge(:case_sensitive => true))
6
- end
7
-
8
- # Unfortunately, we have to tie Uniqueness validators to a class.
9
- def setup(klass)
10
- @klass = klass
5
+ super
6
+ options.reverse_merge(:case_sensitive => true)
7
+ options[:class].send :attr_accessor, :custom_attribute
11
8
  end
12
9
 
13
10
  def validate_each(record, attribute, value)
@@ -1,3 +1,3 @@
1
1
  module ActiveRepository
2
- VERSION = "0.3.6"
2
+ VERSION = "0.3.7"
3
3
  end
@@ -1,22 +1,6 @@
1
1
  require 'active_hash'
2
2
  require 'sql_query_executor'
3
3
 
4
- # Changes made in order to make write support in ActiveHash.
5
-
6
- begin
7
- klass = Module.const_get(ActiveRecord::Rollback)
8
- unless klass.is_a?(Class)
9
- raise "Not defined"
10
- end
11
- rescue
12
- module ActiveRecord
13
- class ActiveRecordError < StandardError
14
- end
15
- class Rollback < ActiveRecord::ActiveRecordError
16
- end
17
- end
18
- end
19
-
20
4
  module ActiveHash
21
5
  class Base
22
6
  def initialize(attributes = {})
@@ -219,7 +219,165 @@ describe ActiveRepository::ResultSet, :result_set do
219
219
  end
220
220
  end
221
221
 
222
+ describe '#each' do
223
+ subject { ActiveRepository::ResultSet.new(Country) }
224
+
225
+ context 'when result_set is not empty' do
226
+ before do
227
+ Country.delete_all
228
+ Country.create(name: 'Canada', continent: 'America')
229
+ Country.create(name: 'Russia', continent: 'Europe')
230
+ Country.create(name: 'USA', continent: 'America')
231
+ Country.create(name: 'Brazil', continent: 'America')
232
+ end
233
+
234
+ it 'returns an array of objects' do
235
+ objects = subject.where(continent: 'America').each
236
+
237
+ expect(objects.class).to eq Enumerator
238
+ end
239
+
240
+ it 'returns a collection of Countries' do
241
+ objects = subject.where(continent: 'America').each
242
+
243
+ expect(objects.map(&:class).uniq).to eq [Country]
244
+ end
245
+
246
+ it 'executes given method on all objects' do
247
+ objects = subject.where('id is not null').each(&:delete)
248
+
249
+ expect(Country.count).to eq 0
250
+ end
251
+ end
252
+
253
+ context 'when result_set is empty' do
254
+ it 'returns nil' do
255
+ expect(subject.where(continent: 'America').each).not_to be_any
256
+ end
257
+ end
258
+ end
259
+
260
+ describe '#empty?' do
261
+ subject { ActiveRepository::ResultSet.new(Country) }
262
+
263
+ context 'when result_set is not empty' do
264
+ before do
265
+ Country.delete_all
266
+ Country.create(name: 'Canada', continent: 'America')
267
+ Country.create(name: 'Russia', continent: 'Europe')
268
+ Country.create(name: 'USA', continent: 'America')
269
+ Country.create(name: 'Brazil', continent: 'America')
270
+ end
271
+
272
+ it 'returns false' do
273
+ result = subject.where(continent: 'America').empty?
274
+
275
+ expect(result).not_to be
276
+ end
277
+ end
278
+
279
+ context 'when result_set is empty' do
280
+ it 'returns true' do
281
+ result = subject.where(continent: 'Americas').empty?
282
+
283
+ expect(result).to be
284
+ end
285
+ end
286
+ end
287
+
288
+ describe '#any?' do
289
+ subject { ActiveRepository::ResultSet.new(Country) }
290
+
291
+ context 'when result_set is not empty' do
292
+ before do
293
+ Country.delete_all
294
+ Country.create(name: 'Canada', continent: 'America')
295
+ Country.create(name: 'Russia', continent: 'Europe')
296
+ Country.create(name: 'USA', continent: 'America')
297
+ Country.create(name: 'Brazil', continent: 'America')
298
+ end
299
+
300
+ it 'returns true' do
301
+ result = subject.where(continent: 'America').any?
302
+
303
+ expect(result).to be
304
+ end
305
+ end
306
+
307
+ context 'when result_set is empty' do
308
+ it 'returns false' do
309
+ result = subject.where(continent: 'Americas').any?
310
+
311
+ expect(result).not_to be
312
+ end
313
+ end
314
+ end
315
+
316
+ describe '#map' do
317
+ subject { ActiveRepository::ResultSet.new(Country) }
318
+
319
+ context 'when result_set is not empty' do
320
+ before do
321
+ Country.delete_all
322
+ Country.create(name: 'Canada', continent: 'America')
323
+ Country.create(name: 'Russia', continent: 'Europe')
324
+ Country.create(name: 'USA', continent: 'America')
325
+ Country.create(name: 'Brazil', continent: 'America')
326
+ end
327
+
328
+ it 'returns an array with given attributes' do
329
+ result = subject.where(continent: 'America').map(&:name)
330
+
331
+ expect(result).to eq ["Canada", "USA", "Brazil"]
332
+ end
333
+ end
334
+
335
+ context 'when result_set is empty' do
336
+ it 'returns an empty array' do
337
+ result = subject.where(continent: 'Americas').map(&:name)
338
+
339
+ expect(result).to eq []
340
+ end
341
+ end
342
+ end
343
+
344
+ describe '#pluck' do
345
+ subject { ActiveRepository::ResultSet.new(Country) }
346
+
347
+ context 'when result_set is not empty' do
348
+ before do
349
+ Country.delete_all
350
+ Country.create(name: 'Canada', continent: 'America')
351
+ Country.create(name: 'Russia', continent: 'Europe')
352
+ Country.create(name: 'USA', continent: 'America')
353
+ Country.create(name: 'Brazil', continent: 'America')
354
+ end
355
+
356
+ it 'returns an array with given attributes' do
357
+ result = subject.where(continent: 'America').pluck(:name)
358
+
359
+ expect(result).to eq ["Canada", "USA", "Brazil"]
360
+ end
361
+ end
362
+
363
+ context 'when result_set is empty' do
364
+ it 'returns an empty array' do
365
+ result = subject.where(continent: 'Americas').pluck(:name)
366
+
367
+ expect(result).to eq []
368
+ end
369
+ end
370
+ end
371
+
222
372
  describe '#first' do
373
+ before do
374
+ Country.delete_all
375
+ Country.create(name: 'Canada', continent: 'America')
376
+ Country.create(name: 'Russia', continent: 'Europe')
377
+ Country.create(name: 'USA', continent: 'America')
378
+ Country.create(name: 'Brazil', continent: 'America')
379
+ end
380
+
223
381
  subject { ActiveRepository::ResultSet.new(Country) }
224
382
 
225
383
  context 'when result_set is not empty' do
data/spec/spec_helper.rb CHANGED
@@ -1,12 +1,18 @@
1
- require 'coveralls'
2
- Coveralls.wear!
3
- # require 'simplecov'
4
- # SimpleCov.start do
5
- # add_filter "/spec/"
6
- # end
1
+ if ENV['TRAVIS']
2
+ require 'coveralls'
3
+ Coveralls.wear!
4
+ else
5
+ require 'simplecov'
6
+ SimpleCov.start do
7
+ add_filter "/spec/"
8
+ end
9
+ end
7
10
 
8
11
  require 'rspec'
9
12
  require 'rspec/autorun'
13
+ require 'i18n'
14
+
15
+ I18n.enforce_available_locales = false
10
16
 
11
17
  RSpec.configure do |c|
12
18
  c.treat_symbols_as_metadata_keys_with_true_values = true
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_repository
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.6
4
+ version: 0.3.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Caio Torres
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-17 00:00:00.000000000 Z
11
+ date: 2014-07-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_hash
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - '>='
32
32
  - !ruby/object:Gem::Version
33
- version: '3.2'
33
+ version: 4.1.4
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - '>='
39
39
  - !ruby/object:Gem::Version
40
- version: '3.2'
40
+ version: 4.1.4
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: sql_query_executor
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -72,42 +72,42 @@ dependencies:
72
72
  requirements:
73
73
  - - '>='
74
74
  - !ruby/object:Gem::Version
75
- version: 2.2.0
75
+ version: 2.14.1
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - '>='
81
81
  - !ruby/object:Gem::Version
82
- version: 2.2.0
82
+ version: 2.14.1
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: activerecord
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - '>='
88
88
  - !ruby/object:Gem::Version
89
- version: '3.2'
89
+ version: 4.1.4
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - '>='
95
95
  - !ruby/object:Gem::Version
96
- version: '3.2'
96
+ version: 4.1.4
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: mongoid
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
101
  - - '>='
102
102
  - !ruby/object:Gem::Version
103
- version: '3.1'
103
+ version: 4.0.0
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
108
  - - '>='
109
109
  - !ruby/object:Gem::Version
110
- version: '3.1'
110
+ version: 4.0.0
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: rake
113
113
  requirement: !ruby/object:Gem::Requirement
@@ -165,6 +165,8 @@ files:
165
165
  - lib/active_repository/adapters/persistence_adapter.rb
166
166
  - lib/active_repository/associations.rb
167
167
  - lib/active_repository/base.rb
168
+ - lib/active_repository/callback/base.rb
169
+ - lib/active_repository/callback/pool.rb
168
170
  - lib/active_repository/finders.rb
169
171
  - lib/active_repository/result_set.rb
170
172
  - lib/active_repository/uniqueness.rb