exceptions-resource 0.0.2p04 → 0.0.2p07

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 659614974614117f357dd7f118d75987fd106fea
4
- data.tar.gz: 2ea7687c01583e3b694875255243c50cbd9728eb
3
+ metadata.gz: 565b64cc3de39009cc01639f072387b43952d8d6
4
+ data.tar.gz: 9dfa0a1cd9a56727b0960d2d36b113ec58f9b629
5
5
  SHA512:
6
- metadata.gz: ec5451c4c2096485bed3f0ed0977d930cb37d7854517429216a8f16f790ef5df5792a6bb7181ac86426ac7224939d49aec702f786b797ce47faf75069dfc6ca8
7
- data.tar.gz: 4c1fd6707cfcb9a7011aabcba412c99ed5d08d2d9e57dfdc523339e4371b1f80451f48abad21fad4d4fa5fd57db2252b47bd5879eaafd27ec0d3359287e02296
6
+ metadata.gz: 2c9d40712df39460304f7f754617a7d145970028c978e5a0b8f03a9b2e664d0524a44c7650d34d1afdf377104ff891f9eb5a2ae4ce0a2b336b7aa6ae18501763
7
+ data.tar.gz: 9fd2031d9e50661c069d61f723a082d6e73df1cc8f0eb203e7ad0b506ee77973d679713d27a27cf96148de23f42b4a8bfaece09278602ecd55c8fa75a829dd14
@@ -1,31 +1,29 @@
1
- module Exceptions
2
- class Base < StandardError
3
- attr_accessor :object, :type
1
+ class Exceptions::Base < StandardError
2
+ attr_accessor :object, :type
4
3
 
5
- # starts a new instance with an object
6
- # @param [Object] object
7
- def initialize object
8
- self.object = object
9
- end
10
-
11
- # standard error for Models
12
- # @param [ActiveRecord::Base] object
13
- # @return [Exceptions::Base]
14
- def self.build object
15
- exception = new object
16
- return exception
17
- end
4
+ # starts a new instance with an object
5
+ # @param [Object] object
6
+ def initialize object
7
+ self.object = object
8
+ end
18
9
 
19
- # return if is as simple error
20
- # @return [Boolean]
21
- def simple?
22
- self.class.name.demodulize.tableize.singularize == "simple"
23
- end
10
+ # standard error for Models
11
+ # @param [ActiveRecord::Base] object
12
+ # @return [Exceptions::Base]
13
+ def self.build object
14
+ exception = new object
15
+ return exception
16
+ end
24
17
 
25
- # return if is as model error
26
- # @return [Boolean]
27
- def model?
28
- self.class.name.demodulize.tableize.singularize == "model"
29
- end
18
+ # return if is as simple error
19
+ # @return [Boolean]
20
+ def simple?
21
+ self.class.name.demodulize.tableize.singularize == "simple"
30
22
  end
23
+
24
+ # return if is as model error
25
+ # @return [Boolean]
26
+ def model?
27
+ self.class.name.demodulize.tableize.singularize == "model"
28
+ end
31
29
  end
@@ -1,89 +1,85 @@
1
- require 'base'
2
-
3
- module Exceptions
4
- class Model < Exceptions::Base
5
- # for model errors this method build a hash with all necessary information
6
- # @return [String] json string
7
- def error
8
- self.is_nested? ? self.build_nested : self.build_normal
9
- end
1
+ class Exceptions::Model < Exceptions::Base
2
+ # for model errors this method build a hash with all necessary information
3
+ # @return [String] json string
4
+ def error
5
+ self.is_nested? ? self.build_nested : self.build_normal
6
+ end
10
7
 
11
- def build_nested
12
- {
13
- error: {
14
- model: self.nested_model.camelcase,
15
- field: "#{self.nested_model}[#{self.nested_attr}]",
16
- attribute: self.nested_attr,
17
- message: self.message,
18
- full_message: "#{self.nested_attr_human} #{self.message}"
19
- }
20
- }
21
- end
8
+ def build_nested
9
+ {
10
+ error: {
11
+ model: self.nested_model.camelcase,
12
+ field: "#{self.nested_model}[#{self.nested_attr}]",
13
+ attribute: self.nested_attr,
14
+ message: self.message,
15
+ full_message: "#{self.nested_attr_human} #{self.message}"
16
+ }
17
+ }
18
+ end
22
19
 
