Permalink
Browse files

flushing out the final array extension specs

  • Loading branch information...
1 parent 39265e1 commit 93a0113daa2231f84f77f5b33424c4e465cb52d8 @durran durran committed Apr 25, 2012
Showing with 177 additions and 0 deletions.
  1. +16 −0 lib/mongoid/extensions/array.rb
  2. +157 −0 spec/mongoid/extensions/array_spec.rb
  3. +4 −0 spec/mongoid/extensions/date_time_spec.rb
@@ -3,11 +3,27 @@ module Mongoid
module Extensions
module Array
+ # Evolve the array into an array of object ids.
+ #
+ # @example Evolve the array to object ids.
+ # [ id ].__evolve_object_id__
+ #
+ # @return [ Array<Moped::BSON::ObjectId> ] The converted array.
+ #
+ # @since 3.0.0
def __evolve_object_id__
map!(&:__evolve_object_id__).compact!
self
end
+ # Converts the array for storing as a time.
+ #
+ # @example Convert the array to a time.
+ # [ 2010, 1, 1 ].__mongoize_time__
+ #
+ # @return [ Time ] The time.
+ #
+ # @since 3.0.0
def __mongoize_time__
time = Mongoid::Config.use_activesupport_time_zone? ? (::Time.zone || ::Time) : ::Time
time.local(*self)
@@ -2,6 +2,105 @@
describe Mongoid::Extensions::Array do
+ describe "#__evolve_object_id__" do
+
+ context "when provided an array of strings" do
+
+ let(:object_id) do
+ BSON::ObjectId.new
+ end
+
+ let(:other) do
+ "blah"
+ end
+
+ let(:array) do
+ [ object_id.to_s, other ]
+ end
+
+ let(:evolved) do
+ array.__evolve_object_id__
+ end
+
+ it "converts the convertible ones to object ids" do
+ evolved.should eq([ object_id, other ])
+ end
+
+ it "returns the same instance" do
+ evolved.should equal(array)
+ end
+ end
+
+ context "when provided an array of object ids" do
+
+ let(:object_id) do
+ BSON::ObjectId.new
+ end
+
+ let(:array) do
+ [ object_id ]
+ end
+
+ let(:evolved) do
+ array.__evolve_object_id__
+ end
+
+ it "returns the array" do
+ evolved.should eq(array)
+ end
+
+ it "returns the same instance" do
+ evolved.should equal(array)
+ end
+ end
+
+ context "when some values are nil" do
+
+ let(:object_id) do
+ BSON::ObjectId.new
+ end
+
+ let(:array) do
+ [ object_id, nil ]
+ end
+
+ let(:evolved) do
+ array.__evolve_object_id__
+ end
+
+ it "returns the array without the nils" do
+ evolved.should eq([ object_id ])
+ end
+
+ it "returns the same instance" do
+ evolved.should equal(array)
+ end
+ end
+
+ context "when some values are empty strings" do
+
+ let(:object_id) do
+ BSON::ObjectId.new
+ end
+
+ let(:array) do
+ [ object_id, "" ]
+ end
+
+ let(:evolved) do
+ array.__evolve_object_id__
+ end
+
+ it "returns the array without the empty strings" do
+ evolved.should eq([ object_id ])
+ end
+
+ it "returns the same instance" do
+ evolved.should equal(array)
+ end
+ end
+ end
+
describe ".__mongoize_fk__" do
context "when the related model uses object ids" do
@@ -156,6 +255,64 @@
end
end
+ describe "#__mongoize_time__" do
+
+ context "when using active support's time zone" do
+
+ before do
+ Mongoid.use_activesupport_time_zone = true
+ ::Time.zone = "Tokyo"
+ end
+
+ after do
+ ::Time.zone = "Berlin"
+ end
+
+ let(:array) do
+ [ 2010, 11, 19, 00, 24, 49 ]
+ end
+
+ let(:time) do
+ array.__mongoize_time__
+ end
+
+ it "converts to a time" do
+ time.should eq(::Time.zone.local(*array))
+ end
+
+ it "converts to the as time zone" do
+ time.zone.should eq("JST")
+ end
+ end
+
+ context "when not using active support's time zone" do
+
+ before do
+ Mongoid.use_activesupport_time_zone = false
+ end
+
+ after do
+ Mongoid.use_activesupport_time_zone = true
+ end
+
+ let(:array) do
+ [ 2010, 11, 19, 00, 24, 49 ]
+ end
+
+ let(:time) do
+ array.__mongoize_time__
+ end
+
+ it "converts to a time" do
+ time.should eq(Time.local(*array))
+ end
+
+ it "converts to the local time zone" do
+ time.zone.should eq("CET")
+ end
+ end
+ end
+
describe "#delete_one" do
context "when the object doesn't exist" do
@@ -55,6 +55,10 @@
context "when the string is an invalid time" do
+ before do
+ Time.zone = nil
+ end
+
it "raises an error" do
expect {
DateTime.mongoize("shitty time")

0 comments on commit 93a0113

Please sign in to comment.