Skip to content

Commit

Permalink
Removed use_time_zone?, time_class and normalize_object_id from Mongo…
Browse files Browse the repository at this point in the history
…Mapper as they are not needed anymore and were most likely a stupid idea.
  • Loading branch information
jnunemaker committed Mar 31, 2010
1 parent 6fac712 commit b1cd7a9
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 55 deletions.
15 changes: 0 additions & 15 deletions lib/mongo_mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,21 +113,6 @@ def self.handle_passenger_forking
end
end

# @api private
def self.use_time_zone?
Time.respond_to?(:zone) && Time.zone ? true : false
end

# @api private
def self.time_class
use_time_zone? ? Time.zone : Time
end

# @api private
def self.normalize_object_id(value)
value.is_a?(String) ? Mongo::ObjectID.from_string(value) : value
end

autoload :Query, 'mongo_mapper/query'
autoload :Document, 'mongo_mapper/document'
autoload :EmbeddedDocument, 'mongo_mapper/embedded_document'
Expand Down
2 changes: 1 addition & 1 deletion lib/mongo_mapper/plugins/keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def id

def id=(value)
if self.class.using_object_id?
value = MongoMapper.normalize_object_id(value)
value = ObjectId.to_mongo(value)
end

self[:_id] = value
Expand Down
5 changes: 3 additions & 2 deletions lib/mongo_mapper/support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,15 @@ def self.to_mongo(value)
if value.nil? || value == ''
nil
else
time = value.is_a?(Time) ? value : MongoMapper.time_class.parse(value.to_s)
time_class = Time.try(:zone).present? ? Time.zone : Time
time = value.is_a?(Time) ? value : time_class.parse(value.to_s)
# Convert time to milliseconds since BSON stores dates with that accurracy, but Ruby uses microseconds
Time.at((time.to_f * 1000).round / 1000.0).utc if time
end
end

def self.from_mongo(value)
if MongoMapper.use_time_zone? && value.present?
if Time.try(:zone).present? && value.present?
value.in_time_zone(Time.zone)
else
value
Expand Down
37 changes: 0 additions & 37 deletions test/unit/test_mongo_mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,41 +115,4 @@ class MongoMapperTest < Test::Unit::TestCase
MongoMapper.setup(config, 'development', :logger => logger, :passenger => true)
end
end


context "use_time_zone?" do
should "be true if Time.zone set" do
Time.zone = 'Hawaii'
MongoMapper.use_time_zone?.should be_true
Time.zone = nil
end

should "be false if Time.zone not set" do
MongoMapper.use_time_zone?.should be_false
end
end

context "time_class" do
should "be Time.zone if using time zones" do
Time.zone = 'Hawaii'
MongoMapper.time_class.should == Time.zone
Time.zone = nil
end

should "be Time if not using time zones" do
MongoMapper.time_class.should == Time
end
end

context "normalize_object_id" do
should "turn string into object id" do
id = Mongo::ObjectID.new
MongoMapper.normalize_object_id(id.to_s).should == id
end

should "leave object id alone" do
id = Mongo::ObjectID.new
MongoMapper.normalize_object_id(id).should == id
end
end
end

0 comments on commit b1cd7a9

Please sign in to comment.