zyra 0.0.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,155 +3,19 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  describe Zyra do
6
- before { described_class.reset }
7
-
8
- describe '.build' do
9
- let(:model_class) { User }
10
- let(:key) { :user }
11
-
12
- context 'when a builder has not been registered' do
13
- it do
14
- expect { described_class.build(key) }
15
- .to raise_error(Zyra::Exceptions::BuilderNotRegistered)
16
- end
17
- end
18
-
19
- context 'when a builder has been registered' do
20
- before do
21
- described_class.register(model_class)
22
- end
23
-
24
- it do
25
- expect(described_class.build(key)).to be_a(model_class)
26
- end
27
-
28
- it do
29
- expect(described_class.build(key))
30
- .not_to be_persisted
31
- end
32
-
33
- context 'when attributes are given' do
34
- let(:name) { SecureRandom.hex(10) }
6
+ let(:registry) { described_class }
35
7
 
36
- it 'initializes the model with the given attribute' do
37
- expect(described_class.build(key, name: name).name)
38
- .to eq(name)
39
- end
40
- end
41
-
42
- context 'when a block is given' do
43
- let(:name) { SecureRandom.hex(10) }
44
-
45
- it 'initializes the model with the given attribute' do
46
- value = name
47
- expect(described_class.build(key) { |model| model.name = value }.name)
48
- .to eq(name)
49
- end
50
- end
51
-
52
- context 'when building has an after build handler' do
53
- let(:name) { SecureRandom.hex(10) }
54
-
55
- before do
56
- value = name
57
- described_class.after(:user, :build) do |model|
58
- model.name = value
59
- end
60
- end
8
+ before { described_class.reset }
61
9
 
62
- it 'runs the event handler' do
63
- expect(described_class.build(key) .name)
64
- .to eq(name)
65
- end
66
- end
67
- end
10
+ describe '.register' do
11
+ it_behaves_like 'a method that registers a finder creator'
68
12
  end
69
13
 
70
- describe '.create' do
71
- let(:model_class) { User }
72
- let(:key) { :user }
73
-
74
- context 'when a builder has not been registered' do
75
- it do
76
- expect { described_class.create(key) }
77
- .to raise_error(Zyra::Exceptions::BuilderNotRegistered)
78
- end
79
- end
80
-
81
- context 'when a builder has been registered' do
82
- before do
83
- described_class.register(model_class)
84
- end
85
-
86
- it do
87
- expect(described_class.create(key)).to be_a(model_class)
88
- end
89
-
90
- it do
91
- expect(described_class.create(key))
92
- .to be_persisted
93
- end
94
-
95
- context 'when attributes are given' do
96
- let(:name) { SecureRandom.hex(10) }
97
-
98
- it 'initializes the model with the given attribute' do
99
- expect(described_class.create(key, name: name).name)
100
- .to eq(name)
101
- end
102
- end
103
-
104
- context 'when a block is given' do
105
- let(:name) { SecureRandom.hex(10) }
106
-
107
- it 'initializes the model with the given attribute' do
108
- value = name
109
- model = described_class.create(key) { |mdl| mdl.name = value }
110
- expect(model.name)
111
- .to eq(name)
112
- end
113
- end
114
-
115
- context 'when building has an after build handler' do
116
- let(:name) { SecureRandom.hex(10) }
117
-
118
- before do
119
- value = name
120
- described_class.after(:user, :build) do |model|
121
- model.name = value
122
- end
123
- end
124
-
125
- it 'runs the event handler' do
126
- expect(described_class.create(key) .name)
127
- .to eq(name)
128
- end
129
- end
130
- end
14
+ describe '.on' do
15
+ it_behaves_like 'a method that registers an event handler'
131
16
  end
132
17
 
133
- describe '.after' do
134
- let(:model_class) { User }
135
- let(:key) { :user }
136
- let(:name) { SecureRandom.hex(10) }
137
-
138
- context 'when a builder has been registered' do
139
- before do
140
- described_class.register(model_class)
141
- end
142
-
143
- it 'register a handler to be ran after an event' do
144
- value = name
145
-
146
- expect { described_class.after(key, :build) { |m| m.name = value } }
147
- .to change { described_class.build(key).name }
148
- .from(nil).to(name)
149
- end
150
-
151
- it do
152
- expect(described_class.after(key, :build) {})
153
- .to be_a(Zyra::Builder)
154
- end
155
- end
18
+ describe '.find_or_create' do
19
+ it_behaves_like 'a method that returns or create a model'
156
20
  end
157
21
  end