23
- def build_normal
24
- {
25
- error: {
26
- model: self.model.camelcase,
27
- field: "#{self.model}[#{self.attribute}]",
28
- attribute: self.attribute,
29
- message: self.message,
30
- full_message: "#{self.attribute_human} #{self.message}"
31
- }
32
- }
33
- end
20
+ def build_normal
21
+ {
22
+ error: {
23
+ model: self.model.camelcase,
24
+ field: "#{self.model}[#{self.attribute}]",
25
+ attribute: self.attribute,
26
+ message: self.message,
27
+ full_message: "#{self.attribute_human} #{self.message}"
28
+ }
29
+ }
30
+ end
34
31
 
35
- # return what model is
36
- # @return [String]
37
- def model
38
- self.object.class.name.demodulize.tableize.singularize.downcase
39
- end
32
+ # return what model is
33
+ # @return [String]
34
+ def model
35
+ self.object.class.name.demodulize.tableize.singularize.downcase
36
+ end
40
37
 
41
- def attribute
42
- self.object.errors.first[0]
43
- end
38
+ def attribute
39
+ self.object.errors.first[0]
40
+ end
44
41
 
45
- def model_human
46
- self.object.class.model_name.human.demodulize.tableize.singularize.downcase
47
- end
42
+ def model_human
43
+ self.object.class.model_name.human.demodulize.tableize.singularize.downcase
44
+ end
48
45
 
49
- def attribute_human
50
- self.object.class.human_attribute_name(self.object.errors.first[0])
51
- end
46
+ def attribute_human
47
+ self.object.class.human_attribute_name(self.object.errors.first[0])
48
+ end
52
49
 
53
- # return the error message
54
- # @return [String]
55
- def message
56
- "#{self.object.errors.first[1]}"
57
- end
50
+ # return the error message
51
+ # @return [String]
52
+ def message
53
+ "#{self.object.errors.first[1]}"
54
+ end
58
55
 
59
- def status
60
- 422
61
- end
56
+ def status
57
+ 422
58
+ end
62
59
 
63
- def is_nested?
64
- attribute = self.object.errors.first[0]
60
+ def is_nested?
61
+ attribute = self.object.errors.first[0]
65
62
 
66
- if self.object.errors.first[0].to_s.split(".").size > 1
67
- self.object.respond_to?(attribute) ? false : true
68
- else
69
- false
70
- end
63
+ if self.object.errors.first[0].to_s.split(".").size > 1
64
+ self.object.respond_to?(attribute) ? false : true
65
+ else
66
+ false
71
67
  end
68
+ end
72
69
 
73
- def nested_model
74
- self.object.errors.first[0].to_s.split(".").first.singularize.downcase
75
- end
70
+ def nested_model
71
+ self.object.errors.first[0].to_s.split(".").first.singularize.downcase
72
+ end
76
73
 
77
- def nested_model_human
78
- self.nested_model.capitalize.constantize.model_name.human
79
- end
74
+ def nested_model_human
75
+ self.nested_model.capitalize.constantize.model_name.human
76
+ end
80
77
 
81
- def nested_attr
82
- self.object.errors.first[0].to_s.split(".").last
83
- end
78
+ def nested_attr
79
+ self.object.errors.first[0].to_s.split(".").last
80
+ end
84
81
 
85
- def nested_attr_human
86
- self.nested_model.capitalize.constantize.human_attribute_name(self.nested_attr)
87
- end
82
+ def nested_attr_human
83
+ self.nested_model.capitalize.constantize.human_attribute_name(self.nested_attr)
88
84
  end
89
85
  end
@@ -1,28 +1,26 @@
1
- module Exceptions
2
- class Resource < Exceptions::Base
3
- # for standard errors this method build a hash
4
- # @return [String] json string
5
- def error
6
- {
7
- error: {
8
- model: self.object["model"],
9
- attribute: self.object["attribute"],
10
- field: self.object["field"],
11
- message: self.object["message"],
12
- full_message: "#{self.object["attribute"]} #{self.object["message"]}"
13
- }
14
- }
15
- end
1
+ class Exceptions::Resource < Exceptions::Base
2
+ # for standard errors this method build a hash
3
+ # @return [String] json string
4
+ def error
5
+ {
6
+ error: {
7
+ model: self.object["model"],
8
+ attribute: self.object["attribute"],
9
+ field: self.object["field"],
10
+ message: self.object["message"],
11
+ full_message: "#{self.object["attribute"]} #{self.object["message"]}"
12
+ }
13
+ }
14
+ end
16
15
 
