zyra 0.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,130 @@
1
+ # frozen_string_literal: true
2
+
3
+ shared_examples 'a method that returns or create a model' do
4
+ let(:key) { :user }
5
+ let(:email) { SecureRandom.hex(10) }
6
+ let(:model_class) { User }
7
+
8
+ let(:attributes) do
9
+ {
10
+ name: 'Some Name',
11
+ email: email,
12
+ password: 'SomePassword'
13
+ }
14
+ end
15
+
16
+ context 'when the model has not been registered' do
17
+ it do
18
+ expect { registry.find_or_create(key, attributes) }
19
+ .to raise_error(Zyra::Exceptions::NotRegistered)
20
+ end
21
+ end
22
+
23
+ context 'when the model has been registered' do
24
+ before do
25
+ registry.register(model_class, find_by: :email)
26
+ end
27
+
28
+ context 'when there is no entry in the database' do
29
+ it do
30
+ expect(registry.find_or_create(key, attributes))
31
+ .to be_a(model_class)
32
+ end
33
+
34
+ it do
35
+ expect { registry.find_or_create(key, attributes) }
36
+ .to change(model_class, :count)
37
+ end
38
+ end
39
+
40
+ context 'when the entry is there with the same attributes' do
41
+ let!(:user) { create(:user, **attributes) }
42
+
43
+ it 'returns the user' do
44
+ expect(registry.find_or_create(key, attributes)).to eq(user)
45
+ end
46
+ end
47
+
48
+ context 'when the entry is there with other attributes' do
49
+ let!(:user) { create(:user, email: email) }
50
+
51
+ it 'returns the user' do
52
+ expect(registry.find_or_create(key, attributes)).to eq(user)
53
+ end
54
+ end
55
+
56
+ context 'when there is another entry' do
57
+ before { create(:user) }
58
+
59
+ it 'returns a new model' do
60
+ expect(registry.find_or_create(key, attributes))
61
+ .to be_a(model_class)
62
+ end
63
+
64
+ it do
65
+ expect { registry.find_or_create(key, attributes) }
66
+ .to change(model_class, :count)
67
+ end
68
+ end
69
+
70
+ context 'when the keys is set as string' do
71
+ let(:keys) { 'email' }
72
+ let!(:user) { create(:user, **attributes) }
73
+
74
+ it 'finds the user the same way' do
75
+ expect(registry.find_or_create(key, attributes)).to eq(user)
76
+ end
77
+ end
78
+
79
+ context 'when the attributes have string keys' do
80
+ let(:attributes) { { 'email' => email } }
81
+ let!(:user) { create(:user, **attributes) }
82
+
83
+ it 'finds the user the same way' do
84
+ expect(registry.find_or_create(key, attributes)).to eq(user)
85
+ end
86
+ end
87
+
88
+ context 'when there is an event handler and it is triggered' do
89
+ let(:name) { 'new_name' }
90
+
91
+ let!(:user) { create(:user, **attributes) }
92
+
93
+ before do
94
+ new_name = name
95
+
96
+ registry.on(:user, :found) do |model|
97
+ model.update(name: new_name)
98
+ end
99
+ end
100
+
101
+ it 'runs the event after the model was found' do
102
+ expect { registry.find_or_create(key, attributes) }
103
+ .to change { user.reload.name }
104
+ .to(name)
105
+ end
106
+ end
107
+
108
+ context 'when there is an event handler and it is not triggered' do
109
+ let(:name) { 'new_name' }
110
+
111
+ before do
112
+ new_name = name
113
+
114
+ registry.on(:user, :found) do |model|
115
+ model.update(name: new_name)
116
+ end
117
+ end
118
+
119
+ it do
120
+ expect(registry.find_or_create(key, attributes))
121
+ .to be_a(model_class)
122
+ end
123
+
124
+ it do
125
+ expect { registry.find_or_create(key, attributes) }
126
+ .to change(model_class, :count)
127
+ end
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ shared_examples 'a method that registers an event handler' do
4
+ let(:model_class) { User }
5
+ let(:key) { :user }
6
+ let(:name) { SecureRandom.hex(10) }
7
+
8
+ let(:attributes) do
9
+ {
10
+ name: 'Some Name',
11
+ email: 'someemail@srv.com',
12
+ password: 'SomePassword'
13
+ }
14
+ end
15
+
16
+ context 'when a creator has not been registered' do
17
+ it do
18
+ expect { registry.on(key, :found) {} }
19
+ .to raise_error(Zyra::Exceptions::NotRegistered)
20
+ end
21
+ end
22
+
23
+ context 'when a creator has been registered' do
24
+ before do
25
+ create(:user, **attributes)
26
+ registry.register(model_class, find_by: :email)
27
+ end
28
+
29
+ it 'register a handler to be ran after an event' do
30
+ value = name
31
+
32
+ expect { registry.on(key, :found) { |m| m.name = value } }
33
+ .to change { registry.find_or_create(key, attributes).name }
34
+ .from('Some Name').to(name)
35
+ end
36
+
37
+ it do
38
+ expect(registry.on(key, :build) {})
39
+ .to be_a(Zyra::FinderCreator)
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ shared_examples 'a method that registers a finder creator' do
4
+ let(:recovery_key) { [key.to_s, key.to_sym].sample }
5
+ let(:attributes) { { email: 'email@srv.com' } }
6
+
7
+ context 'when providing symbol alias' do
8
+ let(:key) { :user_alias }
9
+
10
+ it 'creates a creator for the given class' do
11
+ expect(registry.register(User, key, find_by: :email))
12
+ .to eq(Zyra::FinderCreator.new(User, [:email]))
13
+ end
14
+
15
+ it 'register creator under the key' do
16
+ registry.register(User, key, find_by: :email)
17
+
18
+ expect(registry.find_or_create(recovery_key, attributes))
19
+ .to be_a(User)
20
+ end
21
+ end
22
+
23
+ context 'when providing string alias' do
24
+ let(:key) { 'user' }
25
+
26
+ it 'creates a creator for the given class' do
27
+ expect(registry.register(User, key, find_by: :email))
28
+ .to eq(Zyra::FinderCreator.new(User, [:email]))
29
+ end
30
+
31
+ it 'register creator under the key' do
32
+ registry.register(User, key, find_by: :email)
33
+
34
+ expect(registry.find_or_create(recovery_key, attributes))
35
+ .to be_a(User)
36
+ end
37
+ end
38
+
39
+ context 'when not providing an alias' do
40
+ let(:key) { :user }
41
+
42
+ it 'creates a creator for the given class' do
43
+ expect(registry.register(User, find_by: :email))
44
+ .to eq(Zyra::FinderCreator.new(User, [:email]))
45
+ end
46
+
47
+ it 'register creator under the correct key' do
48
+ registry.register(User, key, find_by: :email)
49
+
50
+ expect(registry.find_or_create(recovery_key, attributes))
51
+ .to be_a(User)
52
+ end
53
+ end
54
+ end
data/zyra.gemspec CHANGED
@@ -19,7 +19,13 @@ Gem::Specification.new do |gem|
19
19
  gem.test_files = gem.files.grep(%r{^(test|gem|features)/})
