Permalink
Browse files

Option :typecast for key works with Key#get

  • Loading branch information...
rainux committed Sep 8, 2010
1 parent 931dab7 commit 7d6f49bbe35e8d908f35f9d92704e549c365e8c2
Showing with 22 additions and 3 deletions.
  1. +7 −3 lib/mongo_mapper/plugins/keys/key.rb
  2. +15 −0 test/unit/test_key.rb
@@ -38,7 +38,11 @@ def get(value)
end
end
type.from_mongo(value)
if options[:typecast].present?
type.from_mongo(value).map! { |v| typecast_class.from_mongo(v) }
else
type.from_mongo(value)
end
end
def set(value)
@@ -48,12 +52,12 @@ def set(value)
end
end
end
private
def typecast_class
@typecast_class ||= options[:typecast].constantize
end
end
end
end
end
end
View
@@ -101,6 +101,21 @@ class KeyTest < Test::Unit::TestCase
end
end
context "for an array with :typecast option of Date" do
setup { @key = Key.new(:dates, Array, :typecast => 'Date') }
subject { @key }
should "cast each element correctly when get" do
dates = [Date.yesterday, Date.today, Date.tomorrow.to_s]
subject.get(dates).should == dates.map { |date| Date.from_mongo(date) }
end
should "cast each element correctly when set" do
dates = [Date.yesterday, Date.today, Date.tomorrow.to_s]
subject.set(dates).should == dates.map { |date| Date.to_mongo(date) }
end
end
context "for a set with :typecast option" do
setup { @key = Key.new(:user_ids, Set, :typecast => 'ObjectId') }
subject { @key }

0 comments on commit 7d6f49b

Please sign in to comment.