horza 1.0.0 → 1.0.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
  SHA1:
3
- metadata.gz: 85e3c731e1151f920c9079e120808d32ebc1a2d2
4
- data.tar.gz: 862ba540a13154564bbe845c89ee03d752db4ca6
3
+ metadata.gz: 8add62c849e87b400e7a027b56e3548531d2b19a
4
+ data.tar.gz: 3253f176d72fea1894b8fb828eb43875d50a76ca
5
5
  SHA512:
6
- metadata.gz: dac65236a361da1124d08ee528c49410f3434da8f197038827b8c374d226f269ca1190d8558905128744e64acaf4585fcd8888c6894a37cd101e914dc9595048
7
- data.tar.gz: cd2bd65854ef303f73743c6fd797d87b8acdfc2d10f44281efa8e4bab382083ac389bfbb78d52cdf4481f67d85f7ce311ca5f5a9108dbe6223c93f7d7baf013c
6
+ metadata.gz: f740207b61658443c12ea116e564d9df896d19a2513d089959c22b15b171f527311a66f4ca896cefcf3e46dbeb2e6c373f6debc1212ea8b44176f485b6c3a418
7
+ data.tar.gz: d65378df9ace26faa092db2ff73b9100a0cf7ae124816d7d359d16d7f5e8585ef19f2b88762b8a964002bfe07e4c899861b10a0a6ea9e6cc03d8fd1cbfa9c995
@@ -7,11 +7,8 @@ module Horza
7
7
 
8
8
  def resolve_dependency(entity_name)
9
9
  file_name = entity_name.to_s.underscore
10
- # Return already loaded constant from memory if possible,
11
- # otherwise search for a matching filename and try to load that.
12
- constant = get_loaded_constant(file_name)
13
- return constant if !constant.nil?
14
-
10
+ # Search for a matching filename in #constant_paths
11
+ # and try to load a constant that matches.
15
12
  resolve_from_file_paths(file_name)
16
13
  end
17
14
 
@@ -57,12 +54,5 @@ module Horza
57
54
  )
58
55
  end
59
56
 
60
- def get_loaded_constant(entity_name)
61
- constant_name = entity_name.camelize
62
- if ActiveSupport::Dependencies.qualified_const_defined?(constant_name)
63
- return ActiveSupport::Dependencies::Reference.get(constant_name)
64
- end
65
- end
66
-
67
57
  end
68
58
  end
@@ -1,3 +1,3 @@
1
1
  module Horza
2
- VERSION = '1.0.0'
2
+ VERSION = '1.0.1'
3
3
  end
@@ -16,30 +16,36 @@ else
16
16
  end
17
17
  end
18
18
 
19
- class Employer < ActiveRecord::Base
20
- has_many :users
21
- has_many :sports_cars
22
- end
23
-
24
- class User < ActiveRecord::Base
25
- belongs_to :employer
26
- end
27
-
28
- class Customer < ActiveRecord::Base
29
- validates :last_name, presence: true
30
- end
31
-
32
- class SportsCar < ActiveRecord::Base
33
- belongs_to :employer
34
- end
35
-
36
- class DummyModel < ActiveRecord::Base
37
- belongs_to :employer
38
- end
39
-
40
- class OtherDummyModel < ActiveRecord::Base
41
- belongs_to :employer
42
- end
19
+ Dir["spec/test_constants/*"].each { |f| load f }
20
+
21
+ # Following are defined for this spec.
22
+ #
23
+ # module TestConstants
24
+ # class Employer < ActiveRecord::Base
25
+ # has_many :users
26
+ # has_many :sports_cars
27
+ # end
28
+
29
+ # class User < ActiveRecord::Base
30
+ # belongs_to :employer
31
+ # end
32
+
33
+ # class Customer < ActiveRecord::Base
34
+ # validates :last_name, presence: true
35
+ # end
36
+
37
+ # class SportsCar < ActiveRecord::Base
38
+ # belongs_to :employer
39
+ # end
40
+
41
+ # class DummyModel < ActiveRecord::Base
42
+ # belongs_to :employer
43
+ # end
44
+
45
+ # class OtherDummyModel < ActiveRecord::Base
46
+ # belongs_to :employer
47
+ # end
48
+ # end
43
49
  end
