tradesman 1.0.9 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -51
- data/lib/tradesman/version.rb +3 -0
- data/lib/tradesman.rb +6 -10
- data/spec/tradesman_spec.rb +25 -58
- metadata +34 -34
- data/lib/tradesman/configuration.rb +0 -40
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7367633d97bb0c1c6404f5beeda0881f28d7b13b
|
4
|
+
data.tar.gz: 917266c0de9a26b681f03bdf32ae50eef26a97b1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fcb507aa6a41d362a4f95e77da96159098245151770a474bf36de12ce6230907125824c4b6d6d5c5950a6f9fa7b6d18dadc9708db89b208cef841eccfd823ec5
|
7
|
+
data.tar.gz: 4c49af6cf0917573be2cd6fe876393a03678ec8d1bafc39bb0fb8a9a6337bc30031810bd2c10d988f8f93c1085110e99570b3a76b729dcc72a791ae6f9aa8077
|
data/README.md
CHANGED
@@ -211,63 +211,17 @@ end
|
|
211
211
|
|
212
212
|
The Tradesman version is self-documenting, cruft-free, and designed for testing.
|
213
213
|
|
214
|
-
## Config
|
215
214
|
|
216
|
-
|
215
|
+
## Config
|
216
|
+
<tt>Tradesman</tt> uses the underlying Horza configuration, so all configuration options can be set on <tt>Tradesman</tt> just like they would on <tt>Horza</tt>.
|
217
217
|
|
218
|
-
|
218
|
+
***e.g. Defining your adapter***
|
219
219
|
|
220
|
+
_config/initializers/tradesman.rb_
|
220
221
|
```ruby
|
221
|
-
# config/initializers/tradesman.rb
|
222
222
|
Tradesman.configure { |config| config.adapter = :active_record }
|
223
223
|
```
|
224
|
-
|
225
|
-
**Development Mode**
|
226
|
-
|
227
|
-
Turn on development mode to help Tradesman cope with Rails' lazy-loading of classes.
|
228
|
-
|
229
|
-
```ruby
|
230
|
-
# config/initializers/tradesman.rb
|
231
|
-
Tradesman.configure do |config|
|
232
|
-
config.adapter = :active_record
|
233
|
-
config.development_mode = Rails.env.development?
|
234
|
-
end
|
235
|
-
```
|
236
|
-
|
237
|
-
**Model Namespaces**
|
238
|
-
|
239
|
-
Lazy loading becomes more complex if your models are namespaced.
|
240
|
-
|
241
|
-
Consider:
|
242
|
-
```ruby
|
243
|
-
module MyNamespace
|
244
|
-
class Employer < ActiveRecord::Base
|
245
|
-
has_many :users
|
246
|
-
end
|
247
|
-
end
|
248
|
-
|
249
|
-
module MyOtherNamespace
|
250
|
-
class User < ActiveRecord::Base
|
251
|
-
belongs_to :employer
|
252
|
-
end
|
253
|
-
end
|
254
|
-
```
|
255
|
-
|
256
|
-
In order to help Tradesman lazy load these models, you need to explicitly configure any namespaces:
|
257
|
-
|
258
|
-
```ruby
|
259
|
-
# config/initializers/tradesman.rb
|
260
|
-
Tradesman.configure do |config|
|
261
|
-
config.adapter = :active_record
|
262
|
-
config.development_mode = Rails.env.development?
|
263
|
-
config.namespaces = [MyNamespace, MyOtherNamespace]
|
264
|
-
end
|
265
|
-
```
|
266
|
-
|
267
|
-
**Reset Tradesman** _(Can be done at runtime or in tests)_
|
268
|
-
```ruby
|
269
|
-
Tradesman.reset
|
270
|
-
```
|
224
|
+
For more details on configuration check out the <tt>Horza</tt> [documentation.](https://github.com/onfido/horza)
|
271
225
|
|
272
226
|
## Mocking & Stubbing in Tests
|
273
227
|
|
data/lib/tradesman.rb
CHANGED
@@ -9,22 +9,18 @@ require 'tradesman/builders/create_for_parent'
|
|
9
9
|
require 'tradesman/builders/delete'
|
10
10
|
require 'tradesman/builders/update'
|
11
11
|
require 'tradesman/class_methods'
|
12
|
-
require 'tradesman/configuration'
|
13
12
|
require 'tradesman/errors'
|
14
13
|
require 'tradesman/error_handling'
|
15
14
|
require 'tradesman/parser'
|
16
15
|
require 'tradesman/template'
|
16
|
+
require 'tradesman/version'
|
17
17
|
|
18
18
|
module Tradesman
|
19
|
-
extend
|
19
|
+
extend Horza::SharedConfig
|
20
20
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
parser = ::Tradesman::Parser.new(class_name)
|
26
|
-
return super(class_name) unless parser.match?
|
27
|
-
Builders.generate_class(parser)
|
28
|
-
end
|
21
|
+
def self.const_missing(class_name)
|
22
|
+
parser = ::Tradesman::Parser.new(class_name)
|
23
|
+
return super(class_name) unless parser.match?
|
24
|
+
Builders.generate_class(parser)
|
29
25
|
end
|
30
26
|
end
|
data/spec/tradesman_spec.rb
CHANGED
@@ -13,30 +13,30 @@ else
|
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
-
module TradesmanSpec
|
17
|
-
class Employer < ActiveRecord::Base
|
18
|
-
has_many :users
|
19
|
-
end
|
20
16
|
|
21
|
-
|
22
|
-
|
23
|
-
|
17
|
+
class Employer < ActiveRecord::Base
|
18
|
+
has_many :users
|
19
|
+
end
|
24
20
|
|
25
|
-
|
26
|
-
|
21
|
+
class User < ActiveRecord::Base
|
22
|
+
belongs_to :employer
|
23
|
+
end
|
27
24
|
|
28
|
-
|
29
|
-
|
25
|
+
class StrictUser < ActiveRecord::Base
|
26
|
+
belongs_to :employer
|
27
|
+
|
28
|
+
validates :last_name, presence: true
|
30
29
|
end
|
30
|
+
|
31
31
|
end
|
32
32
|
|
33
33
|
describe Tradesman do
|
34
34
|
let(:adapter) { :active_record }
|
35
35
|
before { Tradesman.configure { |config| config.adapter = adapter } }
|
36
36
|
after do
|
37
|
-
|
38
|
-
|
39
|
-
|
37
|
+
User.delete_all
|
38
|
+
StrictUser.delete_all
|
39
|
+
Employer.delete_all
|
40
40
|
end
|
41
41
|
|
42
42
|
context '#configure' do
|
@@ -51,44 +51,11 @@ describe Tradesman do
|
|
51
51
|
after { Tradesman.reset }
|
52
52
|
|
53
53
|
it 'throws error' do
|
54
|
-
expect { Tradesman.adapter }.to raise_error(
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
context 'namespaces' do
|
60
|
-
context 'when no namespaces are set' do
|
61
|
-
it 'does not forward namespaces to Horza' do
|
62
|
-
expect(Horza.configuration.namespaces.empty?).to be true
|
63
|
-
end
|
64
|
-
end
|
65
|
-
context 'when namespaces are set' do
|
66
|
-
before do
|
67
|
-
Tradesman.configure { |config| config.namespaces = [TradesmanSpec] }
|
68
|
-
end
|
69
|
-
|
70
|
-
it 'forwards namespaces to Horza' do
|
71
|
-
expect(Horza.configuration.namespaces).to eq [TradesmanSpec]
|
54
|
+
expect { Tradesman.adapter }.to raise_error(Horza::Errors::AdapterError)
|
72
55
|
end
|
73
56
|
end
|
74
57
|
end
|
75
58
|
|
76
|
-
context 'development_mode' do
|
77
|
-
context 'when development_mode is not set' do
|
78
|
-
it 'does not forward namespaces to Horza' do
|
79
|
-
expect(Horza.configuration.development_mode).to be nil
|
80
|
-
end
|
81
|
-
end
|
82
|
-
context 'when namespaces are set' do
|
83
|
-
before do
|
84
|
-
Tradesman.configure { |config| config.development_mode = true }
|
85
|
-
end
|
86
|
-
|
87
|
-
it 'forwards namespaces to Horza' do
|
88
|
-
expect(Horza.configuration.development_mode).to be true
|
89
|
-
end
|
90
|
-
end
|
91
|
-
end
|
92
59
|
|
93
60
|
context '#go' do
|
94
61
|
context 'Create' do
|
@@ -145,7 +112,7 @@ describe Tradesman do
|
|
145
112
|
end
|
146
113
|
|
147
114
|
context 'for parent' do
|
148
|
-
let(:employer) {
|
115
|
+
let(:employer) { Employer.create }
|
149
116
|
let(:outcome) { Tradesman::CreateUserForEmployer.go(employer.id, last_name: 'Turner') }
|
150
117
|
|
151
118
|
it 'creates a new record' do
|
@@ -198,7 +165,7 @@ describe Tradesman do
|
|
198
165
|
end
|
199
166
|
|
200
167
|
context 'Update' do
|
201
|
-
let(:user) {
|
168
|
+
let(:user) { User.create(last_name: 'Smith') }
|
202
169
|
context 'when parameters are valid' do
|
203
170
|
let(:outcome) { Tradesman::UpdateUser.go(user.id, last_name: 'Turner') }
|
204
171
|
|
@@ -213,7 +180,7 @@ describe Tradesman do
|
|
213
180
|
end
|
214
181
|
|
215
182
|
context 'when parameters are invalid' do
|
216
|
-
let(:strict_user) {
|
183
|
+
let(:strict_user) { StrictUser.create(last_name: 'Smith') }
|
217
184
|
let(:outcome) { Tradesman::UpdateStrictUser.go(strict_user, last_name: nil) }
|
218
185
|
|
219
186
|
it 'returns an invalid outcome' do
|
@@ -284,7 +251,7 @@ describe Tradesman do
|
|
284
251
|
|
285
252
|
context 'Delete' do
|
286
253
|
context 'when parameters are valid' do
|
287
|
-
let!(:user) {
|
254
|
+
let!(:user) { User.create }
|
288
255
|
let(:outcome) { Tradesman::DeleteUser.go(user) }
|
289
256
|
|
290
257
|
it 'executes successfully' do
|
@@ -293,7 +260,7 @@ describe Tradesman do
|
|
293
260
|
end
|
294
261
|
|
295
262
|
it 'deletes record' do
|
296
|
-
expect { outcome }.to change(
|
263
|
+
expect { outcome }.to change(User, :count).by(-1)
|
297
264
|
end
|
298
265
|
end
|
299
266
|
|
@@ -361,7 +328,7 @@ describe Tradesman do
|
|
361
328
|
end
|
362
329
|
|
363
330
|
context 'for parent' do
|
364
|
-
let(:employer) {
|
331
|
+
let(:employer) { Employer.create }
|
365
332
|
let(:outcome) { Tradesman::CreateUserForEmployer.go!(employer.id, last_name: 'Turner') }
|
366
333
|
|
367
334
|
it 'creates a new record' do
|
@@ -382,7 +349,7 @@ describe Tradesman do
|
|
382
349
|
end
|
383
350
|
|
384
351
|
context 'Update' do
|
385
|
-
let(:user) {
|
352
|
+
let(:user) { User.create(last_name: 'Smith') }
|
386
353
|
context 'when parameters are valid' do
|
387
354
|
let(:outcome) { Tradesman::UpdateUser.go!(user.id, last_name: 'Turner') }
|
388
355
|
|
@@ -397,7 +364,7 @@ describe Tradesman do
|
|
397
364
|
end
|
398
365
|
|
399
366
|
context 'when parameters are invalid' do
|
400
|
-
let(:strict_user) {
|
367
|
+
let(:strict_user) { StrictUser.create(last_name: 'Smith') }
|
401
368
|
let(:outcome) { Tradesman::UpdateStrictUser.go!(strict_user, last_name: nil) }
|
402
369
|
|
403
370
|
it 'throws error' do
|
@@ -408,7 +375,7 @@ describe Tradesman do
|
|
408
375
|
|
409
376
|
context 'Delete' do
|
410
377
|
context 'when parameters are valid' do
|
411
|
-
let!(:user) {
|
378
|
+
let!(:user) { User.create }
|
412
379
|
let(:outcome) { Tradesman::DeleteUser.go!(user) }
|
413
380
|
|
414
381
|
it 'executes successfully' do
|
@@ -417,7 +384,7 @@ describe Tradesman do
|
|
417
384
|
end
|
418
385
|
|
419
386
|
it 'deletes record' do
|
420
|
-
expect { outcome }.to change(
|
387
|
+
expect { outcome }.to change(User, :count).by(-1)
|
421
388
|
end
|
422
389
|
end
|
423
390
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tradesman
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Blake Turner
|
@@ -9,118 +9,118 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-
|
12
|
+
date: 2015-11-30 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: horza
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
17
|
requirements:
|
18
|
-
- -
|
18
|
+
- - ~>
|
19
19
|
- !ruby/object:Gem::Version
|
20
|
-
version:
|
20
|
+
version: '1.0'
|
21
21
|
type: :runtime
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
|
-
- -
|
25
|
+
- - ~>
|
26
26
|
- !ruby/object:Gem::Version
|
27
|
-
version:
|
27
|
+
version: '1.0'
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: tzu
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|
31
31
|
requirements:
|
32
|
-
- -
|
32
|
+
- - ~>
|
33
33
|
- !ruby/object:Gem::Version
|
34
34
|
version: '0.1'
|
35
35
|
type: :runtime
|
36
36
|
prerelease: false
|
37
37
|
version_requirements: !ruby/object:Gem::Requirement
|
38
38
|
requirements:
|
39
|
-
- -
|
39
|
+
- - ~>
|
40
40
|
- !ruby/object:Gem::Version
|
41
41
|
version: '0.1'
|
42
42
|
- !ruby/object:Gem::Dependency
|
43
43
|
name: bundler
|
44
44
|
requirement: !ruby/object:Gem::Requirement
|
45
45
|
requirements:
|
46
|
-
- -
|
46
|
+
- - '>='
|
47
47
|
- !ruby/object:Gem::Version
|
48
48
|
version: 1.0.0
|
49
49
|
type: :development
|
50
50
|
prerelease: false
|
51
51
|
version_requirements: !ruby/object:Gem::Requirement
|
52
52
|
requirements:
|
53
|
-
- -
|
53
|
+
- - '>='
|
54
54
|
- !ruby/object:Gem::Version
|
55
55
|
version: 1.0.0
|
56
56
|
- !ruby/object:Gem::Dependency
|
57
57
|
name: activerecord
|
58
58
|
requirement: !ruby/object:Gem::Requirement
|
59
59
|
requirements:
|
60
|
-
- -
|
60
|
+
- - ~>
|
61
61
|
- !ruby/object:Gem::Version
|
62
|
-
version: 3.2
|
62
|
+
version: '3.2'
|
63
63
|
type: :development
|
64
64
|
prerelease: false
|
65
65
|
version_requirements: !ruby/object:Gem::Requirement
|
66
66
|
requirements:
|
67
|
-
- -
|
67
|
+
- - ~>
|
68
68
|
- !ruby/object:Gem::Version
|
69
|
-
version: 3.2
|
69
|
+
version: '3.2'
|
70
70
|
- !ruby/object:Gem::Dependency
|
71
71
|
name: activesupport
|
72
72
|
requirement: !ruby/object:Gem::Requirement
|
73
73
|
requirements:
|
74
|
-
- -
|
74
|
+
- - ~>
|
75
75
|
- !ruby/object:Gem::Version
|
76
|
-
version: 3.2
|
76
|
+
version: '3.2'
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
79
|
version_requirements: !ruby/object:Gem::Requirement
|
80
80
|
requirements:
|
81
|
-
- -
|
81
|
+
- - ~>
|
82
82
|
- !ruby/object:Gem::Version
|
83
|
-
version: 3.2
|
83
|
+
version: '3.2'
|
84
84
|
- !ruby/object:Gem::Dependency
|
85
85
|
name: rspec
|
86
86
|
requirement: !ruby/object:Gem::Requirement
|
87
87
|
requirements:
|
88
|
-
- -
|
88
|
+
- - '>='
|
89
89
|
- !ruby/object:Gem::Version
|
90
|
-
version:
|
90
|
+
version: '0'
|
91
91
|
type: :development
|
92
92
|
prerelease: false
|
93
93
|
version_requirements: !ruby/object:Gem::Requirement
|
94
94
|
requirements:
|
95
|
-
- -
|
95
|
+
- - '>='
|
96
96
|
- !ruby/object:Gem::Version
|
97
|
-
version:
|
97
|
+
version: '0'
|
98
98
|
- !ruby/object:Gem::Dependency
|
99
99
|
name: sqlite3
|
100
100
|
requirement: !ruby/object:Gem::Requirement
|
101
101
|
requirements:
|
102
|
-
- -
|
102
|
+
- - '>='
|
103
103
|
- !ruby/object:Gem::Version
|
104
104
|
version: '0'
|
105
105
|
type: :development
|
106
106
|
prerelease: false
|
107
107
|
version_requirements: !ruby/object:Gem::Requirement
|
108
108
|
requirements:
|
109
|
-
- -
|
109
|
+
- - '>='
|
110
110
|
- !ruby/object:Gem::Version
|
111
111
|
version: '0'
|
112
112
|
- !ruby/object:Gem::Dependency
|
113
113
|
name: byebug
|
114
114
|
requirement: !ruby/object:Gem::Requirement
|
115
115
|
requirements:
|
116
|
-
- -
|
116
|
+
- - '>='
|
117
117
|
- !ruby/object:Gem::Version
|
118
118
|
version: '0'
|
119
119
|
type: :development
|
120
120
|
prerelease: false
|
121
121
|
version_requirements: !ruby/object:Gem::Requirement
|
122
122
|
requirements:
|
123
|
-
- -
|
123
|
+
- - '>='
|
124
124
|
- !ruby/object:Gem::Version
|
125
125
|
version: '0'
|
126
126
|
description: Encapsulate common application behaviour with dynamically generated classes
|
@@ -129,23 +129,23 @@ executables: []
|
|
129
129
|
extensions: []
|
130
130
|
extra_rdoc_files: []
|
131
131
|
files:
|
132
|
-
- LICENSE.txt
|
133
|
-
- README.md
|
134
|
-
- lib/tradesman.rb
|
135
|
-
- lib/tradesman/builders.rb
|
136
132
|
- lib/tradesman/builders/base.rb
|
137
133
|
- lib/tradesman/builders/create.rb
|
138
134
|
- lib/tradesman/builders/create_for_parent.rb
|
139
135
|
- lib/tradesman/builders/delete.rb
|
140
136
|
- lib/tradesman/builders/update.rb
|
137
|
+
- lib/tradesman/builders.rb
|
141
138
|
- lib/tradesman/class_methods.rb
|
142
|
-
- lib/tradesman/configuration.rb
|
143
139
|
- lib/tradesman/error_handling.rb
|
144
140
|
- lib/tradesman/errors.rb
|
145
141
|
- lib/tradesman/mixins/existing_records_multiple_execute.rb
|
146
142
|
- lib/tradesman/mixins/new_records_multiple_execute.rb
|
147
143
|
- lib/tradesman/parser.rb
|
148
144
|
- lib/tradesman/template.rb
|
145
|
+
- lib/tradesman/version.rb
|
146
|
+
- lib/tradesman.rb
|
147
|
+
- LICENSE.txt
|
148
|
+
- README.md
|
149
149
|
- spec/builders_spec.rb
|
150
150
|
- spec/parser_spec.rb
|
151
151
|
- spec/spec_helper.rb
|
@@ -160,17 +160,17 @@ require_paths:
|
|
160
160
|
- lib
|
161
161
|
required_ruby_version: !ruby/object:Gem::Requirement
|
162
162
|
requirements:
|
163
|
-
- -
|
163
|
+
- - '>='
|
164
164
|
- !ruby/object:Gem::Version
|
165
165
|
version: '0'
|
166
166
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
167
167
|
requirements:
|
168
|
-
- -
|
168
|
+
- - '>='
|
169
169
|
- !ruby/object:Gem::Version
|
170
170
|
version: '0'
|
171
171
|
requirements: []
|
172
172
|
rubyforge_project:
|
173
|
-
rubygems_version: 2.
|
173
|
+
rubygems_version: 2.0.14
|
174
174
|
signing_key:
|
175
175
|
specification_version: 4
|
176
176
|
summary: Tradesman dynamically generates classes with human-readble names that handle
|
@@ -1,40 +0,0 @@
|
|
1
|
-
module Tradesman
|
2
|
-
module Configuration
|
3
|
-
def configuration
|
4
|
-
@configuration ||= Config.new
|
5
|
-
end
|
6
|
-
|
7
|
-
def reset
|
8
|
-
@configuration = Config.new
|
9
|
-
@adapter, @adapter_map = nil, nil # Class-level cache clear
|
10
|
-
end
|
11
|
-
|
12
|
-
def configure
|
13
|
-
yield(configuration)
|
14
|
-
end
|
15
|
-
|
16
|
-
def adapter
|
17
|
-
raise ::Tradesman::Base.new('Adapter has not been configured') unless configuration.adapter
|
18
|
-
@adapter ||= configuration.adapter
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
class Config
|
23
|
-
attr_accessor :adapter, :namespaces
|
24
|
-
|
25
|
-
def adapter=(adapter)
|
26
|
-
Horza.configure { |config| config.adapter = adapter }
|
27
|
-
@adapter = Horza.adapter
|
28
|
-
end
|
29
|
-
|
30
|
-
def development_mode=(mode)
|
31
|
-
Horza.configure { |config| config.development_mode = mode }
|
32
|
-
end
|
33
|
-
|
34
|
-
def namespaces=(namespaces)
|
35
|
-
fail Tradesman::Base.new 'namespaces must be an array' unless namespaces.is_a? Array
|
36
|
-
Horza.configure { |config| config.namespaces = namespaces }
|
37
|
-
@namespaces = namespaces
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|