20
20
  gem.require_paths = ['lib']
21
21
 
22
+ gem.add_runtime_dependency 'activesupport', '~> 5.2.0'
23
+ gem.add_runtime_dependency 'jace', '>= 0.1.1'
24
+
25
+ gem.add_development_dependency 'activerecord', '5.2.8.1'
22
26
  gem.add_development_dependency 'bundler', '2.3.20'
27
+ gem.add_development_dependency 'database_cleaner', '1.8.3'
28
+ gem.add_development_dependency 'factory_bot', '6.2.1'
23
29
  gem.add_development_dependency 'pry', '0.14.1'
24
30
  gem.add_development_dependency 'pry-nav', '1.0.0'
25
31
  gem.add_development_dependency 'rake', '13.0.1'
@@ -33,6 +39,7 @@ Gem::Specification.new do |gem|
33
39
  gem.add_development_dependency 'rubocop-rspec', '1.38.1'
34
40
  gem.add_development_dependency 'rubycritic', '4.7.0'
35
41
  gem.add_development_dependency 'simplecov', '0.21.2'
42
+ gem.add_development_dependency 'sqlite3', '1.4.2'
36
43
  gem.add_development_dependency 'yard', '0.9.27'
37
44
  gem.add_development_dependency 'yardstick', '0.9.9'
