class Cyrax::Response

Attributes

accessor[RW]
assignments[RW]
errors[RW]
message[RW]
options[RW]
resource_name[RW]
result[RW]
status[RW]

Public Class Methods

new(resource_name, result, options = {}) click to toggle source
# File lib/cyrax/response.rb, line 5
def initialize(resource_name, result, options = {})
  @resource_name = resource_name
  @result = result
  @options = options
  @message = nil
  @errors = {}
  @assignments = {}
  @status = nil
  @accessor = options[:as]
end

Public Instance Methods

as_json(*args) click to toggle source
# File lib/cyrax/response.rb, line 54
def as_json(*args)
  if failure?
    {errors: @errors}
  elsif options[:serializer]
    options[:serializer].new(result, options).serialize
  else
    result.as_json
  end
end
error() click to toggle source
# File lib/cyrax/response.rb, line 44
def error
  if failure?
    message || I18n.t("cyrax.errors.default", default: "There was appeared some errors.")
  end
end
error_messages() click to toggle source
# File lib/cyrax/response.rb, line 38
def error_messages
  errors.map do |key, value|
    "#{key}: #{value}"
  end
end
failure?() click to toggle source
# File lib/cyrax/response.rb, line 30
def failure?
  !success?
end
has_error?(error) click to toggle source
# File lib/cyrax/response.rb, line 50
def has_error?(error)
  errors && errors.has_key?(error)
end
method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/cyrax/response.rb, line 64
def method_missing(method, *args, &block)
  super unless assignments.has_key?(method)
  assignments[method]
end
notice() click to toggle source
# File lib/cyrax/response.rb, line 34
def notice
  message if success?
end
success?() click to toggle source
# File lib/cyrax/response.rb, line 26
def success?
  @errors.nil? || @errors.empty?
end
with_errors(errors) click to toggle source
# File lib/cyrax/response.rb, line 16
def with_errors(errors)
  @errors = errors
  self
end
with_message(message) click to toggle source
# File lib/cyrax/response.rb, line 21
def with_message(message)
  @message = message
  self
end