tzu 0.0.1.0 → 0.0.2.0

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: b809317c8b5ff13fcb4784360d28703691ea3d9d
4
- data.tar.gz: 141562074c39e1d23fc945f2d6d7bf825eb37c6c
3
+ metadata.gz: e476045f4ece3c811b3fdaedad37f1a035f12e13
4
+ data.tar.gz: b699bc51b73743dcaa26a42394fe8a086018dd7b
5
5
  SHA512:
6
- metadata.gz: 1b231785af2e5ca4fab9e1a8590676875f87796592050391f9e32a2358e70878e31cc85c42b5c383c2fb484ab00afc7abb549c15a56a5f0b29fa0dccd3f96878
7
- data.tar.gz: bff733337e18acb528bd37978f3034f67e788225b23b487c878f60d00bca8c0ef95b07c362ec5166024b633b102f968d1c0418451b7ab13a4accee6911d4ff93
6
+ metadata.gz: 2838ccd7ae2ed0cab8e6827ddeaa765520a55a49f42865f7739d96142e232dfbe96e4ea0d6ca20996f3a39fc6258124d90e39fde3adc0cdde769b341d2e0efdf
7
+ data.tar.gz: 337506517ab4d5e0dc25b6a1e7a6fab72aeb71dc47129d8adfbd304a7e579bb4416680440fe7b6109624ea0735ce3ce8381d80461773230bf49dc4d97426c18d
data/lib/tzu/failure.rb CHANGED
@@ -1,10 +1,24 @@
1
1
  module Tzu
2
2
  class Failure < StandardError
3
- attr_reader :errors, :type
3
+ attr_reader :type
4
4
 
5
5
  def initialize(type = nil, errors = nil)
6
- @errors, @type = errors, type
6
+ @type = type
7
+ @raw_errors = errors
7
8
  end
8
9
 
10
+ def errors
11
+ string_error? ? { errors: @raw_errors } : @raw_errors
12
+ end
13
+
14
+ def message
15
+ string_error? ? @raw_errors : @raw_errors.to_s
16
+ end
17
+
18
+ private
19
+
20
+ def string_error?
21
+ @raw_errors.is_a?(String)
22
+ end
9
23
  end
10
24
  end
@@ -32,7 +32,7 @@ module Tzu
32
32
  end
33
33
 
34
34
  def invalid!(obj)
35
- output = [:errors, :messages].reduce(obj) do |result, m|
35
+ output = [:errors, :messages, :message].reduce(obj) do |result, m|
36
36
  result = result.respond_to?(m) ? result.send(m) : result
37
37
  end
38
38
 
