lib/dm-serializer/to_json.rb in dm-serializer-1.1.0.rc2 vs lib/dm-serializer/to_json.rb in dm-serializer-1.1.0.rc3
- old
+ new
@@ -1,11 +1,11 @@
require 'dm-serializer/common'
require 'json'
module DataMapper
- module Serialize
+ module Serializer
#
# Converts the resource into a hash of properties.
#
# @param [Hash] options
# Additional options.
@@ -29,17 +29,23 @@
Array(options[:methods]).each do |method|
next unless respond_to?(method)
result[method] = __send__(method)
end
- # Note: if you want to include a whole other model via relation, use :methods
- # comments.to_json(:relationships=>{:user=>{:include=>[:first_name],:methods=>[:age]}})
- # add relationships
- # TODO: This needs tests and also needs to be ported to #to_xml and #to_yaml
- (options[:relationships] || {}).each do |relationship_name, opts|
- next unless respond_to?(relationship_name)
- result[relationship_name] = __send__(relationship_name).to_json(opts.merge(:to_json => false))
+ # Note: if you want to include a whole other model via relation, use
+ # :methods:
+ #
+ # comments.to_json(:relationships=>{:user=>{:include=>[:first_name],:methods=>[:age]}})
+ #
+ # TODO: This needs tests and also needs to be ported to #to_xml and
+ # #to_yaml
+ if options[:relationships]
+ options[:relationships].each do |relationship_name, opts|
+ if respond_to?(relationship_name)
+ result[relationship_name] = __send__(relationship_name).to_json(opts.merge(:to_json => false))
+ end
+ end
end
result
end
@@ -61,11 +67,11 @@
end
module ValidationErrors
module ToJson
def to_json(*args)
- errors.to_hash.to_json
+ DataMapper::Ext::Array.to_hash(errors).to_json
end
end
end
end
@@ -85,14 +91,14 @@
collection
end
end
end
- if Serialize.dm_validations_loaded?
+ if Serializer.dm_validations_loaded?
module Validations
class ValidationErrors
- include DataMapper::Serialize::ValidationErrors::ToJson
+ include DataMapper::Serializer::ValidationErrors::ToJson
end
end
end