rest_my_case 1.9.0 → 1.9.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: b3806b834670a2c8eb80d7fd00ef3923afee77ed
4
- data.tar.gz: 94a6d4365d873c9a48bf677f5531b9b7e973d4f9
3
+ metadata.gz: 4eaca5ca9d55737b8d3aadc7d215d3bb13f2aabe
4
+ data.tar.gz: e4a6d32211c6c2033b341a1c53e4894b642a9985
5
5
  SHA512:
6
- metadata.gz: 068a0cfdca202c28835aaece3c99b8efd36a3ae38d4c01c04c9db9673a966f1de84743f6278b116fdcaa3a222afae945c0be3d56381936cfef25103bbf100b9c
7
- data.tar.gz: c580aa1241f136e325f7ed3d2db9e878a93c17761b902841dcb54628db413128086071066109d71c3b90ebaf32159d8e221315194765ced3ae86e1298a4141b4
6
+ metadata.gz: f34ecef601ece173ee6960d489ca331c767a23e255977f46c0b2362d745555781f9540a2bbb9032e9e1cd44ad3bd9c96c8c20741d047fefbf5f1644e05cdc790
7
+ data.tar.gz: b2a4efbdf1e1975f9b037a7a6e6162e3a6548c02533dca21474a6f6a53798e7399b78266765478eb814382b9482d7bdcba71abd846f94bc245553ad5ca1a9fde
data/lib/rest_my_case.rb CHANGED
@@ -6,10 +6,10 @@ require 'rest_my_case/errors/base'
6
6
  require 'rest_my_case/config/base'
7
7
  require 'rest_my_case/config/general'
8
8
  require 'rest_my_case/defense_attorney/base'
9
+ require 'rest_my_case/context/errors/base'
10
+ require 'rest_my_case/context/errors/status'
9
11
  require 'rest_my_case/context/base'
10
- require 'rest_my_case/context/status/matcher'
11
- require 'rest_my_case/context/status/status'
12
- require 'rest_my_case/context/status/base'
12
+ require 'rest_my_case/context/status'
13
13
  require 'rest_my_case/context/http_status'
14
14
  require 'rest_my_case/judge/base'
15
15
  require 'rest_my_case/trial/case'
@@ -79,7 +79,7 @@ module RestMyCase
79
79
  end
80
80
 
81
81
  def error(message = '')
82
- abort && context.errors[self.class.name].push(message)
82
+ abort && context.errors.add(self.class.name, message)
83
83
  end
84
84
 
85
85
  def error!(message = '')
@@ -7,12 +7,16 @@ module RestMyCase
7
7
 
8
8
  include ActiveModel::Serialization if defined?(ActiveModel)
9
9
 
10
+ def self.error_class
11
+ Errors::Base
12
+ end
13
+
10
14
  def to_hash
11
15
  Marshal.load Marshal.dump(attributes)
12
16
  end
13
17
 
14
18
  def errors
15
- @errors ||= Hash.new { |hash, key| hash[key] = [] }
19
+ @errors ||= self.class.error_class.new(self)
16
20
  end
17
21
 
18
22
  def valid?
@@ -0,0 +1,23 @@
1
+ module RestMyCase
2
+ module Context
3
+ module Errors
4
+
5
+ class Base < Hash
6
+
7
+ def initialize(context)
8
+ super()
9
+
10
+ @context = context
11
+ end
12
+
13
+ def add(error, message)
14
+ self[error] ||= []
15
+
16
+ self[error].push(message)
17
+ end
18
+
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,17 @@
1
+ module RestMyCase
2
+ module Context
3
+ module Errors
4
+
5
+ class Status < Base
6
+
7
+ def add(error, message)
8
+ super
9
+
10
+ @context.status.send("#{message}!")
11
+ end
12
+
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -1,7 +1,7 @@
1
1
  module RestMyCase
2
2
  module Context
3
3
 