17
- # return the error message
18
- # @return [String]
19
- def message
20
- self.error[:message]
21
- end
16
+ # return the error message
17
+ # @return [String]
18
+ def message
19
+ self.error[:message]
20
+ end
22
21
 
23
- # return the error status
24
- def status
25
- 406
26
- end
22
+ # return the error status
23
+ def status
24
+ 406
27
25
  end
28
26
  end
@@ -1,31 +1,29 @@
1
1
  # represents the simple errors
2
- module Exceptions
3
- class Simple < Exceptions::Base
4
- attr_accessor :status
2
+ class Exceptions::Simple < Exceptions::Base
3
+ attr_accessor :status
5
4
 
6
-
7
- # for standard errors this method build a hash
8
- # @return [String] json string
9
- def error
10
- {
11
- error: {
12
- message: self.object[:message],
13
- full_message: "#{self.object[:field]} #{self.object[:message]} ",
14
- field: self.object[:field]
15
- }
16
- }
17
- end
18
5
 
19
- # return the error message
20
- # @return [String]
21
- def message
22
- self.object[:message]
23
- end
6
+ # for standard errors this method build a hash
7
+ # @return [String] json string
8
+ def error
9
+ {
10
+ error: {
11
+ message: self.object[:message],
12
+ full_message: "#{self.object[:field]} #{self.object[:message]} ",
13
+ field: self.object[:field]
14
+ }
15
+ }
16
+ end
24
17
 
25
- # return the error status
26
- def status
27
- 406
28
- end
18
+ # return the error message
19
+ # @return [String]
20
+ def message
21
+ self.object[:message]
22
+ end
29
23
 
24
+ # return the error status
25
+ def status
26
+ 406
30
27
  end
28
+
31
29
  end
@@ -1,31 +1,27 @@
1
- require 'base'
2
-
3
1
  # represents the simple errors
4
- module Exceptions
5
- class UnauthorizedApplication < Exceptions::Base
6
- attr_accessor :status
2
+ class Exceptions::UnauthorizedApplication < Exceptions::Base
3
+ attr_accessor :status
7
4
 
8
-
9
- # for standard errors this method build a hash
10
- # @return [String] json string
11
- def error
12
- {
13
- error: {
14
- message: self.object[:message]
15
- }
16
- }
17
- end
18
5
 
19
- # return the error message
20
- # @return [String]
21
- def message
22
- self.object[:message]
23
- end
6
+ # for standard errors this method build a hash
7
+ # @return [String] json string
8
+ def error
9
+ {
10
+ error: {
11
+ message: self.object[:message]
12
+ }
13
+ }
14
+ end
24
15
 
25
- # return the error status
26
- def status
27
- 401
28
- end
16
+ # return the error message
17
+ # @return [String]
18
+ def message
19
+ self.object[:message]
20
+ end
29
21
 
22
+ # return the error status
23
+ def status
24
+ 401
30
25
  end
26
+
31
27
  end
@@ -1,3 +1,3 @@
1
1
  module Exceptions
2
- VERSION = "0.0.2p04"
2
+ VERSION = "0.0.2p07"
3
3
  end
data/lib/exceptions.rb CHANGED
@@ -1,4 +1,206 @@
1
1
  module Exceptions