data/spec/tzu_spec.rb CHANGED
@@ -1,354 +1,39 @@
1
- # require 'spec_helper'
2
- #
3
- # if !defined?(ActiveRecord::Base)
4
- # puts "** require 'active_record' to run the specs in #{__FILE__}"
5
- # else
6
- # ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
7
- #
8
- # ActiveRecord::Migration.suppress_messages do
9
- # ActiveRecord::Schema.define(:version => 0) do
10
- # create_table(:employers, force: true) {|t| t.string :name }
11
- # create_table(:users, force: true) {|t| t.string :first_name; t.string :last_name; t.references :employer; }
12
- # create_table(:sports_cars, force: true) {|t| t.string :make; t.references :employer; }
13
- # end
14
- # end
15
- #
16
- # module GetSpec
17
- # class Employer < ActiveRecord::Base
18
- # has_many :users
19
- # has_many :sports_cars
20
- # end
21
- #
22
- # class User < ActiveRecord::Base
23
- # belongs_to :employer
24
- # end
25
- #
26
- # class SportsCar < ActiveRecord::Base
27
- # belongs_to :employer
28
- # end
29
- # end
30
- # end
31
- #
32
- # describe Get do
33
- # let(:last_name) { 'Turner' }
34
- # let(:adapter) { :active_record }
35
- #
36
- # # Preserve system config for other tests
37
- # before(:all) { @system_config = Get.configuration }
38
- # after(:all) { Get.configuration = @system_config }
39
- #
40
- # # Reset base config with each iteration
41
- # before { Get.configure { |config| config.set_adapter(adapter) } }
42
- # after do
43
- # GetSpec::User.delete_all
44
- # GetSpec::Employer.delete_all
45
- # Get.reset
46
- # end
47
- #
48
- # class MyCustomEntity < Horza::Entities::Collection
49
- # def east_london_length
50
- # "#{length}, bruv"
51
- # end
52
- # end
53
- #
54
- # describe '#configure' do
55
- # context '#register_entity' do
56
- # let(:user_count) { 3 }
57
- #
58
- # before do
59
- # Get.configure { |config| config.register_entity(:users_by_last_name, MyCustomEntity) }
60
- # user_count.times { GetSpec::User.create(last_name: last_name) }
61
- # end
62
- # after { Get.reset }
63
- #
64
- # it 'gets registers entity' do
65
- # expect(Get.configuration.entity_for(:users_by_last_name)).to eq MyCustomEntity
66
- # end
67
- #
68
- # it 'returns specified entity type after querying db' do
69
- # result = Get::UsersByLastName.run(last_name)
70
- # expect(result.is_a? MyCustomEntity).to be true
71
- # expect(result.east_london_length).to eq "#{user_count}, bruv"
72
- # end
73
- # end
74
- # end
75
- #
76
- # context '#entity_for' do
77
- # context 'when entity has been registered' do
78
- # before do
79
- # Get.configure do |config|
80
- # config.set_adapter(adapter)
81
- # config.register_entity(:users_by_last_name, MyCustomEntity)
82
- # end
83
- # end
84
- # after { Get.reset }
85
- #
86
- # it 'registers entity' do
87
- # expect(Get.entity_for(:users_by_last_name)).to eq MyCustomEntity
88
- # end
89
- # end
90
- #
91
- # context 'when entity has not been registered' do
92
- # it 'returns nil' do
93
- # expect(Get.entity_for(:users_by_last_name)).to be nil
94
- # end
95
- # end
96
- # end
97
- #
98
- # context '#adapter' do
99
- # context 'when the adapter is set' do
100
- # it 'returns the correct adapter class' do
101
- # expect(Get.adapter).to eq Horza::Adapters::ActiveRecord
102
- # end
103
- # end
104
- #
105
- # context 'when the adapter is not set' do
106
- # before { Get.reset }
107
- # after { Get.reset }
108
- #
109
- # it 'throws error' do
110
- # expect { Get.adapter }.to raise_error(Get::Errors::Base)
111
- # end
112
- # end
113
- # end
114
- #
115
- # context '#reset' do
116
- # before do
117
- # Get.configure do |config|
118
- # config.set_adapter('my_adapter')
119
- # config.register_entity(:users_by_last_name, MyCustomEntity)
120
- # end
121
- # Get.reset
122
- # end
123
- # it 'resets the config' do
124
- # expect(Get.configuration.adapter).to be nil
125
- # expect(Get.entity_for(:users_by_last_name)).to be nil
126
- # end
127
- # end
128
- #
129
- # context '#run!' do
130
- # context 'singular form' do
131
- # context 'when the record exists' do
132
- # let!(:user) { GetSpec::User.create(last_name: last_name) }
133
- #
134
- # context 'field in class name' do
135
- # it 'gets the records based on By[KEY]' do
136
- # result = Get::UserById.run!(user.id)
137
- # expect(result.to_h).to eq user.attributes
138
- # end
139
- #
140
- # it 'returns a dynamically generated response entity' do
141
- # expect(Get::UserById.run!(user.id).is_a?(Horza::Entities::Single)).to be true
142
- # end
143
- # end
144
- #
145
- # context 'field in parameters' do
146
- # it 'gets the records based on parameters' do
147
- # result = Get::UserBy.run!(last_name: last_name)
148
- # expect(result.to_h).to eq user.attributes
149
- # end
150
- #
151
- # it 'returns a dynamically generated response entity' do
152
- # expect(Get::UserBy.run!(last_name: last_name).is_a?(Horza::Entities::Single)).to be true
153
- # end
154
- # end
155
- # end
156
- #
157
- # context 'when the record does not exist' do
158
- # it 'returns nil' do
159
- # expect { Get::UserById.run!(999) }.to raise_error Get::Errors::Base
160
- # end
161
- # end
162
- # end
163
- #
164
- # context 'ancestry' do
165
- # context 'valid ancestry with no saved parent' do
166
- # let(:user2) { GetSpec::User.create }
167
- # it 'returns nil' do
168
- # expect { Get::EmployerFromUser.run!(user2) }.to raise_error Get::Errors::RecordNotFound
169
- # end
170
- # end
171
- # end
172
- # end
173
- #
174
- # context '#run' do
175
- # context 'singular form' do
176
- # context 'when the record exists' do
177
- # let!(:user) { GetSpec::User.create(last_name: last_name) }
178
- #
179
- # context 'field in class name' do
180
- # it 'gets the records based on By[KEY]' do
181
- # result = Get::UserById.run(user.id)
182
- # expect(result.to_h).to eq user.attributes
183
- # end
184
- #
185
- # it 'returns a dynamically generated response entity' do
186
- # expect(Get::UserById.run(user.id).is_a?(Horza::Entities::Single)).to be true
187
- # end
188
- # end
189
- #
190
- # context 'field in parameters' do
191
- # it 'gets the records based on parameters' do
192
- # result = Get::UserBy.run(last_name: last_name)
193
- # expect(result.to_h).to eq user.attributes
194
- # end
195
- #
196
- # it 'returns a dynamically generated response entity' do
197
- # expect(Get::UserBy.run(last_name: last_name).is_a?(Horza::Entities::Single)).to be true
198
- # end
199
- # end
200
- # end
201
- #
202
- # context 'when the record does not exist' do
203
- # it 'returns nil' do
204
- # expect(Get::UserById.run(999)).to eq nil
205
- # end
206
- # end
207
- # end
208
- #
209
- # context 'plural form' do
210
- # let(:last_name) { 'Turner' }
211
- # let(:match_count) { 3 }
212
- # let(:miss_count) { 2 }
213
- #
214
- # context 'when records exist' do
215
- # before do
216
- # match_count.times { GetSpec::User.create(last_name: last_name) }
217
- # miss_count.times { GetSpec::User.create }
218
- # end
219
- #
220
- # context 'field in class name' do
221
- # it 'gets the records based on By[KEY]' do
222
- # result = Get::UsersByLastName.run(last_name)
223
- # expect(result.length).to eq match_count
224
- # end
225
- #
226
- # it 'returns a dynamically generated response entity' do
227
- # expect(Get::UsersByLastName.run(last_name).is_a?(Horza::Entities::Collection)).to be true
228
- # end
229
- # end
230
- #
231
- # context 'field in parameters' do
232
- # it 'gets the records based on parameters' do
233
- # result = Get::UsersBy.run(last_name: last_name)
234
- # expect(result.length).to eq match_count
235
- # end
236
- #
237
- # it 'returns a dynamically generated response entity' do
238
- # expect(Get::UsersBy.run(last_name: last_name).is_a?(Horza::Entities::Collection)).to be true
239
- # end
240
- # end
241
- # end
242
- #
243
- # context 'when no records exist' do
244
- # it 'returns empty collection' do
245
- # expect(Get::UsersBy.run(last_name: last_name).empty?).to be true
246
- # end
247
- # end
248
- # end
249
- #
250
- # context 'ancestry' do
251
- # context 'direct relation' do
252
- # let(:employer) { GetSpec::Employer.create }
253
- # let!(:user1) { GetSpec::User.create(employer: employer) }
254
- # let!(:user2) { GetSpec::User.create(employer: employer) }
255
- #
256
- # context 'ParentFromChild' do
257
- # it 'returns parent' do
258
- # expect(Get::EmployerFromUser.run(user1).to_h).to eq employer.attributes
259
- # end
260
- # end
261
- #
262
- # context 'ChildrenFromParent' do
263
- # it 'returns children' do
264
- # result = Get::UsersFromEmployer.run(employer)
265
- # expect(result.first.to_h).to eq user1.attributes
266
- # expect(result.last.to_h).to eq user2.attributes
267
- # end
268
- # end
269
- #
270
- # context 'invalid ancestry' do
271
- # it 'throws error' do
272
- # expect { Get::UserFromEmployer.run(employer) }.to raise_error Get::Errors::InvalidAncestry
273
- # end
274
- # end
275
- #
276
- # context 'valid ancestry with no saved childred' do
277
- # let(:employer2) { GetSpec::Employer.create }
278
- # it 'returns empty collection error' do
279
- # expect(Get::UsersFromEmployer.run(employer2).empty?).to be true
280
- # end
281
- # end
282
- #
283
- # context 'valid ancestry with no saved parent' do
284
- # let(:user2) { GetSpec::User.create }
285
- # it 'returns nil' do
286
- # expect(Get::EmployerFromUser.run(user2)).to be nil
287
- # end
288
- # end
289
- # end
290
- #
291
- # context 'using via' do
292
- # let(:employer) { GetSpec::Employer.create }
293
- # let(:user) { GetSpec::User.create(employer: employer) }
294
- # let(:sportscar) { GetSpec::SportsCar.create(employer: employer) }
295
- #
296
- # before do
297
- # employer.sports_cars << sportscar
298
- # end
299
- #
300
- # it 'returns the correct ancestor (single via symbol)' do
301
- # result = Get::SportsCarsFromUser.run(user, via: :employer)
302
- # expect(result.first.to_h).to eq sportscar.attributes
303
- # end
304
- #
305
- # it 'returns the correct ancestor (array of via symbols)' do
306
- # result = Get::SportsCarsFromUser.run(user, via: [:employer])
307
- # expect(result.first.to_h).to eq sportscar.attributes
308
- # end
309
- # end
310
- # end
311
- # end
312
- # end
313
- #
314
- # describe Get::Builders::AncestryBuilder do
315
- # let(:name) { 'UserFromEmployer' }
316
- #
317
- # before { Get.configure { |config| config.set_adapter(:active_record) } }
318
- # after { Get.reset }
319
- #
320
- # subject { Get::Builders::AncestryBuilder.new(name) }
321
- #
322
- # describe '#class' do
323
- # it 'builds a class that inherits from Get::Db' do
324
- # expect(subject.class.superclass).to eq Get::Db
325
- # end
326
- #
327
- # it 'correctly assigns class-level variables' do
328
- # [:entity, :query_key, :collection, :store, :result_key].each do |class_var|
329
- # expect(subject.class.respond_to? class_var).to be true
330
- # end
331
- # end
332
- # end
333
- # end
334
- #
335
- # describe Get::Builders::QueryBuilder do
336
- # let(:name) { 'UserFromEmployer' }
337
- #
338
- # before { Get.configure { |config| config.set_adapter(:active_record) } }
339
- # after { Get.reset }
340
- #
341
- # subject { Get::Builders::QueryBuilder.new(name) }
342
- #
343
- # describe '#class' do
344
- # it 'builds a class that inherits from Get::Db' do
345
- # expect(subject.class.superclass).to eq Get::Db
346
- # end
347
- #
348
- # it 'correctly assigns class-level variables' do
349
- # [:entity, :query_key, :collection, :store, :field].each do |class_var|
350
- # expect(subject.class.respond_to? class_var).to be true
351
- # end
352
- # end
353
- # end
354
- # end
1
+ require 'spec_helper'
2
+
3
+ describe Tzu do
4
+
5
+ context 'command fails' do
6
+ subject do
7
+ Class.new do
8
+ include Tzu
9
+
10
+ def call(params)
11
+ fail! :something
12
+ end
13
+ end
14
+ end
15
+
16
+ it 'returns failure' do
17
+ result = subject.run(nil)
18
+ expect(result).to have_attributes(success: false, type: :something)
19
+ end
20
+ end
21
+
22
+ context 'command succeeds' do
23
+ subject do
24
+ Class.new do
25
+ include Tzu
26
+
27
+ def call(params)
28
+ 1234
29
+ end
30
+ end
31
+ end
32
+
33
+ it 'returns result' do
34
+ result = subject.run(nil)
35
+ expect(result).to have_attributes(success: true, result: 1234)
36
+ end
37
+ end
38
+
39
+ end
@@ -41,4 +41,56 @@ describe Tzu::Validation do
41
41
  expect(result).to have_attributes(success: false, type: :validation)