4
- class HttpStatus < Status::Base
4
+ class HttpStatus < Status
5
5
 
6
6
  RAILS_HTTP_STATUS = {
7
7
  continue: 100,
@@ -56,7 +56,7 @@ module RestMyCase
56
56
  }
57
57
 
58
58
  def http_status
59
- RAILS_HTTP_STATUS[status.current.to_sym]
59
+ RAILS_HTTP_STATUS[status.to_sym]
60
60
  end
61
61
 
62
62
  end
@@ -0,0 +1,38 @@
1
+ module RestMyCase
2
+ module Context
3
+
4
+ class Status < Base
5
+
6
+ class StatusString < String
7
+
8
+ def method_missing(method, *args, &block)
9
+ last_char = method[-1]
10
+ method_name = method[0...-1]
11
+
12
+ if last_char == '!'
13
+ self.replace(method_name)
14
+ elsif last_char == '?'
15
+ self == method_name
16
+ else
17
+ super
18
+ end
19
+ end
20
+
21
+ end
22
+
23
+ def self.error_class
24
+ Errors::Status
25
+ end
26
+
27
+ def status
28
+ @status ||= StatusString.new 'ok'
29
+ end
30
+
31
+ def status=(val)
32
+ raise 'status is a reserved keyword which cannot be set'
33
+ end
34
+
35
+ end
36
+
37
+ end
38
+ end
@@ -4,14 +4,12 @@ module RestMyCase
4
4
 
5
5
  def self.trial_court
6
6
  @trial_court ||= Trial::Court.new \
7
- Judge::Base, DefenseAttorney::Base, Base, Context::Status::Base
7
+ Judge::Base, DefenseAttorney::Base, Base, Context::Status
8
8
  end
9
9
 
10
10
  context_reader :status
11
11
 
12
12
  def failure(status, message = nil)
13
- context.status.send("#{status}!")
14
-
15
13
  error(Helpers.blank?(message) ? "#{status}" : "#{status} - #{message}")
16
14
  end
17
15
 
@@ -1,5 +1,5 @@
1
1
  module RestMyCase
2
2
 
3
- VERSION = '1.9.0'
3
+ VERSION = '1.9.1'
4
4
 
5
5
  end
@@ -107,7 +107,7 @@ describe RestMyCase::Base do
107
107
  end
108
108
 
109
109
  it "context should contain only one error" do
110
- expect(@context.errors.keys.length).to be 1
110
+ expect(@context.errors).to match a_hash_including({"Perform::ValidateName"=>[""]})
111
111
  end
112
112
 
113
113
  it "context prove that only the correct method have ran" do
@@ -129,7 +129,7 @@ describe RestMyCase::Base do
129
129
  end
130
130
 
131
131
  it "context should contain only one error" do
132
- expect(@context.errors.keys.length).to be 1
132
+ expect(@context.errors).to match a_hash_including({"Perform::ValidateName"=>[""]})
133
133
  end
134
134
 
135
135
  it "context prove that only the correct method have ran" do
@@ -230,7 +230,7 @@ describe RestMyCase::Base do
230
230
  end
231
231
 
232
232
  it "context should contain only 2 errors" do
233
- expect(@context.errors.keys.length).to be 2
233
+ expect(@context.errors).to match a_hash_including({"Perform::ValidateName"=>[""], "Perform::ValidateBody"=>[""]})
234
234
  end
235
235
 
236
236
  it "context prove that only the correct method have ran" do
@@ -254,7 +254,7 @@ describe RestMyCase::Base do
254
254
  end
255
255
 
256
256
  it "context should contain only 2 errors" do
257
- expect(@context.errors.keys.length).to be 2
257
+ expect(@context.errors).to match a_hash_including({"Perform::ValidateName"=>[""], "Perform::ValidateBody"=>[""]})
258
258
  end
259
259
 
260
260
  it "context prove that only the correct method have ran" do