44
50
 
45
51
  class SimpleRecord < ActiveRecord::Base
@@ -53,18 +59,26 @@ end
53
59
  describe Horza do
54
60
  let(:last_name) { 'Turner' }
55
61
  let(:adapter) { :active_record }
56
- let(:user_adapter) { Horza.adapt(User) }
62
+ let(:user_adapter) { Horza.adapt(TestConstants::User) }
57
63
  let(:customer_adapter) { Horza.adapt(Customer) }
58
- let(:employer_adapter) { Horza.adapt(Employer) }
59
- let(:sports_car_adapter) { Horza.adapt(SportsCar) }
64
+ let(:employer_adapter) { Horza.adapt(TestConstants::Employer) }
65
+ let(:sports_car_adapter) { Horza.adapt(TestConstants::SportsCar) }
60
66
 
61
67
  # Reset base config with each iteration
62
- before { Horza.configure { |config| config.adapter = adapter } }
68
+ before do
69
+ Horza.configure do |config|
70
+ config.adapter = adapter
71
+ config.constant_paths += ["spec"]
72
+ end
73
+
74
+ ActiveSupport::Dependencies.autoload_paths += ["spec"]
75
+ end
76
+
63
77
  after do
64
- User.delete_all
65
- Employer.delete_all
78
+ TestConstants::User.delete_all
79
+ TestConstants::Employer.delete_all
66
80
  Customer.delete_all
67
- SportsCar.delete_all
81
+ TestConstants::SportsCar.delete_all
68
82
  end
69
83
 
70
84
  context '#context_for_entity' do
@@ -77,17 +91,17 @@ describe Horza do
77
91
  end
78
92
 
79
93
  describe '#adapt' do
80
- subject { Horza.adapt(User) }
94
+ subject { Horza.adapt(TestConstants::User) }
81
95
  it 'returns the adaptor class' do
82
96
  expect(subject.is_a? Horza::Adapters::ActiveRecord).to be true
83
97
  end
84
98
  it 'sets the model as context' do
85
- expect(subject.context).to eq User
99
+ expect(subject.context).to eq TestConstants::User
86
100
  end
87
101
  end
88
102
 
89
103
  describe 'Queries' do
90
- let(:user) { User.create }
104
+ let(:user) { TestConstants::User.create }
91
105
  let(:single_entity_klass) { Horza::Entities::Single }
92
106
 
93
107
  describe '#get!' do
@@ -121,15 +135,15 @@ describe Horza do
121
135
  describe '#find_first!' do
122
136
  context 'when users exist' do
123
137
  before do
124
- 3.times { User.create(last_name: last_name) }
125
- 2.times { User.create(last_name: 'OTHER') }
138
+ 3.times { TestConstants::User.create(last_name: last_name) }
139
+ 2.times { TestConstants::User.create(last_name: 'OTHER') }
126
140
  end
127
141
  it 'returns single Entity' do
128
142
  expect(user_adapter.find_first(conditions: { last_name: last_name }).is_a? Horza::Entities::Single).to be true
129
143
  end
130
144
 
131
145
  it 'returns user' do
132
- expect(user_adapter.find_first!(conditions: { last_name: last_name }).to_h).to eq User.where(last_name: last_name).order('id DESC').first.attributes
146
+ expect(user_adapter.find_first!(conditions: { last_name: last_name }).to_h).to eq TestConstants::User.where(last_name: last_name).order('id DESC').first.attributes
133
147
  end
134
148
  end
135
149
 
@@ -154,8 +168,8 @@ describe Horza do
154
168
 
155
169
  context 'when users exist' do
156
170
  before do
157
- 3.times { User.create(conditions) }
158
- 2.times { User.create(last_name: 'OTHER') }
171
+ 3.times { TestConstants::User.create(conditions) }
172
+ 2.times { TestConstants::User.create(last_name: 'OTHER') }
159
173
  end
160
174
  it 'returns user' do