42
42
  end
43
43
  end
44
- end
44
+
45
+ context 'error message is string' do
46
+ let(:str) { 'error_message' }
47
+ subject { Tzu::Invalid.new(str) }
48
+
49
+ context 'invoked directly' do
50
+ it 'has string as #message' do
51
+ expect(subject.message).to eq str
52
+ end
53
+
54
+ it '#errors converts sting to hash' do
55
+ expect(subject.errors).to eq(errors: str)
56
+ end
57
+ end
58
+
59
+ context 'rescued' do
60
+ subject do
61
+ Class.new do
62
+ include Tzu
63
+ include Tzu::Validation
64
+
65
+ def call(params)
66
+ raise StandardError.new(params)
67
+ rescue StandardError => e
68
+ invalid! e
69
+ end
70
+ end
71
+ end
72
+
73
+ describe '#run' do
74
+ it 'returns error hash as result' do
75
+ outcome = subject.run(str)
76
+ expect(outcome.result).to eq(errors: str)
77
+ end
78
+ end
79
+
80
+ describe '#run!' do
81
+ it 'has string as #message' do
82
+ expect { subject.run!(str) }.to raise_error Tzu::Invalid, str
83
+ end
84
+
85
+ it 'has string as #errors' do
86
+ begin
87
+ subject.run!(str)
88
+ expect(false).to be true # Should never reach this
89
+ rescue Tzu::Invalid => e
90
+ expect(e.errors).to eq(errors: str)
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tzu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.0
4
+ version: 0.0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Morgan Bruce
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-05-28 00:00:00.000000000 Z
12
+ date: 2015-07-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -95,7 +95,7 @@ dependencies:
95
95
  - - ">="