@@ -289,7 +289,7 @@ describe RestMyCase::Base do
289
289
  end
290
290
 
291
291
  it "context should contain only 4 errors" do
292
- expect(@context.errors.keys.length).to be 4
292
+ expect(@context.errors).to match a_hash_including({"Perform::BuildPost"=>[""], "Perform::ValidateName"=>[""], "Perform::ValidateBody"=>[""], "Perform::SavePost"=>[""]})
293
293
  end
294
294
 
295
295
  it "context prove that only the correct method have ran" do
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ describe RestMyCase::Context::Status do
4
+
5
+ describe '#status=' do
6
+ let(:context) { described_class.new }
7
+
8
+ it 'raises error' do
9
+ expect do
10
+ context.status = 'not_found'
11
+ end.to raise_error
12
+ end
13
+ end
14
+
15
+ describe '#status' do
16
+ before { @status = described_class.new.status }
17
+
18
+ describe '#method_missing' do
19
+ context "given that no method was previously called" do
20
+ it 'calling an unknown_method should raise an error' do
21
+ expect { @status.unknown_method }.to raise_error
22
+ end
23
+
24
+ it '#ok? should be true' do
25
+ expect(@status.ok?).to be true
26
+ end
27
+ end
28
+ end
29
+
30
+ describe '#to_s' do
31
+ context "given that .not_found! was called" do
32
+ before { @status.not_found! }
33
+
34
+ it "should return 'not_found'" do
35
+ expect(@status.to_s).to eq "not_found"
36
+ end
37
+ end
38
+ end
39
+
40
+ describe '#==' do
41
+ context "given that .unauthorized! was called" do
42
+ before { @status.unauthorized! }
43
+
44
+ it "comparing to 'unauthorized' should return true" do
45
+ expect(@status == 'unauthorized').to be true
46
+ end
47
+ end
48
+ end
49
+
50
+ end
51
+
52
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rest_my_case
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.9.0
4
+ version: 1.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - goncalvesjoao
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-14 00:00:00.000000000 Z
11
+ date: 2015-07-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry
@@ -125,10 +125,10 @@ files:
125
125
  - lib/rest_my_case/config/base.rb
126
126
  - lib/rest_my_case/config/general.rb
127
127
  - lib/rest_my_case/context/base.rb
128
+ - lib/rest_my_case/context/errors/base.rb
129
+ - lib/rest_my_case/context/errors/status.rb
128
130
  - lib/rest_my_case/context/http_status.rb
129
- - lib/rest_my_case/context/status/base.rb
130
- - lib/rest_my_case/context/status/matcher.rb
131
- - lib/rest_my_case/context/status/status.rb
131
+ - lib/rest_my_case/context/status.rb
132
132
  - lib/rest_my_case/defense_attorney/base.rb
133
133
  - lib/rest_my_case/errors/abort.rb
134
134
  - lib/rest_my_case/errors/base.rb
@@ -145,8 +145,7 @@ files:
145
145
  - spec/rest_my_case/accusation_attorneys/base_spec.rb
146
146
  - spec/rest_my_case/accusation_attorneys/each_spec.rb
147
147
  - spec/rest_my_case/base_spec.rb
148
- - spec/rest_my_case/context/status/base_spec.rb
149
- - spec/rest_my_case/context/status/status_spec.rb
148
+ - spec/rest_my_case/context/status_spec.rb
150
149
  - spec/rest_my_case/defense_attorney/base_spec.rb
151
150
  - spec/rest_my_case/http_status_spec.rb
152
151
  - spec/rest_my_case/status_spec.rb
@@ -187,8 +186,7 @@ test_files:
187
186
  - spec/rest_my_case/accusation_attorneys/base_spec.rb
188
187
  - spec/rest_my_case/accusation_attorneys/each_spec.rb
189
188
  - spec/rest_my_case/base_spec.rb
