tradesman 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 88a8291acc6f38c6d19ff311c2c2ab950c755288
4
- data.tar.gz: dca95fa986b0e498bec27f5205fbec416d547c78
3
+ metadata.gz: 0ea231527a974f62567a09d96470e63b76b4d4dc
4
+ data.tar.gz: 69f01315549ff78600d26241d780ce82a05bb680
5
5
  SHA512:
6
- metadata.gz: e403a3da8460e086194e472c92d2827e2e132da3f2150dc26b7a7d248b476f851289f70e6fc95959efd7ee90646981eaaf29bd12a78c94c111fe918893b8563d
7
- data.tar.gz: c601b711e3e59ae362c9fed58870c1b5e3f92e488963d49a9a956c27c0156e9869ce7520bdfc50e55d46643f4cd9ca67006efb26253523e3972fa2f4b178ba4e
6
+ metadata.gz: d96de3e357daa072bc382cd35e20ef70c90c7cb685a96a5a5c5351b3e275e8e5b363984e460165eca368fe3247ef3fef0dcb68544508922948499c1a8e455524
7
+ data.tar.gz: d36f8d0d668865ac215bd49674132fd74bf40229fb61b12f77c16530ba00f8a0c736e613626d704c62750f1832e6b6146c3853487cb212828fac8bee31926b18
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Tradesman
2
2
 
3
- Tradesman lets you invoke human-readble classes that handle the pass, fail, and invalid cases of common create, update, and delete actions without having to write the code!
3
+ Tradesman lets you invoke human-readble classes that handle the pass, fail, and invalid cases of common create, update, and delete actions.
4
4
 
5
5
  ## Usage
6
6
 
@@ -19,7 +19,8 @@ outcome.result #=> Error Class
19
19
  outcome.type #=> :validation
20
20
 
21
21
  # With invalid parameters - fail loudly!
22
- outcome = Tradesman::CreateUser.go!(invalid_user_params) #=> raises Tradesman::Invalid or Tradesman::Failure
22
+ outcome = Tradesman::CreateUser.go!(invalid_user_params)
23
+ #=> raises Tradesman::Invalid or Tradesman::Failure
23
24
 
24
25
  # Passing a block - Well-suited for Controllers
25
26
  Tradesman::UpdateUser.go(params[:id], user_update_params) do