161
175
  expect(user_adapter.find_all(options).length).to eq 3
@@ -171,7 +185,7 @@ describe Horza do
171
185
  let(:total) { 20 }
172
186
  let(:offset) { 7 }
173
187
  before do
174
- total.times { User.create(last_name: 'Smith') }
188
+ total.times { TestConstants::User.create(last_name: 'Smith') }
175
189
  end
176
190
  it 'offsets response' do
177
191
  expect(user_adapter.find_all(conditions: { last_name: 'Smith' }, offset: offset).length).to eq total - offset
@@ -217,7 +231,7 @@ describe Horza do
217
231
  end
218
232
 
219
233
  describe '#create_as_child' do
220
- let(:employer) { Employer.create }
234
+ let(:employer) { TestConstants::Employer.create }
221
235
  let(:parent) do
222
236
  {
223
237
  id: employer.id,
@@ -228,7 +242,7 @@ describe Horza do
228
242
 
229
243
  context 'when parameters are valid' do
230
244
  it 'creates the record' do
231
- expect { action }.to change(User, :count).by(1)
245
+ expect { action }.to change(TestConstants::User, :count).by(1)
232
246
  end
233
247
 
234
248
  it 'returns the entity' do
@@ -246,7 +260,7 @@ describe Horza do
246
260
  end
247
261
 
248
262
  it 'does not create the record' do
249
- expect { action }.to change(User, :count).by(0)
263
+ expect { action }.to change(TestConstants::User, :count).by(0)
250
264
  end
251
265
 
252
266
  it 'returns nil' do
@@ -256,7 +270,7 @@ describe Horza do
256
270
  end
257
271
 
258
272
  describe '#create_as_child!' do
259
- let(:employer) { Employer.create }
273
+ let(:employer) { TestConstants::Employer.create }
260
274
  let(:action) { user_adapter.create_as_child!(parent, last_name: last_name) }
261
275
 
262
276
  context 'when parameters are invalid' do
@@ -347,9 +361,9 @@ describe Horza do
347
361
 
348
362
  context '#association' do
349
363
  context 'direct relation' do
350
- let(:employer) { Employer.create }
351
- let!(:user1) { User.create(employer: employer) }
352
- let!(:user2) { User.create(employer: employer) }
364
+ let(:employer) { TestConstants::Employer.create }
365
+ let!(:user1) { TestConstants::User.create(employer: employer) }
366
+ let!(:user2) { TestConstants::User.create(employer: employer) }
353
367
 
354
368
  context 'parent' do
355
369
  it 'returns parent' do
@@ -362,7 +376,7 @@ describe Horza do
362
376
  result = employer_adapter.association(id: employer.id, target: :users)
363
377
  expect(result.length).to eq 2
364
378
  expect(result.first.is_a? Horza::Entities::Single).to be true
365
- expect(result.first.to_hash).to eq User.order('id DESC').first.attributes
379
+ expect(result.first.to_hash).to eq TestConstants::User.order('id DESC').first.attributes
366
380
  end
367
381
  end
368
382
 
@@ -373,14 +387,14 @@ describe Horza do
373
387
  end
374
388
 
375
389
  context 'valid ancestry with no saved childred' do
376
- let(:employer2) { Employer.create }
390
+ let(:employer2) { TestConstants::Employer.create }
377
391
  it 'returns empty collection' do
378
392
  expect(employer_adapter.association(id: employer2.id, target: :users).empty?).to be true
379
393
  end
380
394
  end
381
395
 
382
396
  context 'valid ancestry with no saved parent' do
383
- let(:user2) { User.create }
397
+ let(:user2) { TestConstants::User.create }
384
398
  it 'returns nil' do
385
399
  expect(user_adapter.association(id: user2.id, target: :employer)).to be nil
386
400
  end
@@ -392,8 +406,8 @@ describe Horza do
392
406
  let(:conditions) { { last_name: 'Turner' } }
393
407
 
394
408
  before do
395
- turner_total.times { employer.users << User.create(conditions.merge(employer: employer)) }
396
- other_total.times { employer.users << User.create(employer: employer) }
409
+ turner_total.times { employer.users << TestConstants::User.create(conditions.merge(employer: employer)) }
410
+ other_total.times { employer.users << TestConstants::User.create(employer: employer) }
397
411
  end
398
412
 
399
413
  context 'limit' do
@@ -417,7 +431,7 @@ describe Horza do
417
431
  context 'order' do
418
432
  it 'orders response' do
419
433
  horza_response = employer_adapter.association(id: employer.id, target: :users, conditions: conditions, order: { id: :asc })
420
- ar_response = User.where(conditions).order('id asc')
434
+ ar_response = TestConstants::User.where(conditions).order('id asc')
421
435
  expect(horza_response.first.id).to eq ar_response.first.id
422
436
  expect(horza_response.last.id).to eq ar_response.last.id
423
437
  end
@@ -427,7 +441,7 @@ describe Horza do
427
441
  context 'simple' do
428
442
  it 'works as expected' do
429
443
  horza_response = employer_adapter.association(id: employer.id, target: :users, conditions: conditions, order: { id: :asc }, eager_load: true)
430
- ar_response = User.where(conditions).order('id asc')
444
+ ar_response = TestConstants::User.where(conditions).order('id asc')
431
445
  expect(horza_response.first.id).to eq ar_response.first.id
432
446
  expect(horza_response.last.id).to eq ar_response.last.id
433
447
  end
@@ -437,9 +451,9 @@ describe Horza do
437
451
  end
438
452
 
439
453
  context 'using via' do
440
- let(:employer) { Employer.create }
441
- let(:user) { User.create(employer: employer) }
442
- let(:sportscar) { SportsCar.create(employer: employer) }
454
+ let(:employer) { TestConstants::Employer.create }
455
+ let(:user) { TestConstants::User.create(employer: employer) }
456
+ let(:sportscar) { TestConstants::SportsCar.create(employer: employer) }
443
457
 
444
458
  before do
445
459
  employer.sports_cars << sportscar
@@ -507,8 +521,8 @@ describe Horza do
507
521
 
508
522
  context 'without conditions' do
509
523
  context 'when one join record exists' do
510
- let!(:employer) { Employer.create(name: 'Corporation ltd.', boss_email: 'boss@boss.com') }
511
- let!(:user) { User.create(employer: employer, first_name: 'John', last_name: 'Turner', email: 'email@email.com') }
524
+ let!(:employer) { TestConstants::Employer.create(name: 'Corporation ltd.', boss_email: 'boss@boss.com') }
525
+ let!(:user) { TestConstants::User.create(employer: employer, first_name: 'John', last_name: 'Turner', email: 'email@email.com') }
512
526
 
513
527
  context 'without fields' do
514
528
  it 'returns joined record' do
@@ -541,8 +555,8 @@ describe Horza do
541
555
  end
542
556
 
543
557
  context 'complex predicate' do
544
- let!(:match_user) { User.create(employer: employer, first_name: 'John', last_name: 'Turner', email: 'boss@boss.com') }
545
- let!(:match_user2) { User.create(employer: employer, first_name: 'Helen', last_name: 'Jones', email: 'boss@boss.com') }
558
+ let!(:match_user) { TestConstants::User.create(employer: employer, first_name: 'John', last_name: 'Turner', email: 'boss@boss.com') }
559
+ let!(:match_user2) { TestConstants::User.create(employer: employer, first_name: 'Helen', last_name: 'Jones', email: 'boss@boss.com') }
546
560
  it 'returns joined records and the specified fields' do
547
561
  result = user_adapter.join(complex_predicate)
548
562
  expect(result.length).to eq 2
@@ -556,8 +570,8 @@ describe Horza do
556
570
  end
557
571
 
558
572
  context 'when no join record exists' do
559
- let!(:employer) { Employer.create(name: 'Corporation ltd.') }
560
- let!(:user) { User.create(employer_id: 9999, first_name: 'John', last_name: 'Turner', email: 'email@email.com') }
573
+ let!(:employer) { TestConstants::Employer.create(name: 'Corporation ltd.') }
574
+ let!(:user) { TestConstants::User.create(employer_id: 9999, first_name: 'John', last_name: 'Turner', email: 'email@email.com') }
561
575
 
562
576
  it 'returns an empty collection' do
563
577
  result = user_adapter.join(simple)
@@ -566,10 +580,10 @@ describe Horza do
566
580
  end
567
581
 
568
582
  context 'when multiple join records exists' do
569
- let!(:employer) { Employer.create(name: 'Corporation ltd.') }
570
- let!(:user) { User.create(employer: employer, first_name: 'John', last_name: 'Turner', email: 'email@turner.com') }
571
- let!(:user2) { User.create(employer: employer, first_name: 'Adam', last_name: 'Boots', email: 'email@boots.com') }
572
- let!(:user3) { User.create(employer: employer, first_name: 'Tim', last_name: 'Socks', email: 'email@socks.com') }
583
+ let!(:employer) { TestConstants::Employer.create(name: 'Corporation ltd.') }
584
+ let!(:user) { TestConstants::User.create(employer: employer, first_name: 'John', last_name: 'Turner', email: 'email@turner.com') }
585
+ let!(:user2) { TestConstants::User.create(employer: employer, first_name: 'Adam', last_name: 'Boots', email: 'email@boots.com') }
586
+ let!(:user3) { TestConstants::User.create(employer: employer, first_name: 'Tim', last_name: 'Socks', email: 'email@socks.com') }
573
587
 
574
588
  it 'returns an empty collection' do
575
589
  result = user_adapter.join(simple)
@@ -579,11 +593,11 @@ describe Horza do
579
593
  end
580
594
 
581
595
  context 'with conditions' do
582
- let!(:employer) { Employer.create(name: 'Corporation ltd.') }
583
- let!(:employer2) { Employer.create(name: 'BigBucks ltd.') }
584
- let!(:user) { User.create(employer: employer, first_name: 'John', last_name: 'Turner', email: 'email@turner.com') }
585
- let!(:user2) { User.create(employer: employer, first_name: 'Adam', last_name: 'Boots', email: 'email@boots.com') }
586
- let!(:user3) { User.create(employer: employer2, first_name: 'Tim', last_name: 'Socks', email: 'email@socks.com') }
596
+ let!(:employer) { TestConstants::Employer.create(name: 'Corporation ltd.') }
597
+ let!(:employer2) { TestConstants::Employer.create(name: 'BigBucks ltd.') }
598
+ let!(:user) { TestConstants::User.create(employer: employer, first_name: 'John', last_name: 'Turner', email: 'email@turner.com') }
599
+ let!(:user2) { TestConstants::User.create(employer: employer, first_name: 'Adam', last_name: 'Boots', email: 'email@boots.com') }
600
+ let!(:user3) { TestConstants::User.create(employer: employer2, first_name: 'Tim', last_name: 'Socks', email: 'email@socks.com') }
587
601
 
588
602
  it 'returns only the joins that match the conditions' do
589
603
  result = user_adapter.join(conditions)
@@ -593,11 +607,11 @@ describe Horza do
593
607
  end
594
608
 
595
609
  context 'limits/offset' do
596
- let!(:employer) { Employer.create(name: 'Corporation ltd.') }
597
- let!(:employer2) { Employer.create(name: 'BigBucks ltd.') }
598
- let!(:user) { User.create(employer: employer, first_name: 'John', last_name: 'Turner', email: 'email@turner.com') }
599
- let!(:user2) { User.create(employer: employer, first_name: 'Adam', last_name: 'Turner', email: 'email@boots.com') }
600
- let!(:user3) { User.create(employer: employer2, first_name: 'Tim', last_name: 'Socks', email: 'email@socks.com') }
610
+ let!(:employer) { TestConstants::Employer.create(name: 'Corporation ltd.') }
611
+ let!(:employer2) { TestConstants::Employer.create(name: 'BigBucks ltd.') }
612
+ let!(:user) { TestConstants::User.create(employer: employer, first_name: 'John', last_name: 'Turner', email: 'email@turner.com') }
613
+ let!(:user2) { TestConstants::User.create(employer: employer, first_name: 'Adam', last_name: 'Turner', email: 'email@boots.com') }
614
+ let!(:user3) { TestConstants::User.create(employer: employer2, first_name: 'Tim', last_name: 'Socks', email: 'email@socks.com') }
601
615
 
602
616
  it 'limits the joins that match the conditions' do
603
617
  params = conditions.merge(limit: 1)
@@ -619,7 +633,7 @@ describe Horza do
619
633
  describe 'Entities' do
620
634
  describe 'Collection' do
621
635
  let(:members) do
622
- 3.times.map { User.create }
636
+ 3.times.map { TestConstants::User.create }
623
637
  end
624
638
 
625
639
  let(:single_entity_klass) { Horza::Entities::SingleWithActiveModel }
@@ -36,15 +36,6 @@ describe Horza::DependencyLoading do
36
36
  expect { Horza::DependencyLoading.resolve_dependency("test_employer") }.to raise_error
37
37
  end
38
38
 
39
- it "returns constant if already loaded" do
40
- Object.const_set(:A, Class.new)
41
-
42
- constant = Horza::DependencyLoading.resolve_dependency("a")
43
- expect(constant).to eq A
44
-
45
- Object.send(:remove_const, :A)
46
- end
47
-
48
39
  context "finds multiple matched constant_paths" do
49
40
 
50
41
  it "resolves constant for first matched file_path" do
@@ -66,7 +57,7 @@ describe Horza::DependencyLoading do
66
57
  end
67
58
 
68
59
  it "raises if file path is not matched" do
69
- expect { Horza::DependencyLoading.search_for_file("employer") }.to raise_error(Horza::DependencyLoading::MissingFile)
60
+ expect { Horza::DependencyLoading.search_for_file("missing_file") }.to raise_error(Horza::DependencyLoading::MissingFile)
70
61
  end
71
62
  end
72
63
  end
@@ -0,0 +1,3 @@
1
+ class Customer < ActiveRecord::Base
2
+ validates :last_name, presence: true
3
+ end
@@ -0,0 +1,3 @@
1
+ class DummyModel < ActiveRecord::Base
2
+ belongs_to :employer
3
+ end
@@ -0,0 +1,6 @@
1
+ module TestConstants
2
+ class Employer < ActiveRecord::Base
3
+ has_many :users
4
+ has_many :sports_cars
5
+ end
6
+ end
@@ -0,0 +1,3 @@
1
+ class OtherDummyModel < ActiveRecord::Base
2
+ belongs_to :employer
3
+ end
@@ -0,0 +1,5 @@
1
+ module TestConstants
2
+ class SportsCar < ActiveRecord::Base
3
+ belongs_to :employer
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module TestConstants
2
+ class User < ActiveRecord::Base
3
+ belongs_to :employer
4
+ end
5
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: horza
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Blake Turner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-30 00:00:00.000000000 Z
11
+ date: 2015-12-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hashie
@@ -126,7 +126,13 @@ files:
126
126
  - spec/horza_spec.rb
127
127
  - spec/shared_config_spec.rb
128
128
  - spec/spec_helper.rb
129
+ - spec/test_constants/customer.rb
130
+ - spec/test_constants/dummy_model.rb
131
+ - spec/test_constants/employer.rb
132
+ - spec/test_constants/other_dummy_model.rb
133
+ - spec/test_constants/sports_car.rb
129
134
  - spec/test_constants/test_employer.rb
135
+ - spec/test_constants/user.rb
130
136
  homepage: https://github.com/onfido/horza
131
137
  licenses:
132
138
  - MIT
@@ -158,4 +164,10 @@ test_files:
158
164
  - spec/horza_spec.rb
159
165
  - spec/shared_config_spec.rb
160
166
  - spec/spec_helper.rb
167
+ - spec/test_constants/customer.rb
168
+ - spec/test_constants/dummy_model.rb
169
+ - spec/test_constants/employer.rb
170
+ - spec/test_constants/other_dummy_model.rb
171
+ - spec/test_constants/sports_car.rb
161
172
  - spec/test_constants/test_employer.rb
173
+ - spec/test_constants/user.rb