Class: DataMapper::Validate::ValidationErrors

Inherits:
Object
  • Object
show all
Defined in:
lib/restfulx/datamapper_foo.rb

Overview

By default DataMapper validation errors doesn’t have to_xml method. This is actually very useful when dealing with remote stateful clients such as Flex/AIR.

Instance Method Summary collapse

Instance Method Details

#to_jsonObject

Add to_json support for datamapper errors too



76
77
78
# File 'lib/restfulx/datamapper_foo.rb', line 76

def to_json
  @errors.to_json
end

#to_xmlObject

Add Flex-friendly to_xml implementation



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/restfulx/datamapper_foo.rb', line 58

def to_xml
  xml = DataMapper::Serialize::XMLSerializers::SERIALIZER
  doc ||= xml.new_document
  root = xml.root_node(doc, "errors")
  @errors.each_key do |attribute|
    @errors[attribute].each do |msg|
      next if msg.nil?
      if attribute == "base"
        xml.add_node(root, "error", nil, {"message" => msg})
      else
        xml.add_node(root, "error", nil, {"field" => attribute.to_s.camel_case.dcfirst, "message" => msg})
      end
    end
  end
  xml.output(doc)
end