lib/mongo_mapper/plugins/keys/key.rb in mongo_mapper-0.7.6 vs lib/mongo_mapper/plugins/keys/key.rb in mongo_mapper-0.8.0
- old
+ new
@@ -1,24 +1,26 @@
+# encoding: UTF-8
module MongoMapper
module Plugins
module Keys
class Key
attr_accessor :name, :type, :options, :default_value
def initialize(*args)
options = args.extract_options!
@name, @type = args.shift.to_s, args.shift
self.options = (options || {}).symbolize_keys
- self.default_value = self.options.delete(:default)
+ self.default_value = self.options[:default]
end
def ==(other)
@name == other.name && @type == other.type
end
def embeddable?
- type.respond_to?(:embeddable?) && type.embeddable? ? true : false
+ return false unless type.respond_to?(:embeddable?)
+ type.embeddable?
end
def number?
[Integer, Float].include?(type)
end
@@ -34,11 +36,20 @@
type.from_mongo(value)
end
def set(value)
- type.to_mongo(value)
+ type.to_mongo(value).tap do |values|
+ if options[:typecast].present?
+ values.map! { |v| typecast_class.to_mongo(v) }
+ end
+ end
end
+
+ private
+ def typecast_class
+ @typecast_class ||= options[:typecast].constantize
+ end
end
end
end
end
\ No newline at end of file