lib/pupa/models/base.rb in pupa-0.0.3 vs lib/pupa/models/base.rb in pupa-0.0.4
- old
+ new
@@ -150,11 +150,11 @@
#
# @raises [JSON::Schema::ValidationError] if the object is invalid
def validate!
if self.class.json_schema
# JSON::Validator#initialize_data runs fastest if given a hash.
- JSON::Validator.validate!(self.class.json_schema, to_h.deep_stringify_keys)
+ JSON::Validator.validate!(self.class.json_schema, stringify_keys(to_h))
end
end
# Returns the object as a hash.
#
@@ -180,8 +180,27 @@
a = to_h
b = other.to_h
a.delete(:_id)
b.delete(:_id)
a == b
+ end
+
+ private
+
+ def stringify_keys(object)
+ case object
+ when Hash
+ {}.tap do |hash|
+ object.each do |key,value|
+ hash[key.to_s] = stringify_keys(value)
+ end
+ end
+ when Array
+ object.map do |value|
+ stringify_keys(value)
+ end
+ else
+ object
+ end
end
end
end