class ServiceObject::Errors
Provides a customized Array
to contain errors that happen in service layer. Also provides a utility APIs to handle errors well in controllers. (All array methods are available by delegation, too.)
errs = ServiceObject::Errors.new errs.add 'Something is wrong.' errs.add 'Another is wrong.' errs.messages => ['Something is wrong.','Another is wrong.'] errs.full_messages => ['Something is wrong.','Another is wrong.'] errs.to_xml => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<errors>\n <error>Something is wrong.</error>\n <error>Another is wrong.</error>\n</errors>\n" errs.empty? => false errs.clear => [] errs.empty? => true
Attributes
messages[R]
@return [Array<String>] Messages of the current errors
Public Class Methods
new()
click to toggle source
# File lib/service_object/errors.rb, line 29 def initialize @messages = [] end
Public Instance Methods
add(message)
click to toggle source
Adds a new error message to the current error messages @param message [String] New error message to add
# File lib/service_object/errors.rb, line 46 def add(message) @messages << message end
as_json()
click to toggle source
Generates duplication of the message @return [Array<String>]
# File lib/service_object/errors.rb, line 68 def as_json messages.dup end
full_messages()
click to toggle source
Returns all the current error messages @return [Array<String>] Messages of the current errors
# File lib/service_object/errors.rb, line 40 def full_messages messages end
to_xml(options={})
click to toggle source
Generates XML format errors
errs = ServiceObject::Errors.new errs.add 'Something is wrong.' errs.add 'Another is wrong.' errs.to_xml => <?xml version=\"1.0\" encoding=\"UTF-8\"?> <errors> <error>Something is wrong.</error> <error>Another is wrong.</error> </errors>
@return [String] XML format string
Calls superclass method
# File lib/service_object/errors.rb, line 62 def to_xml(options={}) super({ root: "errors", skip_types: true }.merge!(options)) end