38
45
  end
metadata CHANGED
@@ -1,15 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zyra
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - DarthJee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-08-31 00:00:00.000000000 Z
11
+ date: 2022-09-07 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: jace
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.1.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: activerecord
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 5.2.8.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 5.2.8.1
13
55
  - !ruby/object:Gem::Dependency
14
56
  name: bundler
15
57
  requirement: !ruby/object:Gem::Requirement
@@ -24,6 +66,34 @@ dependencies:
24
66
  - - '='
25
67
  - !ruby/object:Gem::Version
26
68
  version: 2.3.20
69
+ - !ruby/object:Gem::Dependency
70
+ name: database_cleaner
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '='
74
+ - !ruby/object:Gem::Version
75
+ version: 1.8.3
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '='
81
+ - !ruby/object:Gem::Version
82
+ version: 1.8.3
83
+ - !ruby/object:Gem::Dependency
84
+ name: factory_bot
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '='
88
+ - !ruby/object:Gem::Version
89
+ version: 6.2.1
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '='
95
+ - !ruby/object:Gem::Version
96
+ version: 6.2.1
27
97
  - !ruby/object:Gem::Dependency
28
98
  name: pry
29
99
  requirement: !ruby/object:Gem::Requirement
@@ -206,6 +276,20 @@ dependencies:
206
276
  - - '='
207
277
  - !ruby/object:Gem::Version
208
278
  version: 0.21.2
279
+ - !ruby/object:Gem::Dependency
280
+ name: sqlite3
281
+ requirement: !ruby/object:Gem::Requirement
282
+ requirements:
283
+ - - '='
284
+ - !ruby/object:Gem::Version
285
+ version: 1.4.2
286
+ type: :development
287
+ prerelease: false
288
+ version_requirements: !ruby/object:Gem::Requirement
289
+ requirements:
290
+ - - '='
291
+ - !ruby/object:Gem::Version
292
+ version: 1.4.2
209
293
  - !ruby/object:Gem::Dependency
210
294
  name: yard
211
295
  requirement: !ruby/object:Gem::Requirement
@@ -247,6 +331,7 @@ files:
247
331
  - ".rubocop.yml"
248
332
  - ".rubocop_todo.yml"
249
333
  - Dockerfile
334
+ - Dockerfile.circleci
250
335
  - Gemfile
251
336
  - LICENSE
252
337
  - README.md
@@ -257,13 +342,33 @@ files:
257
342
  - config/yardstick.yml
258
343
  - docker-compose.yml
259
344
  - lib/zyra.rb
345
+ - lib/zyra/creator.rb
346
+ - lib/zyra/exceptions.rb
347
+ - lib/zyra/finder.rb
348
+ - lib/zyra/finder_creator.rb
349
+ - lib/zyra/registry.rb
260
350
  - lib/zyra/version.rb
261
351
  - spec/integration/readme/.keep
352
+ - spec/integration/readme/zyra_spec.rb
262
353
  - spec/integration/yard/.keep
354
+ - spec/integration/yard/zyra/registry_spec.rb
355
+ - spec/integration/yard/zyra_spec.rb
356
+ - spec/lib/zyra/creator_spec.rb
357
+ - spec/lib/zyra/finder_creator_spec.rb
358
+ - spec/lib/zyra/finder_spec.rb
359
+ - spec/lib/zyra/registry_spec.rb
263
360
  - spec/lib/zyra_spec.rb
264
361
  - spec/spec_helper.rb
362
+ - spec/support/factories/user.rb
363
+ - spec/support/factory_bot.rb
265
364
  - spec/support/models/.keep
365
+ - spec/support/models/post.rb
366
+ - spec/support/models/user.rb
367
+ - spec/support/schema.rb
266
368
  - spec/support/shared_examples/.keep
369
+ - spec/support/shared_examples/registry/find_or_create.rb
370
+ - spec/support/shared_examples/registry/on.rb
371
+ - spec/support/shared_examples/registry/register.rb
267
372
  - zyra.gemspec
268
373
  - zyra.jpg
269
374
  homepage: https://github.com/darthjee/zyra