lib/mongo_mapper/plugins/keys/key.rb in mongo_mapper-0.15.3 vs lib/mongo_mapper/plugins/keys/key.rb in mongo_mapper-0.15.4

- old
+ new

@@ -59,11 +59,11 @@ def get(value) # Special Case: Generate default _id on access value = default_value if @is_id and !value - value = type.from_mongo(value) + value = type ? type.from_mongo(value) : value if @typecast klass = typecast_class # Don't make this lookup on every call # typecast assumes array-ish object. value = value.map { |v| klass.from_mongo(v) } @@ -74,25 +74,28 @@ value end def set(value) # Avoid tap here so we don't have to create a block binding. - values = type.to_mongo(value) + value = type ? type.to_mongo(value) : value.to_mongo + if @typecast - values.map { |v| typecast_class.to_mongo(v) } + klass = typecast_class # Don't make this lookup on every call + value.map { |v| klass.to_mongo(v) } else - values + value end end def default_value return unless default? + if default.instance_of? Proc - type.to_mongo default.call + default.call else # Using Marshal is easiest way to get a copy of mutable objects # without getting an error on immutable objects - type.to_mongo Marshal.load(Marshal.dump(default)) + Marshal.load(Marshal.dump(default)) end end def valid_ruby_name? !!@name.match(/\A[a-z_][a-z0-9_]*\z/i)