lib/contentful/resource.rb in contentful-0.6.0 vs lib/contentful/resource.rb in contentful-0.7.0
- old
+ new
@@ -19,11 +19,11 @@
date: ->(v) { DateTime.parse(v) }
}
attr_reader :properties, :request, :client, :default_locale
- def initialize(object, request = nil, client = nil, nested_locale_fields = false, default_locale = Contentful::Client::DEFAULT_CONFIGURATION[:default_locale])
+ def initialize(object = nil, request = nil, client = nil, nested_locale_fields = false, default_locale = Contentful::Client::DEFAULT_CONFIGURATION[:default_locale])
self.class.update_coercions!
@nested_locale_fields = nested_locale_fields
@default_locale = default_locale
@properties = extract_from_object(object, :property,
@@ -102,12 +102,39 @@
def coerce_value_or_array(value, what = nil)
if value.nil?
nil
elsif value.is_a? ::Array
value.map { |v| coerce_or_create_class(v, what) }
+ elsif should_coerce_hash?(value)
+ ::Hash[value.map { |k, v|
+ to_coerce = v.is_a?(Hash) ? v : v.to_s
+ coercion = v.is_a?(Numeric) ? v : coerce_or_create_class(to_coerce, what)
+ [k.to_sym, coercion]
+ }]
else
coerce_or_create_class(value, what)
end
+ end
+
+ def should_coerce_hash?(value)
+ value.is_a?(::Hash) &&
+ !self.is_a?(Asset) &&
+ !self.is_a?(Field) &&
+ !is_location?(value) &&
+ !is_link?(value) &&
+ !is_image?(value)
+ end
+
+ def is_location?(value)
+ value.has_key?("lat") || value.has_key?("lon")
+ end
+
+ def is_link?(value)
+ value.has_key?("sys") && value["sys"]["type"] == "Link"
+ end
+
+ def is_image?(value)
+ value.has_key?("image")
end
def coerce_or_create_class(value, what)
case what
when Symbol