Skip to content
Browse files

Inspect should return attributes in alpha order, conversion fixes, ne…

…w version
  • Loading branch information...
1 parent 8a7d19a commit d9d3a87d233ca7a0039f337f6c9bcd2e1309fa88 Carlos Paramio committed Aug 28, 2010
View
20 Gemfile
@@ -1,16 +1,16 @@
# Use `bundle install` in order to install these gems
source 'http://rubygems.org'
-gem "activesupport", "3.0.0.beta3"
-gem "activemodel", "3.0.0.beta3"
-gem "mongo", "~>1.0.2"
-gem "bson_ext", "~>1.0.1"
-gem "tzinfo", "~>0.3.22"
+gem "activesupport", "~>3.0.0.rc2"
+gem "activemodel", "~>3.0.0.rc2"
+gem "mongo", "~>1.0.7"
+gem "bson_ext", "~>1.0.4"
+gem "tzinfo", "~>0.3.23"
group :development do
- gem 'jeweler', '1.4.0'
- gem 'yard', '0.5.6'
- gem 'rcov', '0.9.8'
- gem 'rspec', '2.0.0.beta.12'
- gem 'watchr', '~>0.6'
+ gem 'jeweler', '~>1.4.0'
+ gem 'yard', '~>0.5.8'
+ gem 'rcov', '~>0.9.8'
+ gem 'rspec', '~>2.0.0.beta.20'
+ gem 'watchr', '~>0.7'
end
View
17 README.rdoc
@@ -4,14 +4,16 @@ Flexible persistence module for any Ruby class to MongoDB.
= Why another ODM for MongoDB?
-MongoMapper, Mongoid, and others, are amazing projects. But I still think it's too much for an ODM for MongoDB.
-The Mongo Ruby driver allows you to query the database with a nice interface, the only required thing is a way to instanciate
-the results as objects of predefined classes, so you can encapsulate all the logic under a particular class and don't have to
-worry about type conversions.
-
+ * Fully compatible with Rails 3
+ * Use the Mongo ruby driver when possible (query syntax, cursors, indexes management...)
+ * No association methods (for now): Just declare your own methods on models to fetch related items
+ * Give support for dirty objects, validations, etc. through ActiveModel 3
+ * Automanage type conversions and default values
+ * Keep it as simple as possible
+
= Basics
-A piece of code is better than a hundred words:
+A piece of code is better than a hundred of words:
class Shape
include MongoODM::Document
@@ -32,8 +34,7 @@ A piece of code is better than a hundred words:
# "name" : "Point"
# }
- class Circle < Shape
- set_collection :shapes # Because we don't want them to be saved on a separated collection
+ class Circle < Shape # This items are stored on the 'shapes' collection
field :radius, Float, :default => 1.0
end
View
2 Rakefile
@@ -15,7 +15,7 @@ if Gem.available? 'jeweler'
gemspec.authors = ["Carlos Paramio"]
gemspec.files = FileList["[A-Z]*", "{lib,spec}/**/*"]
gemspec.version = MongoODM::VERSION
- bundle = Bundler::Definition.from_gemfile('Gemfile')
+ bundle = Bundler.definition
bundle.dependencies.each do |dependency|
if dependency.groups.include?(:default)
gemspec.add_dependency(dependency.name, dependency.requirement.to_s)
View
7 lib/mongo_odm.rb
@@ -8,7 +8,6 @@
module MongoODM
extend ActiveSupport::Autoload
- include ActiveSupport::Configurable
autoload :VERSION
autoload :Criteria
@@ -29,9 +28,13 @@ def self.database
connection.db(config[:database] || 'test')
end
+ def self.config
+ @_config ||= {}
+ end
+
def self.config=(value)
self.connection = nil
- super
+ @_config = value
end
def self.instanciate(value)
View
28 lib/mongo_odm/core_ext/conversions.rb
@@ -14,19 +14,6 @@
# * An attribute of the same class is sent to the Mongo driver
# @private
-class Mongo::ObjectID
- def self.type_cast(value)
- return nil if value.nil?
- return value if value.is_a?(Mongo::ObjectID)
- value.to_s
- end
-
- def to_mongo
- self
- end
-end
-
-# @private
class BSON::ObjectID
def self.type_cast(value)
return nil if value.nil?
@@ -66,8 +53,12 @@ def to_mongo
# @private
class Symbol
def self.type_cast(value)
- return nil if value.nil?
- value.to_s.intern
+ case value
+ when nil then nil
+ when Symbol then value
+ when String then value.intern
+ else value.inspect.intern
+ end
end
def to_mongo
@@ -114,8 +105,11 @@ def to_mongo
# @private
class String
def self.type_cast(value)
- return nil if value.nil?
- value.to_s
+ case value
+ when nil then nil
+ when String, Symbol then value.to_s
+ else value.inspect
+ end
end
def to_mongo
View
2 lib/mongo_odm/document/inspect.rb
@@ -19,7 +19,7 @@ def private_attribute_names
end
def inspect
- "#<#{self.class.name} #{attributes.except(*private_attribute_names).keys.map{|k| inspect_attribute(k)}.join(", ")}>"
+ "#<#{self.class.name} #{attributes.except(*private_attribute_names).keys.sort.map{|k| inspect_attribute(k)}.join(", ")}>"
end
end
View
4 lib/mongo_odm/version.rb
@@ -1,7 +1,7 @@
# encoding: utf-8
module MongoODM
VERSION_MAJOR = "0"
- VERSION_MINOR = "1"
- VERSION_BUILD = "9"
+ VERSION_MINOR = "2"
+ VERSION_BUILD = "0"
VERSION = "#{VERSION_MAJOR}.#{VERSION_MINOR}.#{VERSION_BUILD}"
end
View
18 spec/mongo_odm/core_ext/conversions_spec.rb
@@ -3,6 +3,10 @@
describe "Conversions" do
+ before do
+ Time.zone = 'UTC'
+ end
+
describe Array do
describe ".type_cast" do
@@ -74,6 +78,8 @@
it "tries to convert any other value to a symbol" do
Symbol.type_cast("test").should == :test
+ Symbol.type_cast(1).should == :"1"
+ Symbol.type_cast(3.14).should == :"3.14"
Symbol.type_cast([1, 2]).should == :"[1, 2]"
Symbol.type_cast({:a => 1, :b => 2}).should == :"{:a=>1, :b=>2}"
end
@@ -199,13 +205,13 @@
describe Date do
describe ".type_cast" do
-
+
it "returns nil when called with nil" do
Date.type_cast(nil).should == nil
end
it "tries to convert any other value to a date" do
- Date.type_cast("1/2/1980").should == Date.new(1980, 2, 1)
+ Date.type_cast("2/1/1980").should == Date.new(1980, 1, 2)
end
end
@@ -229,8 +235,8 @@
DateTime.type_cast(nil).should == nil
end
- it "tries to convert any other value to a date" do
- DateTime.type_cast("1/2/1980 11:30").should == DateTime.new(1980, 2, 1, 11, 30)
+ it "tries to convert any other value to a datetime" do
+ DateTime.type_cast("2/1/1980 11:30").should == DateTime.new(1980, 1, 2, 11, 30)
end
end
@@ -306,8 +312,8 @@
Time.type_cast(nil).should == nil
end
- it "tries to convert any other value to a date" do
- Time.type_cast("1/2/1980 11:30").should == Time.utc(1980, 2, 1, 11, 30)
+ it "tries to convert any other value to a timestamp" do
+ Time.type_cast("2/1/1980 11:30").should == Time.utc(1980, 1, 2, 11, 30)
end
end
View
2 spec/mongo_odm/document/inspect_spec.rb
@@ -19,7 +19,7 @@
describe "#inspect" do
it "returns a string with the name of the class that includes the MongoODM::Document module and a list of its attributes with values" do
circle = Circle.new(:x => 10, :y => 12.5, :radius => 1.2)
- circle.inspect.should == "#<Circle x: 10.0, y: 12.5, color: nil, radius: 1.2>"
+ circle.inspect.should == "#<Circle color: nil, radius: 1.2, x: 10.0, y: 12.5>"
end
end

0 comments on commit d9d3a87

Please sign in to comment.
Something went wrong with that request. Please try again.