Class: Mks::Common::MethodResponse
- Inherits:
-
Object
- Object
- Mks::Common::MethodResponse
- Defined in:
- lib/mks/common/methodresponse.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#message ⇒ Object
Returns the value of attribute message.
-
#success ⇒ Object
Returns the value of attribute success.
-
#total ⇒ Object
Returns the value of attribute total.
Class Method Summary collapse
- .error_response(data) ⇒ Object
- .failure_response(data) ⇒ Object
- .success_response(data, msg = nil) ⇒ Object
Instance Method Summary collapse
-
#initialize(success = nil, message = nil, data = nil, errors = [], total = nil) ⇒ MethodResponse
constructor
A new instance of MethodResponse.
Constructor Details
#initialize(success = nil, message = nil, data = nil, errors = [], total = nil) ⇒ MethodResponse
Returns a new instance of MethodResponse.
8 9 10 11 12 13 14 |
# File 'lib/mks/common/methodresponse.rb', line 8 def initialize(success = nil, = nil, data = nil, errors = [], total = nil) @success = success @message = @data = data @errors = errors @total = total end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
6 7 8 |
# File 'lib/mks/common/methodresponse.rb', line 6 def data @data end |
#errors ⇒ Object
Returns the value of attribute errors.
6 7 8 |
# File 'lib/mks/common/methodresponse.rb', line 6 def errors @errors end |
#message ⇒ Object
Returns the value of attribute message.
6 7 8 |
# File 'lib/mks/common/methodresponse.rb', line 6 def @message end |
#success ⇒ Object
Returns the value of attribute success.
6 7 8 |
# File 'lib/mks/common/methodresponse.rb', line 6 def success @success end |
#total ⇒ Object
Returns the value of attribute total.
6 7 8 |
# File 'lib/mks/common/methodresponse.rb', line 6 def total @total end |
Class Method Details
.error_response(data) ⇒ Object
27 28 29 30 |
# File 'lib/mks/common/methodresponse.rb', line 27 def self.error_response(data) errors = data.is_a?(String) ? [data] : data.errors return MethodResponse.new(false,nil, nil, errors, nil) end |
.failure_response(data) ⇒ Object
32 33 34 |
# File 'lib/mks/common/methodresponse.rb', line 32 def self.failure_response(data) data.errors.details end |
.success_response(data, msg = nil) ⇒ Object
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/mks/common/methodresponse.rb', line 16 def self.success_response(data, msg = nil) names = %w(ActiveRecord::Relation ActiveRecord::Associations::CollectionProxy) return MethodResponse.new(true, msg, nil, nil, nil) if data.nil? if data.kind_of?(Array) || names.include?(data.class.name) return MethodResponse.new(true, msg, [], nil, nil) if data.count.zero? return MethodResponse.new(true, msg, data[0].json(data), nil, data.count) else return MethodResponse.new(true, msg, data.json, nil, nil) end end |