Class: Cyrax::Response
- Inherits:
-
Object
- Object
- Cyrax::Response
- Defined in:
- lib/cyrax/response.rb
Instance Attribute Summary collapse
-
#accessor ⇒ Object
Returns the value of attribute accessor.
-
#assignments ⇒ Object
Returns the value of attribute assignments.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#message ⇒ Object
Returns the value of attribute message.
-
#options ⇒ Object
Returns the value of attribute options.
-
#resource_name ⇒ Object
Returns the value of attribute resource_name.
-
#result ⇒ Object
Returns the value of attribute result.
-
#status ⇒ Object
Returns the value of attribute status.
Instance Method Summary collapse
- #as_json(*args) ⇒ Object
- #error ⇒ Object
- #error_messages ⇒ Object
- #failure? ⇒ Boolean
- #has_error?(error) ⇒ Boolean
-
#initialize(resource_name, result, options = {}) ⇒ Response
constructor
A new instance of Response.
- #method_missing(method, *args, &block) ⇒ Object
- #notice ⇒ Object
- #success? ⇒ Boolean
- #with_errors(errors) ⇒ Object
- #with_message(message) ⇒ Object
Constructor Details
#initialize(resource_name, result, options = {}) ⇒ Response
Returns a new instance of Response.
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/cyrax/response.rb', line 5 def initialize(resource_name, result, = {}) @resource_name = resource_name @result = result @options = @message = nil @errors = {} @assignments = {} @status = nil @accessor = [:as] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
64 65 66 67 |
# File 'lib/cyrax/response.rb', line 64 def method_missing(method, *args, &block) super unless assignments.has_key?(method) assignments[method] end |
Instance Attribute Details
#accessor ⇒ Object
Returns the value of attribute accessor.
2 3 4 |
# File 'lib/cyrax/response.rb', line 2 def accessor @accessor end |
#assignments ⇒ Object
Returns the value of attribute assignments.
2 3 4 |
# File 'lib/cyrax/response.rb', line 2 def assignments @assignments end |
#errors ⇒ Object
Returns the value of attribute errors.
2 3 4 |
# File 'lib/cyrax/response.rb', line 2 def errors @errors end |
#message ⇒ Object
Returns the value of attribute message.
2 3 4 |
# File 'lib/cyrax/response.rb', line 2 def @message end |
#options ⇒ Object
Returns the value of attribute options.
2 3 4 |
# File 'lib/cyrax/response.rb', line 2 def @options end |
#resource_name ⇒ Object
Returns the value of attribute resource_name.
2 3 4 |
# File 'lib/cyrax/response.rb', line 2 def resource_name @resource_name end |
#result ⇒ Object
Returns the value of attribute result.
2 3 4 |
# File 'lib/cyrax/response.rb', line 2 def result @result end |
#status ⇒ Object
Returns the value of attribute status.
2 3 4 |
# File 'lib/cyrax/response.rb', line 2 def status @status end |
Instance Method Details
#as_json(*args) ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/cyrax/response.rb', line 54 def as_json(*args) if failure? {errors: @errors} elsif [:serializer] [:serializer].new(result, ).serialize else result.as_json end end |
#error ⇒ Object
44 45 46 47 48 |
# File 'lib/cyrax/response.rb', line 44 def error if failure? || I18n.t("cyrax.errors.default", default: "There was appeared some errors.") end end |
#error_messages ⇒ Object
38 39 40 41 42 |
# File 'lib/cyrax/response.rb', line 38 def errors.map do |key, value| "#{key}: #{value}" end end |
#failure? ⇒ Boolean
30 31 32 |
# File 'lib/cyrax/response.rb', line 30 def failure? !success? end |
#has_error?(error) ⇒ Boolean
50 51 52 |
# File 'lib/cyrax/response.rb', line 50 def has_error?(error) errors && errors.has_key?(error) end |
#notice ⇒ Object
34 35 36 |
# File 'lib/cyrax/response.rb', line 34 def notice if success? end |
#success? ⇒ Boolean
26 27 28 |
# File 'lib/cyrax/response.rb', line 26 def success? @errors.nil? || @errors.empty? end |
#with_errors(errors) ⇒ Object
16 17 18 19 |
# File 'lib/cyrax/response.rb', line 16 def with_errors(errors) @errors = errors self end |
#with_message(message) ⇒ Object
21 22 23 24 |
# File 'lib/cyrax/response.rb', line 21 def () @message = self end |