2
+ class Base < StandardError
3
+ attr_accessor :object, :type
4
+
5
+ # starts a new instance with an object
6
+ # @param [Object] object
7
+ def initialize object
8
+ self.object = object
9
+ end
10
+
11
+ # standard error for Models
12
+ # @param [ActiveRecord::Base] object
13
+ # @return [Exceptions::Base]
14
+ def self.build object
15
+ exception = new object
16
+ return exception
17
+ end
18
+
19
+ # return if is as simple error
20
+ # @return [Boolean]
21
+ def simple?
22
+ self.class.name.demodulize.tableize.singularize == "simple"
23
+ end
24
+
25
+ # return if is as model error
26
+ # @return [Boolean]
27
+ def model?
28
+ self.class.name.demodulize.tableize.singularize == "model"
29
+ end
30
+ end
31
+
32
+ class Model < Base
33
+ # for model errors this method build a hash with all necessary information
34
+ # @return [String] json string
35
+ def error
36
+ self.is_nested? ? self.build_nested : self.build_normal
37
+ end
38
+
39
+ def build_nested
40
+ {
41
+ error: {
42
+ model: self.nested_model.camelcase,
43
+ field: "#{self.nested_model}[#{self.nested_attr}]",
44
+ attribute: self.nested_attr,
45
+ message: self.message,
46
+ full_message: "#{self.nested_attr_human} #{self.message}"
47
+ }
48
+ }
49
+ end
50
+
51
+ def build_normal
52
+ {
53
+ error: {
54
+ model: self.model.camelcase,
55
+ field: "#{self.model}[#{self.attribute}]",
56
+ attribute: self.attribute,
57
+ message: self.message,
58
+ full_message: "#{self.attribute_human} #{self.message}"
59
+ }
60
+ }
61
+ end
62
+
63
+ # return what model is
64
+ # @return [String]
65
+ def model
66
+ self.object.class.name.demodulize.tableize.singularize.downcase
67
+ end
68
+
69
+ def attribute
70
+ self.object.errors.first[0]
71
+ end
72
+
73
+ def model_human
74
+ self.object.class.model_name.human.demodulize.tableize.singularize.downcase
75
+ end
76
+
77
+ def attribute_human
78
+ self.object.class.human_attribute_name(self.object.errors.first[0])
79
+ end
80
+
81
+ # return the error message
82
+ # @return [String]
83
+ def message
84
+ "#{self.object.errors.first[1]}"
85
+ end
86
+
87
+ def status
88
+ 422
89
+ end
90
+
91
+ def is_nested?
92
+ attribute = self.object.errors.first[0]
93
+
94
+ if self.object.errors.first[0].to_s.split(".").size > 1
95
+ self.object.respond_to?(attribute) ? false : true
96
+ else
97
+ false
98
+ end
99
+ end
100
+
101
+ def nested_model
102
+ self.object.errors.first[0].to_s.split(".").first.singularize.downcase
103
+ end
104
+
105
+ def nested_model_human
106
+ self.nested_model.capitalize.constantize.model_name.human
107
+ end
108
+
109
+ def nested_attr
110
+ self.object.errors.first[0].to_s.split(".").last
111
+ end
112
+
113
+ def nested_attr_human
114
+ self.nested_model.capitalize.constantize.human_attribute_name(self.nested_attr)
115
+ end
116
+ end
117
+
118
+ class Resource < Base
119
+ # for standard errors this method build a hash
120
+ # @return [String] json string
121
+ def error
122
+ {
123
+ error: {
124
+ model: self.object["model"],
125
+ attribute: self.object["attribute"],
126
+ field: self.object["field"],
127
+ message: self.object["message"],
128
+ full_message: "#{self.object["attribute"]} #{self.object["message"]}"
129
+ }
130
+ }
131
+ end
132
+
133
+ # return the error message
134
+ # @return [String]
135
+ def message
136
+ self.error[:message]
137
+ end
138
+
139
+ # return the error status
140
+ def status
141
+ 406
142
+ end
143
+ end
144
+
145
+ class Simple < Base
146
+ attr_accessor :status
147
+
148
+
149
+ # for standard errors this method build a hash
150
+ # @return [String] json string
151
+ def error
152
+ {
153
+ error: {
154
+ message: self.object[:message],
155
+ full_message: "#{self.object[:field]} #{self.object[:message]} ",
156
+ field: self.object[:field]
157
+ }
158
+ }
159
+ end
160
+
161
+ # return the error message
162
+ # @return [String]
163
+ def message
164
+ self.object[:message]
165
+ end
166
+
167
+ # return the error status
168
+ def status
169
+ 406
170
+ end
171
+ end
172
+
173
+ # represents the simple errors
174
+ class UnauthorizedApplication < Base
175
+ attr_accessor :status
176
+
177
+
178
+ # for standard errors this method build a hash
179
+ # @return [String] json string
180
+ def error
181
+ {
182
+ error: {
183
+ message: self.object[:message]
184
+ }
185
+ }
186
+ end
187
+
188
+ # return the error message
189
+ # @return [String]
190
+ def message
191
+ self.object[:message]
192
+ end
193
+
194
+ # return the error status
195
+ def status
196
+ 401
197
+ end
198
+
199
+ end
200
+
2
201
  end
3
202
 
4
- Dir["#{File.expand_path('../exceptions', __FILE__)}/*.rb"].each { |f| require f }
203
+ # require 'exceptions/model'
204
+ # require 'exceptions/resource'
205
+ # require 'exceptions/simple'
206
+ # require 'exceptions/unauthorized_application'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: exceptions-resource
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2p04
4
+ version: 0.0.2p07
5
5
  platform: ruby
6
6
  authors:
7
7
  - Douglas Rossignolli