data/spec/spec_helper.rb CHANGED
@@ -10,6 +10,8 @@ SimpleCov.start 'gem'
10
10
 
11
11
  require 'zyra'
12
12
  require 'pry-nav'
13
+ require 'factory_bot'
14
+ require 'database_cleaner'
13
15
 
14
16
  require 'active_record'
15
17
  ActiveRecord::Base.establish_connection(
@@ -26,6 +28,17 @@ RSpec.configure do |config|
26
28
  config.filter_run_excluding :integration unless ENV['ALL']
27
29
 
28
30
  config.order = 'random'
31
+
32
+ config.before(:suite) do
33
+ DatabaseCleaner.strategy = :transaction
34
+ DatabaseCleaner.clean_with(:truncation)
35
+ end
36
+
37
+ config.around do |example|
38
+ DatabaseCleaner.cleaning do
39
+ example.run
40
+ end
41
+ end
29
42
  end
30
43
 
31
44
  RSpec::Matchers.define_negated_matcher :not_change, :change
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ FactoryBot.define do
4
+ factory :user, class: '::User' do
5
+ sequence(:name) { |n| "Name-#{n}" }
6
+ sequence(:email) { |n| "user-#{n}@email.com" }
7
+ password { SecureRandom.hex(10) }
8
+ end
9
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.configure do |config|
4
+ config.include FactoryBot::Syntax::Methods
5
+ end
6
+
7
+ FactoryBot.find_definitions
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Post < ActiveRecord::Base
4
+ belongs_to :user
5
+ end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class User < ActiveRecord::Base
4
+ has_many :posts
4
5
  end
@@ -6,5 +6,13 @@ ActiveRecord::Schema.define do
6
6
  create_table :users, force: true do |t|
7
7
  t.string :name
8
8
  t.string :email
9
+ t.string :password
10
+ t.string :reference
11
+ end
12
+
13
+ create_table :posts, force: true do |t|
14
+ t.integer :user_id
15
+ t.string :name
16
+ t.text :content
9
17
  end
10
18
  end
@@ -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,11 +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'
22
23
  gem.add_runtime_dependency 'jace', '>= 0.1.1'
23
- gem.add_runtime_dependency 'sinclair', '>= 1.8.0'
24
24
 
25
25
  gem.add_development_dependency 'activerecord', '5.2.8.1'
26
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'
27
29
  gem.add_development_dependency 'pry', '0.14.1'
28
30
  gem.add_development_dependency 'pry-nav', '1.0.0'
29
31
  gem.add_development_dependency 'rake', '13.0.1'
metadata CHANGED
@@ -1,43 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zyra
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 1.0.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-09-01 00:00:00.000000000 Z
11
+ date: 2022-09-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: jace
14
+ name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.1.1
19
+ version: 5.2.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.1.1
26
+ version: 5.2.0
27
27
  - !ruby/object:Gem::Dependency
28
- name: sinclair
28
+ name: jace
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 1.8.0
33
+ version: 0.1.1
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: 1.8.0
40
+ version: 0.1.1
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: activerecord
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -66,6 +66,34 @@ dependencies:
66
66
  - - '='
67
67
  - !ruby/object:Gem::Version
68
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
69
97
  - !ruby/object:Gem::Dependency
70
98
  name: pry
71
99
  requirement: !ruby/object:Gem::Requirement
@@ -314,20 +342,33 @@ files:
314
342
  - config/yardstick.yml
315
343
  - docker-compose.yml
316
344
  - lib/zyra.rb
317
- - lib/zyra/builder.rb
345
+ - lib/zyra/creator.rb
318
346
  - lib/zyra/exceptions.rb
347
+ - lib/zyra/finder.rb
348
+ - lib/zyra/finder_creator.rb
319
349
  - lib/zyra/registry.rb
320
350
  - lib/zyra/version.rb
321
351
  - spec/integration/readme/.keep
352
+ - spec/integration/readme/zyra_spec.rb
322
353
  - spec/integration/yard/.keep
323
- - spec/lib/zyra/builder_spec.rb
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
324
359
  - spec/lib/zyra/registry_spec.rb
325
360
  - spec/lib/zyra_spec.rb
326
361
  - spec/spec_helper.rb
362
+ - spec/support/factories/user.rb
363
+ - spec/support/factory_bot.rb
327
364
  - spec/support/models/.keep
365
+ - spec/support/models/post.rb
328
366
  - spec/support/models/user.rb
329
367
  - spec/support/schema.rb
330
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
331
372
  - zyra.gemspec
332
373
  - zyra.jpg
333
374
  homepage: https://github.com/darthjee/zyra