zyra 0.0.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/zyra/registry.rb CHANGED
@@ -1,33 +1,199 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Zyra
4
- # @api private
4
+ # @api public
5
5
  #
6
- # Registry of all registered builders
6
+ # Registry of all registered creators
7
7
  class Registry
8
- # (see Zyra.register)
9
- def register(klass, key = klass.name.gsub(/::([A-Z])/, '_\1').downcase)
10
- registry[key.to_sym] = Builder.new(klass)
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)
11
136
  end
12
137
 
13
- # Returns a registered builder
138
+ # Builds an instance of the registered model class
14
139
  #
15
- # when the builder was not registerd, +nil+ is returned
140
+ # @param key [String,Symbol] key under which the
141
+ # {FinderCreator finder_creator}
142
+ # is {Zyra::Registry#register registered}
16
143
  #
17
- # @param key [String,Symbol] key under which the builder is registered
144
+ # @param (see FinderCreator#find_or_create)
18
145
  #
19
- # @return [Zyra::Builder]
20
- def builder_for(key)
21
- registry[key.to_sym]
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)
22
173
  end
23
174
 
24
175
  private
25
176
 
26
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
27
193
  #
28
- # Registry store for all builders
194
+ # Registry store for all creators
29
195
  #
30
- # @return [Hash] map of all registered builders
196
+ # @return [Hash] map of all registered creators
31
197
  def registry
32
198
  @registry ||= {}
33
199
  end
data/lib/zyra/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Zyra
4
- VERSION = '0.0.2'
4
+ VERSION = '1.0.0'
5
5
  end
data/lib/zyra.rb CHANGED
@@ -1,84 +1,171 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'sinclair'
3
+ require 'active_support'
4
+ require 'active_support/core_ext'
4
5
  require 'jace'
5
6
 
6
7
  # @api public
7
8
  # @author darthjee
8
9
  #
9
- # Zyra allows builders to be registered to ease up
10
+ # Zyra allows creators to be registered to ease up
10
11
  # seeding
12
+ #
11
13
  module Zyra
12
14
  autoload :VERSION, 'zyra/version'
13
15
 
14
- autoload :Builder, 'zyra/builder'
15
- autoload :Exceptions, 'zyra/exceptions'
16
- autoload :Registry, 'zyra/registry'
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'
17
21
 
18
22
  class << self
19
- # @method .register
23
+ # @method register(klass, key)
20
24
  # @api public
21
25
  #
22
- # Register a new builder
26
+ # Register a new creator
23
27
  #
24
- # The builder will focus on one class and be registered under a
28
+ # The creator will focus on one class and be registered under a
25
29
  # symbol key
26
30
  #
27
- # @param klass [Class] Model class to be used by the builder
31
+ # @param klass [Class] Model class to be used by the creator
28
32
  #
29
33
  # @overload register(klass)
30
34
  # When the key is not provided, it is infered from the class name
31
35
  #
32
36
  # @overload register(klass, key)
33
- # @param key [String,Symbol] key to be used when storyin the builder
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
+ # }
34
55
  #
35
- # @return [Zyra::Builder] registered builder
36
- delegate :register, to: :registry
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'
37
61
 
62
+ # @method on(key, event, &block)
63
+ # @api public
64
+ #
38
65
  # Register a handler on a certain event
39
66
  #
40
- # Possible events are +build+, +create+
67
+ # Possible events are +found+, +build+, +create+
68
+ # and +return+
41
69
  #
42
- # @param key [String,Symbol] key under which the {Builder builder}
43
- # is {Registry registered}
44
- # @param (see Builder#after)
70
+ # @param (see Zyra::Registry#after)
71
+ # @yield (see Zyra::Registry#after)
72
+ # @return [Finder] The finder registered under that key
45
73
  #
46
- # @yield [Object] the model built
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
47
79
  #
48
- # @return (see Builder#after)
49
- def after(key, event, &block)
50
- builder_for(key).after(event, &block)
51
- end
52
-
53
- # Builds an instance of the registered model class
80
+ # email = 'email@srv.com'
54
81
  #
55
- # @param key [String,Symbol] key under which the {Builder builder}
56
- # is {Registry registered}
57
- # @param (see Builder#build)
82
+ # user = Zyra.find_or_create(
83
+ # :user,
84
+ # email: email
85
+ # )
86
+ # # returns a User with name 'initial name'
58
87
  #
59
- # @yield (see Builder#build)
88
+ # user.update(name: 'some other name')
60
89
  #
61
- # @return (see Builder#build)
90
+ # user = Zyra.find_or_create(:user, email: email)
91
+ # # returns a User with name 'initial name'
62
92
  #
63
- # @see .register
64
- # @see Builder#build
65
- def build(key, **attributes, &block)
66
- builder_for(key).build(**attributes, &block)
67
- end
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'
68
139
 
69
- # Creates an instance of the registered model class
140
+ # @method find_or_create(key, attributes = {}, &block)
141
+ # @api public
70
142
  #
71
- # This behaves like {.build}, but persists the entry
143
+ # Builds an instance of the registered model class
72
144
  #
73
- # @param (see .build)
74
- # @yield (see .build)
75
- # @return (see .build)
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
76
149
  #
77
150
  # @see .register
78
- # @see Builder#create
79
- def create(key, **attributes, &block)
80
- builder_for(key).create(**attributes, &block)
81
- end
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
82
169
 
83
170
  # @api private
84
171
  #
@@ -96,20 +183,7 @@ module Zyra
96
183
  # @private
97
184
  # @api private
98
185
  #
99
- # Returns a registered builder for a key
100
- #
101
- # @param key [String,Symbol] key under which the {Builder builder}
102
- # is {Registry registered}
103
- #
104
- # @return [Builder]
105
- def builder_for(key)
106
- registry.builder_for(key) || raise(Exceptions::BuilderNotRegistered)
107
- end
108
-
109
- # @private
110
- # @api private
111
- #
112
- # Returns the registry containing all the builders
186
+ # Returns the registry containing all the creators
113
187
  #
114
188
  # @return [Registry]
115
189
  def registry
@@ -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