@@ -52,8 +53,6 @@ Tradesman::CreateUserForEmployer.go(employer, [user_params, user_params, user_pa
52
53
  Tradesman::UpdateUser.go([user1, user2, user3], update_params)
53
54
 
54
55
  # Update n records with n sets of parameters
55
- # Whenever you pass an id, you can either pass the id itself,
56
- # or an object that response to :id
57
56
  update_params = {
58
57
  user1.id => user1_params,
59
58
  user2.id => user2_params,
@@ -94,10 +93,63 @@ CreateInvoiceForCustomer
94
93
 
95
94
  Where 'For' is a string literal and **ParentRecord** is the parent model classname, CamelCased.
96
95
 
96
+ ## Parameters
97
+
98
+ **Create**
99
+
100
+ `Create` classes take either a parameters hash or an array of parameters hashes.
101
+
102
+ Examples:
103
+ ```ruby
104
+ Tradesman::CreateUser.go(params)
105
+ Tradesman::CreateUser.go([params1, params2, params3])
106
+ ```
107
+
108
+ **CreateForParent**
109
+
110
+ `CreateForParent` classes take a parent and either a parameters hash or an array of parameters hashes.
111
+ The parent can either be an :id or an object that responds to #id.
112
+
113
+ Examples:
114
+ ```ruby
115
+ Tradesman::CreateInvoiceForCustomer.go(customer, invoice_params)
116
+ Tradesman::CreateInvoiceForCustomer.go(123, [invoice1, invoice2, invoice3])
117
+ ```
118
+
119
+ **Update**
120
+
121
+ `Update` classes take a record or array of records, and a paramter hash or array of parameter hashes.
122
+
123
+ Examples:
124
+ ```ruby
125
+ # Update a single record
126
+ Tradesman::UpdateUser.go(user, update_params)
127
+
128
+ # Update multiple records with the same parameters
129
+ Tradesman::UpdateUser.go([111, 222, 333], update_params)
130
+
131
+ # Update n records with n sets of parameters
132
+ update_params = {
133
+ user1 => user1_params,
134
+ user2 => user2_params,
135
+ user3 => user3_params
136
+ }
137
+ Tradesman::UpdateUser.go(update_params.keys, update_params.values)
138
+ ```
139
+
140
+ **Delete**
141
+
142
+ `Delete` classes take either a record of array of records.
143
+
144
+ Examples:
145
+ ```ruby
146
+ Tradesman::DeleteUser.go(123)
147
+ Tradesman::DeleteUser.go([user1, user2, user3])
148
+ ```
97
149
 
98
150
  ## Why is this necessary?
99
151
 
100
- At Onfido, we observed that many Create, Update and Delete actions we programmed were are simple and repeated (albeit with different records and parameter lists) in several locations. They can generally be broken in to the following steps:
152
+ Many Create, Update and Delete actions we program are simple and often repeated (albeit with different records and parameter lists) in several locations. They can generally be broken in to the following steps:
101
153
 
102
154
  - Query existing record by some group of parameters, but generally just by :id (Update and Delete only)
103
155
  - Return 404 if record does not exist
@@ -129,17 +181,19 @@ Tradesman is designed to handle the above and a few other common use-cases to re
129
181
 
130
182
  Tradesman version of the above:
131
183
  ```ruby
132
- Tradesman::UpdateUser.go(user_id, user_params) do
133
- success do |result|
134
- @user = result
135
- render 'user'
136
- end
184
+ def update
185
+ Tradesman::UpdateUser.go(params[:id], user_params) do
186
+ success do |result|
187
+ @user = result
188
+ render 'user'
189
+ end
137
190
 
138
- invalid do |error|
139
- render(text: error.message, status: 422)
140
- end
191
+ invalid do |error|
192
+ render(text: error.message, status: 422)
193
+ end
141
194
 
142
- failure { |result| render(text: 'false', status: 400) } # If you prefer one-liners
195
+ failure { |result| render(text: 'false', status: 400) } # If you prefer one-liners
196
+ end
143
197
  end
144
198
 
145
199
  private
@@ -149,20 +203,34 @@ def user_params
149
203
  end
150
204
  ```
151
205
 
152
- The Tradesman version says exactly what it does, is cruft free, and is much quicker to test (more on that later).
206
+ The Tradesman version is self-documenting, cruft-free, and designed for testing.
153
207
 
154
208
  ## Config
155
209
 
156
210
  **Define your adapter**
157
211
 
212
+ Tradesman sits on top of [Horza](https://github.com/onfido/horza/), and can use any of its adapters.
213
+
158
214
  ```ruby
159
215
  # config/initializers/tradesman.rb
160
216
  Tradesman.configure { |config| config.adapter = :active_record }
161
217
  ```
162
218
 
163
- **Development Mode and Model Namespaces**
219
+ **Development Mode**
164
220
 
165
- Rails' lazy-loading in the development environment makes a bit more configuration necessary, particularly if you have wrapped your models in namespaces.
221
+ Turn on development mode to help Tradesman cope with Rails' lazy-loading of classes.
222
+
223
+ ```ruby
224
+ # config/initializers/tradesman.rb
225
+ Tradesman.configure do |config|
226
+ config.adapter = :active_record
227
+ config.development_mode = Rails.env.development?
228
+ end
229
+ ```
230
+
231
+ **Model Namespaces**
232
+
233
+ Lazy loading becomes more complex if your models are namespaced.
166
234
 
167
235
  Consider:
168
236
  ```ruby
@@ -179,7 +247,7 @@ module MyOtherNamespace
179
247
  end
180
248
  ```
181
249
 
182
- In order to help Tradesman lazy load these models, you need to enable development mode and configure any namespaces:
250
+ In order to help Tradesman lazy load these models, you need to explicitly configure any namespaces:
183
251
 
184
252
  ```ruby
185
253
  # config/initializers/tradesman.rb
@@ -195,10 +263,6 @@ end
195
263
  Tradesman.reset
196
264
  ```
197
265
 
198
- ## Adapters
199
-
200
- Tradesman sits on top of [Horza](https://github.com/onfido/horza/), and can use any of its adapters.
201
-
202
266
  ## Edge Cases
203
267
 
204
268
  **Models ending with double 's'**
@@ -24,9 +24,10 @@ module Tradesman
24
24
  end
25
25
 
26
26
  def id_from_obj(obj)
27
- return obj.id if obj.respond_to? :id
28
- raise Tradesman::InvalidId.new('ID must be an integer') unless obj.is_a? Integer
29
- obj
27
+ id = obj.respond_to?(:id) ? obj.id : obj
28
+ Integer(id)
29
+ rescue ArgumentError
30
+ raise Tradesman::InvalidId.new('You must pass an object that responds to id or an integer')
30
31
  end
31
32
 
32
33
  def prepare_params(params)
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: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Blake Turner
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-06-19 00:00:00.000000000 Z
12
+ date: 2015-06-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: horza