96
96
  - !ruby/object:Gem::Version
97
97
  version: '0'
98
- description: Tzu is a library for issuing commands in Ruby
98
+ description: Encapsulate your database queries with dynamically generated classes
99
99
  email: morgan@onfido.com
100
100
  executables: []
101
101
  extensions: []
@@ -112,7 +112,6 @@ files:
112
112
  - lib/tzu/outcome.rb
113
113
  - lib/tzu/validation.rb
114
114
  - lib/tzu/validation_result.rb
115
- - spec/command_spec.rb
116
115
  - spec/hooks_spec.rb
117
116
  - spec/organizer_spec.rb
118
117
  - spec/outcome_spec.rb
@@ -142,9 +141,9 @@ rubyforge_project:
142
141
  rubygems_version: 2.2.2
143
142
  signing_key:
144
143
  specification_version: 4
145
- summary: Standardise and encapsulate your application's actions
144
+ summary: Get is a library designed to encapsulate Rails database queries and prevent
145
+ query pollution in the view layer.
146
146
  test_files:
147
- - spec/command_spec.rb
148
147
  - spec/hooks_spec.rb
149
148
  - spec/organizer_spec.rb
150
149
  - spec/outcome_spec.rb
data/spec/command_spec.rb DELETED
@@ -1,39 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Tzu do
4
-
5
- context 'command fails' do
6
- subject do
7
- Class.new do
8
- include Tzu
9
-
10
- def call(params)
11
- fail! :something
12
- end
13
- end
14
- end
15
-
16
- it 'returns failure' do
17
- result = subject.run(nil)
18
- expect(result).to have_attributes(success: false, type: :something)
19
- end
20
- end
21
-
22
- context 'command succeeds' do
23
- subject do
24
- Class.new do
25
- include Tzu
26
-
27
- def call(params)
28
- 1234
29
- end
30
- end
31
- end
32
-
33
- it 'returns result' do
34
- result = subject.run(nil)
35
- expect(result).to have_attributes(success: true, result: 1234)
36
- end
37
- end
38
-
39
- end