190
- - spec/rest_my_case/context/status/base_spec.rb
191
- - spec/rest_my_case/context/status/status_spec.rb
189
+ - spec/rest_my_case/context/status_spec.rb
192
190
  - spec/rest_my_case/defense_attorney/base_spec.rb
193
191
  - spec/rest_my_case/http_status_spec.rb
194
192
  - spec/rest_my_case/status_spec.rb
@@ -1,27 +0,0 @@
1
- module RestMyCase
2
- module Context
3
- module Status
4
-
5
- class Base < Context::Base
6
-
7
- def initialize(*args)
8
- super(*args)
9
-
10
- @status = ::RestMyCase::Context::Status::Status.new
11
- end
12
-
13
- def status
14
- @status
15
- end
16
-
17
- def status=(val)
18
- raise 'status is a reserved keyword which cannot be set'
19
- end
20
-
21
- alias :success? :valid?
22
-
23
- end
24
-
25
- end
26
- end
27
- end
@@ -1,30 +0,0 @@
1
- module RestMyCase
2
- module Context
3
- module Status
4
-
5
- class Matcher
6
-
7
- STATUS_SETTER_REGEX = /\A[a-zA-Z](.*)!\z/
8
- STATUS_QUESTION_REGEX = /\A[a-zA-Z](.*)\?\z/
9
-
10
- def initialize(text)
11
- @text = text
12
- end
13
-
14
- def match_as_setter?
15
- !!(@text =~ STATUS_SETTER_REGEX)
16
- end
17
-
18
- def match_as_question?
19
- !!(@text =~ STATUS_QUESTION_REGEX)
20
- end
21
-
22
- def status
23
- @text[0...-1]
24
- end
25
-
26
- end
27
-
28
- end
29
- end
30
- end
@@ -1,37 +0,0 @@
1
- module RestMyCase
2
- module Context
3
- module Status
4
-
5
- class Status
6
-
7
- attr_reader :current
8
-
9
- def initialize
10
- @current = 'ok'
11
- end
12
-
13
- def method_missing(method, *args, &block)
14
- matcher = ::RestMyCase::Context::Status::Matcher.new(method)
15
-
16
- if matcher.match_as_setter?
17
- @current = matcher.status
18
- elsif matcher.match_as_question?
19
- @current == matcher.status
20
- else
21
- super
22
- end
23
- end
24
-
25
- def to_s
26
- current.to_s
27
- end
28
-
29
- def ==(value)
30
- to_s == value
31
- end
32
-
33
- end
34
-
35
- end
36
- end
37
- end
@@ -1,16 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe RestMyCase::Context::Status::Base do
4
-
5
- describe '#status=' do
6
-
7
- let(:context) { described_class.new }
8
-
9
- it 'raises error' do
10
- expect do
11
- context.status = 'not_found'
12
- end.to raise_error
13
- end
14
-
15
- end
16
- end
@@ -1,45 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe RestMyCase::Context::Status::Status do
4
-
5
- describe '#method_missing' do
6
- context "given that no method was previously called" do
7
- before { @status = described_class.new }
8
-
9
- it 'calling an unknown_method should raise an error' do
10
- expect { @status.unknown_method }.to raise_error
11
- end
12
-
13
- it '#ok? should be true' do
14
- expect(@status.ok?).to be true
15
- end
16
- end
17
- end
18
-
19
- describe '#to_s' do
20
- context "given that .not_found! was called" do
21
- before do
22
- @status = described_class.new
23
- @status.not_found!
24
- end
25
-
26
- it "should return 'not_found'" do
27
- expect(@status.to_s).to eq "not_found"
28
- end
29
- end
30
- end
31
-
32
- describe '#==' do
33
- context "given that .unauthorized! was called" do
34
- before do
35
- @status = described_class.new
36
- @status.unauthorized!
37
- end
38
-
39
- it "comparing to 'unauthorized' should return true" do
40
- expect(@status == 'unauthorized').to be true
41
- end
42
- end
43
- end
44
-
45
- end