Class: DataMapper::Validate::ValidationErrors

Inherits:
Object
  • Object
show all
Defined in:
lib/restfulx/rx_datamapper.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



74
75
76
# File 'lib/restfulx/rx_datamapper.rb', line 74

def to_json
  @errors.to_json
end

#to_xmlObject

Add Flex-friendly to_xml implementation



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

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