zyra 0.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +20 -6
- data/.rubocop.yml +1 -0
- data/Dockerfile +1 -1
- data/Dockerfile.circleci +7 -0
- data/README.md +68 -2
- data/config/check_specs.yml +1 -0
- data/config/yardstick.yml +6 -2
- data/docker-compose.yml +11 -0
- data/lib/zyra/creator.rb +68 -0
- data/lib/zyra/exceptions.rb +11 -0
- data/lib/zyra/finder.rb +103 -0
- data/lib/zyra/finder_creator.rb +162 -0
- data/lib/zyra/registry.rb +201 -0
- data/lib/zyra/version.rb +2 -2
- data/lib/zyra.rb +187 -1
- data/spec/integration/readme/zyra_spec.rb +54 -0
- data/spec/integration/yard/zyra/registry_spec.rb +135 -0
- data/spec/integration/yard/zyra_spec.rb +128 -0
- data/spec/lib/zyra/creator_spec.rb +75 -0
- data/spec/lib/zyra/finder_creator_spec.rb +263 -0
- data/spec/lib/zyra/finder_spec.rb +117 -0
- data/spec/lib/zyra/registry_spec.rb +19 -0
- data/spec/lib/zyra_spec.rb +14 -1
- data/spec/spec_helper.rb +18 -0
- data/spec/support/factories/user.rb +9 -0
- data/spec/support/factory_bot.rb +7 -0
- data/spec/support/models/post.rb +5 -0
- data/spec/support/models/user.rb +5 -0
- data/spec/support/schema.rb +18 -0
- data/spec/support/shared_examples/registry/find_or_create.rb +130 -0
- data/spec/support/shared_examples/registry/on.rb +42 -0
- data/spec/support/shared_examples/registry/register.rb +54 -0
- data/zyra.gemspec +7 -0
- metadata +107 -2
@@ -0,0 +1,201 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Zyra
|
4
|
+
# @api public
|
5
|
+
#
|
6
|
+
# Registry of all registered creators
|
7
|
+
class Registry
|
8
|
+
# Register a new creator
|
9
|
+
#
|
10
|
+
# The creator will focus on one class and be registered under a
|
11
|
+
# symbol key
|
12
|
+
#
|
13
|
+
# @param klass [Class] Model class to be used by the creator
|
14
|
+
#
|
15
|
+
# @overload register(klass)
|
16
|
+
# When the key is not provided, it is infered from the class name
|
17
|
+
#
|
18
|
+
# @overload register(klass, key)
|
19
|
+
# @param key [String,Symbol] key to be used when storyin the creator
|
20
|
+
#
|
21
|
+
# @return [Zyra::FinderCreator] registered finder_creator
|
22
|
+
#
|
23
|
+
# @example Register models searching
|
24
|
+
# registry = Zyra::Registry.new
|
25
|
+
#
|
26
|
+
# registry.register(User, find_by: :email)
|
27
|
+
# registry
|
28
|
+
# .register(User, :user_by_name, find_by: :name)
|
29
|
+
# .on(:return) do |user|
|
30
|
+
# user.update(email: "#{user.name.gsub(/ /, '_')}@srv.com")
|
31
|
+
# end
|
32
|
+
#
|
33
|
+
# attributes = {
|
34
|
+
# name: 'my name',
|
35
|
+
# email: 'my_email@srv.com'
|
36
|
+
# }
|
37
|
+
#
|
38
|
+
# user = registry.find_or_create(:user, attributes)
|
39
|
+
# # returns a User with name 'my_email@srv.com'
|
40
|
+
#
|
41
|
+
# user = registry.find_or_create(:user_by_name, attributes)
|
42
|
+
# # returns a User with name 'my_name@srv.com'
|
43
|
+
def register(klass, key = nil, find_by:)
|
44
|
+
key ||= klass.name.gsub(/::([A-Z])/, '_\1').downcase
|
45
|
+
|
46
|
+
registry[key.to_sym] = FinderCreator.new(klass, find_by)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Register a handler on a certain event
|
50
|
+
#
|
51
|
+
# Possible events are +found+, +build+, +create+
|
52
|
+
# and +return+
|
53
|
+
#
|
54
|
+
# @param key [String,Symbol] key under which the
|
55
|
+
# {FinderCreator finder_creator}
|
56
|
+
# is {Zyra::Registry#register registered}
|
57
|
+
#
|
58
|
+
# @param (see FinderCreator#after)
|
59
|
+
# @yield (see FinderCreator#after)
|
60
|
+
# @return [Finder] The finder registered under that key
|
61
|
+
#
|
62
|
+
# @see Zyra::Finder#find
|
63
|
+
# @see Zyra::Creator#create
|
64
|
+
#
|
65
|
+
# @example Adding a hook on return
|
66
|
+
# registry = Zyra::Registry.new
|
67
|
+
# registry.register(User, find_by: :email)
|
68
|
+
# registry.on(:user, :return) do |user|
|
69
|
+
# user.update(name: 'initial name')
|
70
|
+
# end
|
71
|
+
#
|
72
|
+
# email = 'email@srv.com'
|
73
|
+
#
|
74
|
+
# user = registry.find_or_create(
|
75
|
+
# :user,
|
76
|
+
# email: email
|
77
|
+
# )
|
78
|
+
# # returns a User with name 'initial name'
|
79
|
+
#
|
80
|
+
# user.update(name: 'some other name')
|
81
|
+
#
|
82
|
+
# user = registry.find_or_create(:user, email: email)
|
83
|
+
# # returns a User with name 'initial name'
|
84
|
+
#
|
85
|
+
# @example Adding a hook on found
|
86
|
+
# registry = Zyra::Registry.new
|
87
|
+
# registry.register(User, find_by: :email)
|
88
|
+
# registry.on(:user, :found) do |user|
|
89
|
+
# user.update(name: 'final name')
|
90
|
+
# end
|
91
|
+
#
|
92
|
+
# email = 'email@srv.com'
|
93
|
+
# attributes = { email: email, name: 'initial name' }
|
94
|
+
#
|
95
|
+
# user = registry.find_or_create(:user, attributes)
|
96
|
+
# # returns a User with name 'initial name'
|
97
|
+
#
|
98
|
+
# user = registry.find_or_create(:user, attributes)
|
99
|
+
# # returns a User with name 'final name'
|
100
|
+
#
|
101
|
+
# @example Adding a hook on build
|
102
|
+
# registry = Zyra::Registry.new
|
103
|
+
# registry.register(User, find_by: :email)
|
104
|
+
# registry.on(:user, :build) do |user|
|
105
|
+
# user.name = 'initial name'
|
106
|
+
# end
|
107
|
+
#
|
108
|
+
# email = 'email@srv.com'
|
109
|
+
#
|
110
|
+
# user = registry.find_or_create(:user, email: email)
|
111
|
+
# # returns a User with name 'initial name'
|
112
|
+
#
|
113
|
+
# user.update(name: 'some other name')
|
114
|
+
#
|
115
|
+
# user = registry.find_or_create(:user, email: email)
|
116
|
+
# # returns a User with name 'some other name'
|
117
|
+
#
|
118
|
+
# @example Adding a hook on create
|
119
|
+
# registry = Zyra::Registry.new
|
120
|
+
# registry.register(User, find_by: :email)
|
121
|
+
# registry.on(:user, :create) do |user|
|
122
|
+
# user.update(name: 'initial name')
|
123
|
+
# end
|
124
|
+
#
|
125
|
+
# email = 'email@srv.com'
|
126
|
+
#
|
127
|
+
# user = registry.find_or_create(:user, email: email)
|
128
|
+
# # returns a User with name 'initial name'
|
129
|
+
#
|
130
|
+
# user.update(name: 'some other name')
|
131
|
+
#
|
132
|
+
# user = registry.find_or_create(:user, email: email)
|
133
|
+
# # returns a User with name 'some other name'
|
134
|
+
def on(key, event, &block)
|
135
|
+
finder_creator_for(key).on(event, &block)
|
136
|
+
end
|
137
|
+
|
138
|
+
# Builds an instance of the registered model class
|
139
|
+
#
|
140
|
+
# @param key [String,Symbol] key under which the
|
141
|
+
# {FinderCreator finder_creator}
|
142
|
+
# is {Zyra::Registry#register registered}
|
143
|
+
#
|
144
|
+
# @param (see FinderCreator#find_or_create)
|
145
|
+
#
|
146
|
+
# @yield (see FinderCreator#find_or_create)
|
147
|
+
#
|
148
|
+
# @return [Object] A model either from the database or just
|
149
|
+
# inserted into it
|
150
|
+
#
|
151
|
+
# @see #register
|
152
|
+
# @see FinderCreator#find_or_create
|
153
|
+
#
|
154
|
+
# @example Regular usage passing all attributes
|
155
|
+
# registry = Zyra::Registry.new
|
156
|
+
# registry.register(User, find_by: :email)
|
157
|
+
#
|
158
|
+
# email = 'email@srv.com'
|
159
|
+
#
|
160
|
+
# user = registry.find_or_create(
|
161
|
+
# :user,
|
162
|
+
# email: email, name: 'initial name'
|
163
|
+
# )
|
164
|
+
# # returns a User with name 'initial name'
|
165
|
+
#
|
166
|
+
# user = registry.find_or_create(
|
167
|
+
# :user,
|
168
|
+
# email: email, name: 'final name'
|
169
|
+
# )
|
170
|
+
# # returns a User with name 'initial name'
|
171
|
+
def find_or_create(key, attributes, &block)
|
172
|
+
finder_creator_for(key).find_or_create(attributes, &block)
|
173
|
+
end
|
174
|
+
|
175
|
+
private
|
176
|
+
|
177
|
+
# @private
|
178
|
+
# @api private
|
179
|
+
# Returns a registered creator
|
180
|
+
#
|
181
|
+
# when the creator was not registerd, +nil+ is returned
|
182
|
+
#
|
183
|
+
# @param key [String,Symbol] key under which the creator is registered
|
184
|
+
#
|
185
|
+
# @return [Zyra::Creator]
|
186
|
+
def finder_creator_for(key)
|
187
|
+
registry[key.to_sym] ||
|
188
|
+
raise(Exceptions::NotRegistered)
|
189
|
+
end
|
190
|
+
|
191
|
+
# @private
|
192
|
+
# @api private
|
193
|
+
#
|
194
|
+
# Registry store for all creators
|
195
|
+
#
|
196
|
+
# @return [Hash] map of all registered creators
|
197
|
+
def registry
|
198
|
+
@registry ||= {}
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end
|
data/lib/zyra/version.rb
CHANGED
data/lib/zyra.rb
CHANGED
@@ -1,7 +1,193 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'active_support'
|
4
|
+
require 'active_support/core_ext'
|
5
|
+
require 'jace'
|
6
|
+
|
3
7
|
# @api public
|
4
8
|
# @author darthjee
|
5
|
-
|
9
|
+
#
|
10
|
+
# Zyra allows creators to be registered to ease up
|
11
|
+
# seeding
|
12
|
+
#
|
13
|
+
module Zyra
|
6
14
|
autoload :VERSION, 'zyra/version'
|
15
|
+
|
16
|
+
autoload :Creator, 'zyra/creator'
|
17
|
+
autoload :Exceptions, 'zyra/exceptions'
|
18
|
+
autoload :Finder, 'zyra/finder'
|
19
|
+
autoload :FinderCreator, 'zyra/finder_creator'
|
20
|
+
autoload :Registry, 'zyra/registry'
|
21
|
+
|
22
|
+
class << self
|
23
|
+
# @method register(klass, key)
|
24
|
+
# @api public
|
25
|
+
#
|
26
|
+
# Register a new creator
|
27
|
+
#
|
28
|
+
# The creator will focus on one class and be registered under a
|
29
|
+
# symbol key
|
30
|
+
#
|
31
|
+
# @param klass [Class] Model class to be used by the creator
|
32
|
+
#
|
33
|
+
# @overload register(klass)
|
34
|
+
# When the key is not provided, it is infered from the class name
|
35
|
+
#
|
36
|
+
# @overload register(klass, key)
|
37
|
+
# @param key [String,Symbol] key to be used when storyin the creator
|
38
|
+
#
|
39
|
+
# @return [Zyra::FinderCreator] registered finder_creator
|
40
|
+
#
|
41
|
+
# @example Register models searching
|
42
|
+
# registry = Zyra::Registry.new
|
43
|
+
#
|
44
|
+
# registry.register(User, find_by: :email)
|
45
|
+
# registry
|
46
|
+
# .register(User, :user_by_name, find_by: :name)
|
47
|
+
# .on(:return) do |user|
|
48
|
+
# user.update(email: "#{user.name.gsub(/ /, '_')}@srv.com")
|
49
|
+
# end
|
50
|
+
#
|
51
|
+
# attributes = {
|
52
|
+
# name: 'my name',
|
53
|
+
# email: 'my_email@srv.com'
|
54
|
+
# }
|
55
|
+
#
|
56
|
+
# user = registry.find_or_create(:user, attributes)
|
57
|
+
# # returns a User with name 'my_email@srv.com'
|
58
|
+
#
|
59
|
+
# user = registry.find_or_create(:user_by_name, attributes)
|
60
|
+
# # returns a User with name 'my_name@srv.com'
|
61
|
+
|
62
|
+
# @method on(key, event, &block)
|
63
|
+
# @api public
|
64
|
+
#
|
65
|
+
# Register a handler on a certain event
|
66
|
+
#
|
67
|
+
# Possible events are +found+, +build+, +create+
|
68
|
+
# and +return+
|
69
|
+
#
|
70
|
+
# @param (see Zyra::Registry#after)
|
71
|
+
# @yield (see Zyra::Registry#after)
|
72
|
+
# @return [Finder] The finder registered under that key
|
73
|
+
#
|
74
|
+
# @example Adding a hook on return
|
75
|
+
# Zyra.register(User, find_by: :email)
|
76
|
+
# Zyra.on(:user, :return) do |user|
|
77
|
+
# user.update(name: 'initial name')
|
78
|
+
# end
|
79
|
+
#
|
80
|
+
# email = 'email@srv.com'
|
81
|
+
#
|
82
|
+
# user = Zyra.find_or_create(
|
83
|
+
# :user,
|
84
|
+
# email: email
|
85
|
+
# )
|
86
|
+
# # returns a User with name 'initial name'
|
87
|
+
#
|
88
|
+
# user.update(name: 'some other name')
|
89
|
+
#
|
90
|
+
# user = Zyra.find_or_create(:user, email: email)
|
91
|
+
# # returns a User with name 'initial name'
|
92
|
+
#
|
93
|
+
# @example Adding a hook on found
|
94
|
+
# Zyra.register(User, find_by: :email)
|
95
|
+
# Zyra.on(:user, :found) do |user|
|
96
|
+
# user.update(name: 'final name')
|
97
|
+
# end
|
98
|
+
#
|
99
|
+
# email = 'email@srv.com'
|
100
|
+
# attributes = { email: email, name: 'initial name' }
|
101
|
+
#
|
102
|
+
# user = Zyra.find_or_create(:user, attributes)
|
103
|
+
# # returns a User with name 'initial name'
|
104
|
+
#
|
105
|
+
# user = Zyra.find_or_create(:user, attributes)
|
106
|
+
# # returns a User with name 'final name'
|
107
|
+
#
|
108
|
+
# @example Adding a hook on build
|
109
|
+
# Zyra.register(User, find_by: :email)
|
110
|
+
# Zyra.on(:user, :build) do |user|
|
111
|
+
# user.name = 'initial name'
|
112
|
+
# end
|
113
|
+
#
|
114
|
+
# email = 'email@srv.com'
|
115
|
+
#
|
116
|
+
# user = Zyra.find_or_create(:user, email: email)
|
117
|
+
# # returns a User with name 'initial name'
|
118
|
+
#
|
119
|
+
# user.update(name: 'some other name')
|
120
|
+
#
|
121
|
+
# user = Zyra.find_or_create(:user, email: email)
|
122
|
+
# # returns a User with name 'some other name'
|
123
|
+
#
|
124
|
+
# @example Adding a hook on create
|
125
|
+
# Zyra.register(User, find_by: :email)
|
126
|
+
# Zyra.on(:user, :create) do |user|
|
127
|
+
# user.update(name: 'initial name')
|
128
|
+
# end
|
129
|
+
#
|
130
|
+
# email = 'email@srv.com'
|
131
|
+
#
|
132
|
+
# user = Zyra.find_or_create(:user, email: email)
|
133
|
+
# # returns a User with name 'initial name'
|
134
|
+
#
|
135
|
+
# user.update(name: 'some other name')
|
136
|
+
#
|
137
|
+
# user = Zyra.find_or_create(:user, email: email)
|
138
|
+
# # returns a User with name 'some other name'
|
139
|
+
|
140
|
+
# @method find_or_create(key, attributes = {}, &block)
|
141
|
+
# @api public
|
142
|
+
#
|
143
|
+
# Builds an instance of the registered model class
|
144
|
+
#
|
145
|
+
# @param (see Zyra::Registry#after)
|
146
|
+
# @yield (see Zyra::Registry#after)
|
147
|
+
# @return [Object] A model either from the database or just
|
148
|
+
# inserted into it
|
149
|
+
#
|
150
|
+
# @see .register
|
151
|
+
#
|
152
|
+
# @example Regular usage passing all attributes
|
153
|
+
# Zyra.register(User, find_by: :email)
|
154
|
+
#
|
155
|
+
# email = 'email@srv.com'
|
156
|
+
#
|
157
|
+
# user = Zyra.find_or_create(
|
158
|
+
# :user,
|
159
|
+
# email: email, name: 'initial name'
|
160
|
+
# )
|
161
|
+
# # returns a User with name 'initial name'
|
162
|
+
#
|
163
|
+
# user = Zyra.find_or_create(
|
164
|
+
# :user,
|
165
|
+
# email: email, name: 'final name'
|
166
|
+
# )
|
167
|
+
# # returns a User with name 'initial name'
|
168
|
+
delegate :register, :on, :find_or_create, to: :registry
|
169
|
+
|
170
|
+
# @api private
|
171
|
+
#
|
172
|
+
# Resets the state of the registry
|
173
|
+
#
|
174
|
+
# This is mainly used for testing
|
175
|
+
#
|
176
|
+
# @return [NilClass]
|
177
|
+
def reset
|
178
|
+
@registry = nil
|
179
|
+
end
|
180
|
+
|
181
|
+
private
|
182
|
+
|
183
|
+
# @private
|
184
|
+
# @api private
|
185
|
+
#
|
186
|
+
# Returns the registry containing all the creators
|
187
|
+
#
|
188
|
+
# @return [Registry]
|
189
|
+
def registry
|
190
|
+
@registry ||= Registry.new
|
191
|
+
end
|
192
|
+
end
|
7
193
|
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Zyra do
|
6
|
+
describe 'readme' do
|
7
|
+
it 'registering a model' do
|
8
|
+
Zyra
|
9
|
+
.register(User, find_by: :email)
|
10
|
+
.on(:build) do |user|
|
11
|
+
user.reference = SecureRandom.hex(16)
|
12
|
+
end
|
13
|
+
|
14
|
+
attributes = {
|
15
|
+
email: 'usr@srv.com',
|
16
|
+
name: 'Some User',
|
17
|
+
password: 'pass'
|
18
|
+
}
|
19
|
+
|
20
|
+
user = Zyra.find_or_create(:user, attributes) do |usr|
|
21
|
+
usr.update(attributes)
|
22
|
+
end
|
23
|
+
|
24
|
+
expect(user.name).to eq('Some User')
|
25
|
+
expect(user.email).to eq('usr@srv.com')
|
26
|
+
expect(user.password).to eq('pass')
|
27
|
+
expect(user.reference).not_to be_nil
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'Registering hooks' do
|
31
|
+
Zyra
|
32
|
+
.register(User, find_by: :email)
|
33
|
+
.on(:build) do |user|
|
34
|
+
user.posts.build(name: 'first', content: 'some content')
|
35
|
+
end
|
36
|
+
|
37
|
+
Zyra.on(:user, :return) do |user|
|
38
|
+
user.update(reference: SecureRandom.hex(16))
|
39
|
+
end
|
40
|
+
|
41
|
+
attributes = {
|
42
|
+
email: 'usr@srv.com',
|
43
|
+
name: 'Some User',
|
44
|
+
password: 'pass'
|
45
|
+
}
|
46
|
+
|
47
|
+
user = Zyra.find_or_create(:user, attributes).reload
|
48
|
+
|
49
|
+
expect(user.email).to eq('usr@srv.com')
|
50
|
+
expect(user.posts).not_to be_empty
|
51
|
+
expect(user.reference).not_to be_nil
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,135 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Zyra::Registry do
|
6
|
+
describe 'yard' do
|
7
|
+
describe '#register' do
|
8
|
+
it 'Register models searching' do
|
9
|
+
registry = Zyra::Registry.new
|
10
|
+
|
11
|
+
registry.register(User, find_by: :email)
|
12
|
+
registry
|
13
|
+
.register(User, :user_by_name, find_by: :name)
|
14
|
+
.on(:return) do |user|
|
15
|
+
user.update(email: "#{user.name.gsub(/ /, '_')}@srv.com")
|
16
|
+
end
|
17
|
+
|
18
|
+
attributes = {
|
19
|
+
name: 'my name',
|
20
|
+
email: 'my_email@srv.com'
|
21
|
+
}
|
22
|
+
|
23
|
+
user = registry.find_or_create(:user, attributes)
|
24
|
+
expect(user.email).to eq('my_email@srv.com')
|
25
|
+
|
26
|
+
user = registry.find_or_create(:user_by_name, attributes)
|
27
|
+
expect(user.email).to eq('my_name@srv.com')
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe '#find_or_create' do
|
32
|
+
it 'Regular usage passing all attributes' do
|
33
|
+
registry = Zyra::Registry.new
|
34
|
+
registry.register(User, find_by: :email)
|
35
|
+
|
36
|
+
email = 'email@srv.com'
|
37
|
+
|
38
|
+
user = registry.find_or_create(
|
39
|
+
:user,
|
40
|
+
email: email, name: 'initial name'
|
41
|
+
)
|
42
|
+
|
43
|
+
expect(user.name).to eq('initial name')
|
44
|
+
|
45
|
+
user = registry.find_or_create(
|
46
|
+
:user,
|
47
|
+
email: email, name: 'final name'
|
48
|
+
)
|
49
|
+
|
50
|
+
expect(user.name).to eq('initial name')
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe '#on' do
|
55
|
+
it 'Adding a hook on return' do
|
56
|
+
registry = Zyra::Registry.new
|
57
|
+
registry.register(User, find_by: :email)
|
58
|
+
registry.on(:user, :return) do |user|
|
59
|
+
user.update(name: 'initial name')
|
60
|
+
end
|
61
|
+
|
62
|
+
email = 'email@srv.com'
|
63
|
+
|
64
|
+
user = registry.find_or_create(
|
65
|
+
:user,
|
66
|
+
email: email
|
67
|
+
)
|
68
|
+
|
69
|
+
expect(user.name).to eq('initial name')
|
70
|
+
user.update(name: 'some other name')
|
71
|
+
|
72
|
+
user = registry.find_or_create(:user, email: email)
|
73
|
+
|
74
|
+
expect(user.name).to eq('initial name')
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'Adding a hook on found' do
|
78
|
+
registry = Zyra::Registry.new
|
79
|
+
registry.register(User, find_by: :email)
|
80
|
+
registry.on(:user, :found) do |user|
|
81
|
+
user.update(name: 'final name')
|
82
|
+
end
|
83
|
+
|
84
|
+
email = 'email@srv.com'
|
85
|
+
attributes = { email: email, name: 'initial name' }
|
86
|
+
|
87
|
+
user = registry.find_or_create(:user, attributes)
|
88
|
+
|
89
|
+
expect(user.name).to eq('initial name')
|
90
|
+
|
91
|
+
user = registry.find_or_create(:user, attributes)
|
92
|
+
|
93
|
+
expect(user.name).to eq('final name')
|
94
|
+
end
|
95
|
+
|
96
|
+
it 'Adding a hook on build' do
|
97
|
+
registry = Zyra::Registry.new
|
98
|
+
registry.register(User, find_by: :email)
|
99
|
+
registry.on(:user, :build) do |user|
|
100
|
+
user.name = 'initial name'
|
101
|
+
end
|
102
|
+
|
103
|
+
email = 'email@srv.com'
|
104
|
+
|
105
|
+
user = registry.find_or_create(:user, email: email)
|
106
|
+
|
107
|
+
expect(user.name).to eq('initial name')
|
108
|
+
user.update(name: 'some other name')
|
109
|
+
|
110
|
+
user = registry.find_or_create(:user, email: email)
|
111
|
+
|
112
|
+
expect(user.name).to eq('some other name')
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'Adding a hook on create' do
|
116
|
+
registry = Zyra::Registry.new
|
117
|
+
registry.register(User, find_by: :email)
|
118
|
+
registry.on(:user, :create) do |user|
|
119
|
+
user.update(name: 'initial name')
|
120
|
+
end
|
121
|
+
|
122
|
+
email = 'email@srv.com'
|
123
|
+
|
124
|
+
user = registry.find_or_create(:user, email: email)
|
125
|
+
|
126
|
+
expect(user.name).to eq('initial name')
|
127
|
+
user.update(name: 'some other name')
|
128
|
+
|
129
|
+
user = registry.find_or_create(:user, email: email)
|
130
|
+
|
131
|
+
expect(user.name).to eq('some other name')
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|