From 991b28a7a32a2d6b91cfc79c3e3cc6084ac8a479 Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Sat, 29 Jan 2011 11:40:33 -0800 Subject: [PATCH 01/56] Fixed Config#from_hash to use indifferent-access, added Config#to_hash. --- lib/mongo_odm/config.rb | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/mongo_odm/config.rb b/lib/mongo_odm/config.rb index 7ea28f8..63ba42a 100644 --- a/lib/mongo_odm/config.rb +++ b/lib/mongo_odm/config.rb @@ -1,3 +1,5 @@ +require 'active_support/core_ext/hash/indifferent_access' +require 'active_support/core_ext/object/blank' require 'uri' module MongoODM @@ -20,8 +22,8 @@ def uri=(uri) @host, @port, @username, @password = uri.host, uri.port, uri.user, uri.password end - def from_hash(opts) - opts = opts.dup.symbolize_keys! + def from_hash( opts ) + opts = opts.with_indifferent_access if opts[:uri].present? self.uri = opts[:uri] @@ -34,6 +36,15 @@ def from_hash(opts) @pool_size ||= opts[:pool_size] end + def to_hash + conf = %w(database host port username password pool_size).map do |k| + [ k.to_sym, self.send(k.to_sym) ] + end + conf = Hash[conf] + conf.reject! { |k, v| v.nil? } + conf + end + def connection opts = { :logger => self.logger, :pool_size => self.pool_size } Mongo::Connection.new(self.host, self.port, opts).tap do |conn| From 82ec84fc8dc79feee3726a83a724ea240fd9eaec Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Sat, 29 Jan 2011 14:54:40 -0800 Subject: [PATCH 02/56] Added database_cleaner --- Gemfile | 5 +++-- spec/spec_helper.rb | 26 ++++++++++++++++++++++---- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/Gemfile b/Gemfile index 75b604f..b113310 100644 --- a/Gemfile +++ b/Gemfile @@ -8,9 +8,10 @@ gem "bson_ext", "~>1.2.0" gem "tzinfo", "~>0.3.24" group :development do + gem 'database_cleaner' gem 'jeweler', '~>1.5.2' - gem 'yard', '~>0.6.4' gem 'rcov', '~>0.9.9' gem 'rspec', '2.4.0' + gem 'yard', '~>0.6.4' gem 'watchr', '~>0.7' -end \ No newline at end of file +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index f005726..b685814 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -2,10 +2,28 @@ $LOAD_PATH.unshift(File.dirname(__FILE__)) $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib")) -require "rubygems" -require "rspec" -require "mongo_odm" +require 'rubygems' +require 'rspec' +require 'mongo_odm' +require 'database_cleaner' +#require File.expand_path('../../config/environment', __FILE__) + +MongoODM.config.database = 'mongo_odm-test' # Load all example models MODELS_PATH = File.join(File.dirname(__FILE__), "models") -Dir[ File.join(MODELS_PATH, "*.rb") ].sort.each { |file| require file } \ No newline at end of file +Dir[ File.join(MODELS_PATH, "*.rb") ].sort.each { |file| require file } + +RSpec.configure do |config| + # db cleaner + class ::Mongoid + def self.database() MongoODM.database end + end + config.before(:suite) do + DatabaseCleaner.orm = :mongoid + DatabaseCleaner.strategy = :truncation + end + config.before(:each) do + DatabaseCleaner.clean + end +end From b86e58e4d1959c4972e7440f378183be88b7402f Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Sat, 29 Jan 2011 14:58:22 -0800 Subject: [PATCH 03/56] Fixed spec for Time conversion. --- spec/mongo_odm/core_ext/conversions_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/mongo_odm/core_ext/conversions_spec.rb b/spec/mongo_odm/core_ext/conversions_spec.rb index 7bc10e4..81a7cba 100644 --- a/spec/mongo_odm/core_ext/conversions_spec.rb +++ b/spec/mongo_odm/core_ext/conversions_spec.rb @@ -321,8 +321,8 @@ describe "#to_mongo" do it "returns the time in UTC" do - @test = Time.local(4, 56, 8, 9, 4, 2003, 3, 99, true, "CDT") - @test.to_mongo.should == Time.utc(2003, 4, 9, 6, 56, 4) + @time = Time.zone.parse('2003-04-09 08:56:04 CDT') + @time.to_mongo.should == Time.utc(2003, 4, 9, 13, 56, 4) end end From ae682d588f5fa9381c0e6f06d6bf582e42c7ab47 Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Sat, 29 Jan 2011 17:05:43 -0800 Subject: [PATCH 04/56] Added railtie with DB rake tasks. --- lib/mongo_odm/railtie.rb | 6 ++++++ lib/mongo_odm/railties/database.rake | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 lib/mongo_odm/railties/database.rake diff --git a/lib/mongo_odm/railtie.rb b/lib/mongo_odm/railtie.rb index 96a389d..24b73c8 100644 --- a/lib/mongo_odm/railtie.rb +++ b/lib/mongo_odm/railtie.rb @@ -1,6 +1,12 @@ module Rails #:nodoc: module MongoODM #:nodoc: class Railtie < Rails::Railtie #:nodoc: + config.generators.orm :mongo_odm, :migration => false + + rake_tasks do + load 'mongo_odm/railties/database.rake' + end + initializer 'configure database' do config_file = Rails.root.join 'config', 'mongo.yml' if config_file.file? diff --git a/lib/mongo_odm/railties/database.rake b/lib/mongo_odm/railties/database.rake new file mode 100644 index 0000000..df08b51 --- /dev/null +++ b/lib/mongo_odm/railties/database.rake @@ -0,0 +1,26 @@ +namespace :db do + unless Rake::Task.task_defined?('db:drop') + desc 'Drops all the collections from the database' + task :drop => :environment do + MongoODM.database.collections.select { |c| c.name !~ /system/ }.each(&:drop) + end + end + + unless Rake::Task.task_defined?('db:seed') + desc 'Load the seed data from db/seeds.rb' + task :seed => :environment do + seed_file = File.join( Rails.root, 'db', 'seeds.rb' ) + load(seed_file) if File.exist?(seed_file) + end + end + + unless Rake::Task.task_defined?('db:setup') + desc 'Create the database, and initialize with the seed data' + task :setup => [ 'db:seed' ] + end + + unless Rake::Task.task_defined?('db:reseed') + desc 'Delete data and seed' + task :reseed => [ 'db:drop', 'db:seed' ] + end +end From bd89e54e979605d1de9709ea71954fb6bccb35db Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Sun, 30 Jan 2011 15:39:04 -0800 Subject: [PATCH 05/56] Criteria: fixed infinite-recursion by mapping enumerable methods directly to Cursor. --- lib/mongo_odm/criteria.rb | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/lib/mongo_odm/criteria.rb b/lib/mongo_odm/criteria.rb index 9ae9394..cf1122f 100644 --- a/lib/mongo_odm/criteria.rb +++ b/lib/mongo_odm/criteria.rb @@ -2,21 +2,23 @@ module MongoODM class Criteria - delegate :inspect, :to_xml, :to_yaml, :length, :collect, :map, :each, :all?, :include?, :to => :to_a - + delegate :to_a, :count, :collect, :map, :each, :all?, :include?, :to => :cursor + delegate :inspect, :to_xml, :to_yaml, :length, :to => :to_a + def initialize(klass, selector = {}, opts = {}) @klass, @selector, @opts = klass, selector, opts - @loaded = false + @cursor = nil end - + def find(selector = {}, opts = {}) _merge_criteria(selector, opts) + @cursor = nil end - + def loaded? - @loaded + @cursor end - + def ==(other) case other when Criteria @@ -26,7 +28,7 @@ def ==(other) to_a == other.to_a end end - + def method_missing(method_name, *args, &block) if Array.method_defined?(method_name) to_a.send(method_name, *args, &block) @@ -40,15 +42,22 @@ def method_missing(method_name, *args, &block) result end else - @klass.collection.find(@selector, @opts).send(method_name, *args) + cursor.send(method_name, *args) end end - + def _merge_criteria(selector, opts) @selector.merge!(selector) @opts.merge!(opts) + @cursor = nil self end + + protected + + def cursor + @cursor ||= @klass.collection.find(@selector, @opts) + end end end \ No newline at end of file From ca81e6382e92f7e540de39d9fdfe61cfe3fee39a Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Sun, 30 Jan 2011 16:42:59 -0800 Subject: [PATCH 06/56] Added autotest, ruby-debug. Updated gitignore. --- .gitignore | 5 ++++- Gemfile | 7 +++++++ autotest/discover.rb | 1 + 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 autotest/discover.rb diff --git a/.gitignore b/.gitignore index d97cde2..4c8eabd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,7 @@ +Gemfile.lock doc pkg +vendor/bundle +.bundle .rvmrc -.bundle \ No newline at end of file +.kdev4 diff --git a/Gemfile b/Gemfile index b113310..3308add 100644 --- a/Gemfile +++ b/Gemfile @@ -8,10 +8,17 @@ gem "bson_ext", "~>1.2.0" gem "tzinfo", "~>0.3.24" group :development do + gem 'autotest' gem 'database_cleaner' gem 'jeweler', '~>1.5.2' gem 'rcov', '~>0.9.9' gem 'rspec', '2.4.0' gem 'yard', '~>0.6.4' gem 'watchr', '~>0.7' + + if RUBY_VERSION =~ /1.9/ + gem 'ruby-debug19', :require => 'ruby-debug' + else + gem 'ruby-debug' + end end diff --git a/autotest/discover.rb b/autotest/discover.rb new file mode 100644 index 0000000..47355c9 --- /dev/null +++ b/autotest/discover.rb @@ -0,0 +1 @@ +Autotest.add_discovery { 'rspec2' } From 511674e080c798f4b108bd96ca777ab3d2df5c14 Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Sat, 29 Jan 2011 11:48:58 -0800 Subject: [PATCH 07/56] Document: added Finders, refactored #find to take one or more ids, added #where. --- lib/mongo_odm/document.rb | 4 +- lib/mongo_odm/document/finders.rb | 51 +++++++++++++++++++++++++ lib/mongo_odm/document/persistence.rb | 33 +++++++--------- spec/mongo_odm/document/finders_spec.rb | 22 +++++++++++ 4 files changed, 90 insertions(+), 20 deletions(-) create mode 100644 lib/mongo_odm/document/finders.rb create mode 100644 spec/mongo_odm/document/finders_spec.rb diff --git a/lib/mongo_odm/document.rb b/lib/mongo_odm/document.rb index 43c7a56..b463671 100644 --- a/lib/mongo_odm/document.rb +++ b/lib/mongo_odm/document.rb @@ -10,6 +10,7 @@ module Document autoload :AttributeMethods autoload :Callbacks autoload :Fields + autoload :Finders autoload :Inspect autoload :Persistence autoload :Timestamps @@ -24,6 +25,7 @@ module Document include ActiveModel::Serializers::JSON include ActiveModel::Serializers::Xml + include Document::Finders include Document::Persistence include Document::AttributeMethods include Document::Fields @@ -34,4 +36,4 @@ module Document end end -end \ No newline at end of file +end diff --git a/lib/mongo_odm/document/finders.rb b/lib/mongo_odm/document/finders.rb new file mode 100644 index 0000000..7c492a7 --- /dev/null +++ b/lib/mongo_odm/document/finders.rb @@ -0,0 +1,51 @@ +module MongoODM + module Document + module Finders + + extend ActiveSupport::Concern + + module ClassMethods + def where( *args ) + MongoODM::Criteria.new(self, *args) + end + + def find( *args ) + if (args.size == 1) && (criteria = find_one_by_id(args.first)) + criteria.first + elsif (criteria = find_many_by_ids(args)) + criteria + else + where(*args) + end + end + + protected + def find_one_by_id( id ) + unless id.kind_of? BSON::ObjectId + begin + id = BSON::ObjectId.from_string(id) + rescue BSON::InvalidObjectId + return nil + end + end + self.where( :_id => id ) + end + + def find_many_by_ids( ids ) + ids = ids.map do |id| + if id.kind_of?(BSON::ObjectId) + id + else + begin + BSON::ObjectId.from_string(id) + rescue BSON::InvalidObjectId + return nil + end + end + end + self.where( :_id => { :$in => ids } ) + end + end + end + end +end \ No newline at end of file diff --git a/lib/mongo_odm/document/persistence.rb b/lib/mongo_odm/document/persistence.rb index ce21f96..814bb4b 100644 --- a/lib/mongo_odm/document/persistence.rb +++ b/lib/mongo_odm/document/persistence.rb @@ -5,7 +5,7 @@ module MongoODM module Document module Persistence - + extend ActiveSupport::Concern module InstanceMethods @@ -16,20 +16,20 @@ def id def new_record? id.nil? end - + def persisted? !new_record? end - + def reload self.load_attributes_or_defaults(self.class.find_one(:_id => id).attributes) unless new_record? end - + # Save a document to its collection. # # @return [ObjectId] the _id of the saved document. # - # @option opts [Boolean] :safe (+false+) + # @option opts [Boolean] :safe (+false+) # If true, check that the save succeeded. OperationFailure # will be raised on an error. Note that a safe check requires an extra # round-trip to the database. @@ -38,19 +38,19 @@ def save(options = {}) write_attribute(:_id, self.class.save(to_mongo, options)) end end - + def update_attributes(attributes) self.attributes = attributes save end - + def destroy return false if new_record? _run_destroy_callbacks do self.class.remove({ :_id => self.id }) end end - + def to_mongo attributes.inject({}) do |attrs, (key, value)| attrs[key] = value.to_mongo unless value.nil? # self.class.fields[key].default == value @@ -58,13 +58,13 @@ def to_mongo end end end - + module ClassMethods delegate :collection, :db, :hint, :hint=, :pk_factory, :[], :count, :create_index, :distinct, :drop, :drop_index, :drop_indexes, :find_and_modify, :find_one, :group, :index_information, :insert, :<<, :map_reduce, :mapreduce, :options, :remove, :rename, :save, :stats, :update, :to => :collection - + def collection @collection ||= if self.superclass.included_modules.include?(MongoODM::Document) self.superclass.collection @@ -72,7 +72,7 @@ def collection MongoODM::Collection.new(MongoODM.database, name.tableize) end end - + def set_collection(name_or_collection) @collection = case name_or_collection when MongoODM::Collection @@ -83,18 +83,14 @@ def set_collection(name_or_collection) raise "MongoODM::Collection instance or valid collection name required" end end - - def find(*args) - MongoODM::Criteria.new(self, *args) - end - + def destroy_all(*args) documents = find(*args) count = documents.count documents.each { |doc| doc.destroy } count end - + def type_cast(value) return nil if value.nil? return value if value.class == self @@ -102,7 +98,6 @@ def type_cast(value) end end - end end -end \ No newline at end of file +end diff --git a/spec/mongo_odm/document/finders_spec.rb b/spec/mongo_odm/document/finders_spec.rb new file mode 100644 index 0000000..e048852 --- /dev/null +++ b/spec/mongo_odm/document/finders_spec.rb @@ -0,0 +1,22 @@ +# encoding: utf-8 +require 'spec_helper' + +describe MongoODM::Document::Finders do + before do + @shapes = %w(red green blue).map { |color| shape = Shape.new(:color => color) ; shape.save ; shape } + end + + describe "#find" do + it "finds one by id" do + Shape.find(@shapes.first.id).should == @shapes.first + end + + it "finds multiple by ids" do + Shape.find( *@shapes.map(&:id) ).should == @shapes + end + + it "finds by criteria" do + Shape.find( :color => @shapes.last.color ).should == [@shapes.last] + end + end +end \ No newline at end of file From be1e0dce077db64f0186db34d269f6fdd8194eec Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Sat, 29 Jan 2011 11:52:40 -0800 Subject: [PATCH 08/56] Document: added various AR-compatibility and convenience functions, refactored Errors. --- lib/mongo_odm/document/finders.rb | 18 ++++++++++++ lib/mongo_odm/document/persistence.rb | 4 +++ lib/mongo_odm/errors.rb | 17 ++++++++--- spec/mongo_odm/document/finders_spec.rb | 38 +++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 4 deletions(-) diff --git a/lib/mongo_odm/document/finders.rb b/lib/mongo_odm/document/finders.rb index 7c492a7..b394f58 100644 --- a/lib/mongo_odm/document/finders.rb +++ b/lib/mongo_odm/document/finders.rb @@ -19,6 +19,24 @@ def find( *args ) end end + def find!( *ids ) + documents = Array( find(*ids) ) + found = documents.map(&:id) + missing = ids.reject { |id| found.include?(id) } + missing.empty? or raise Errors::DocumentNotFound.new(self.class, missing) + (documents.count == 1) ? documents.first : documents + end + + def find_or_initialize_by( attr ) + self.find(attr).first || self.new(attr) + end + + def find_or_create_by( attr ) + doc = find_or_initialize_by(attr) + doc.save if doc.new_record? + doc + end + protected def find_one_by_id( id ) unless id.kind_of? BSON::ObjectId diff --git a/lib/mongo_odm/document/persistence.rb b/lib/mongo_odm/document/persistence.rb index 814bb4b..65b6c67 100644 --- a/lib/mongo_odm/document/persistence.rb +++ b/lib/mongo_odm/document/persistence.rb @@ -13,6 +13,10 @@ def id attributes[:_id] end + def to_param + id.to_s + end + def new_record? id.nil? end diff --git a/lib/mongo_odm/errors.rb b/lib/mongo_odm/errors.rb index 4e8ff71..9fc716e 100644 --- a/lib/mongo_odm/errors.rb +++ b/lib/mongo_odm/errors.rb @@ -1,18 +1,27 @@ # encoding: utf-8 module MongoODM + class Error < StandardError + end module Errors - class TypeCastMissing < StandardError + class TypeCastMissing < Error def initialize(value, klass) super "can't convert #{value.inspect} to #{klass}: Define a `type_cast' method for #{value.class}:#{value.class.class}" end end - - class InvalidLocalizedField < StandardError + + class InvalidLocalizedField < Error def initialize(field_name, klass) super "can't localize field #{field_name}; it has to be declared as a Hash, was #{klass}" end end + + class DocumentNotFound < Error + attr_reader :klass, :ids + def initialize( klass, ids ) + @klass, @ids = klass, Array(ids) + end + end end -end \ No newline at end of file +end diff --git a/spec/mongo_odm/document/finders_spec.rb b/spec/mongo_odm/document/finders_spec.rb index e048852..eb0cf40 100644 --- a/spec/mongo_odm/document/finders_spec.rb +++ b/spec/mongo_odm/document/finders_spec.rb @@ -19,4 +19,42 @@ Shape.find( :color => @shapes.last.color ).should == [@shapes.last] end end + + describe "#find!" do + it "finds existing document" do + Shape.find!(@shapes.first.id).should == @shapes.first + end + + it "errors when document missing" do + lambda { + Shape.find!( BSON::ObjectId.new ) + }.should raise_error( MongoODM::Errors::DocumentNotFound ) + end + end + + describe "#find_or_initialize_by" do + it "finds existing document" do + Shape.find_or_initialize_by( :color => @shapes.last.color ).should == @shapes.last + end + + it "initializes new document" do + color = 'purple' + shape = Shape.find_or_initialize_by( :color => color ) + shape.color.should == color + shape.should be_new_record + end + end + + describe "#find_or_create_by" do + it "finds existing document" do + Shape.find_or_create_by( :color => @shapes.last.color ).should == @shapes.last + end + + it "creates new document" do + color = 'purple' + shape = Shape.find_or_create_by( :color => color ) + shape.color.should == color + shape.should_not be_new_record + end + end end \ No newline at end of file From 5bf3a7da76985274df9f1a545abec1f0f7b14c01 Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Sun, 30 Jan 2011 15:23:41 -0800 Subject: [PATCH 09/56] Added #save! to raise error on invalid documents --- lib/mongo_odm/document/persistence.rb | 5 +++++ lib/mongo_odm/errors.rb | 7 +++++++ spec/mongo_odm/document/persistence_spec.rb | 16 ++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/lib/mongo_odm/document/persistence.rb b/lib/mongo_odm/document/persistence.rb index 65b6c67..5c77444 100644 --- a/lib/mongo_odm/document/persistence.rb +++ b/lib/mongo_odm/document/persistence.rb @@ -43,6 +43,11 @@ def save(options = {}) end end + def save!(options = {}) + valid? or raise Errors::Validation.new(self) + save(options) + end + def update_attributes(attributes) self.attributes = attributes save diff --git a/lib/mongo_odm/errors.rb b/lib/mongo_odm/errors.rb index 9fc716e..da2411a 100644 --- a/lib/mongo_odm/errors.rb +++ b/lib/mongo_odm/errors.rb @@ -16,6 +16,13 @@ def initialize(field_name, klass) end end + class Validation < Error + attr_reader :document + def initialize( doc ) + @document = doc + end + end + class DocumentNotFound < Error attr_reader :klass, :ids def initialize( klass, ids ) diff --git a/spec/mongo_odm/document/persistence_spec.rb b/spec/mongo_odm/document/persistence_spec.rb index 2c94593..5ccce17 100644 --- a/spec/mongo_odm/document/persistence_spec.rb +++ b/spec/mongo_odm/document/persistence_spec.rb @@ -2,5 +2,21 @@ require "spec_helper" describe MongoODM::Document::Persistence do + describe "#save!" do + class TestDocument < Shape + validates_presence_of :color + end + it "saves a document" do + doc = TestDocument.new :color => 'blue' + doc.should be_valid + proc { doc.save! }.should_not raise_error(MongoODM::Errors::Validation) + end + + it "raises error on invalid document" do + doc = TestDocument.new :color => nil + doc.should_not be_valid + proc { doc.save! }.should raise_error(MongoODM::Errors::Validation) + end + end end \ No newline at end of file From 32f43a3ac6e99c5a8e93c28f5921dd5ab5b85161 Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Sun, 30 Jan 2011 16:28:55 -0800 Subject: [PATCH 10/56] Document#save now returns instance instead of id (warning: backward-incompatible!) --- lib/mongo_odm/document/persistence.rb | 1 + spec/mongo_odm/document/finders_spec.rb | 2 +- spec/mongo_odm/document/persistence_spec.rb | 8 ++++++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/mongo_odm/document/persistence.rb b/lib/mongo_odm/document/persistence.rb index 5c77444..75bc103 100644 --- a/lib/mongo_odm/document/persistence.rb +++ b/lib/mongo_odm/document/persistence.rb @@ -41,6 +41,7 @@ def save(options = {}) _run_save_callbacks do write_attribute(:_id, self.class.save(to_mongo, options)) end + self end def save!(options = {}) diff --git a/spec/mongo_odm/document/finders_spec.rb b/spec/mongo_odm/document/finders_spec.rb index eb0cf40..e28d867 100644 --- a/spec/mongo_odm/document/finders_spec.rb +++ b/spec/mongo_odm/document/finders_spec.rb @@ -3,7 +3,7 @@ describe MongoODM::Document::Finders do before do - @shapes = %w(red green blue).map { |color| shape = Shape.new(:color => color) ; shape.save ; shape } + @shapes = %w(red green blue).map { |color| shape = Shape.new(:color => color).save } end describe "#find" do diff --git a/spec/mongo_odm/document/persistence_spec.rb b/spec/mongo_odm/document/persistence_spec.rb index 5ccce17..26b48fc 100644 --- a/spec/mongo_odm/document/persistence_spec.rb +++ b/spec/mongo_odm/document/persistence_spec.rb @@ -2,6 +2,14 @@ require "spec_helper" describe MongoODM::Document::Persistence do + describe "#save" do + it "saves a document" do + shape = Shape.new( :color => 'magenta' ) + shape.save.should == shape + shape.id.should_not be_nil + end + end + describe "#save!" do class TestDocument < Shape validates_presence_of :color From b1f00a34a1a8049e6e11941fef19b3abcbfb322c Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Sat, 12 Feb 2011 11:45:21 -0800 Subject: [PATCH 11/56] Document: fixed #find when no arguments. --- lib/mongo_odm/document/finders.rb | 4 ++-- spec/mongo_odm/document/finders_spec.rb | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/mongo_odm/document/finders.rb b/lib/mongo_odm/document/finders.rb index b394f58..9dedabb 100644 --- a/lib/mongo_odm/document/finders.rb +++ b/lib/mongo_odm/document/finders.rb @@ -12,7 +12,7 @@ def where( *args ) def find( *args ) if (args.size == 1) && (criteria = find_one_by_id(args.first)) criteria.first - elsif (criteria = find_many_by_ids(args)) + elsif criteria = find_many_by_ids(args) criteria else where(*args) @@ -61,7 +61,7 @@ def find_many_by_ids( ids ) end end end - self.where( :_id => { :$in => ids } ) + ids.present? && self.where( :_id => { :$in => ids } ) end end end diff --git a/spec/mongo_odm/document/finders_spec.rb b/spec/mongo_odm/document/finders_spec.rb index e28d867..3cb19df 100644 --- a/spec/mongo_odm/document/finders_spec.rb +++ b/spec/mongo_odm/document/finders_spec.rb @@ -7,6 +7,12 @@ end describe "#find" do + it "returns criteria" do + shapes = Shape.find + shapes.should be_kind_of(MongoODM::Criteria) + shapes.should == @shapes + end + it "finds one by id" do Shape.find(@shapes.first.id).should == @shapes.first end @@ -21,6 +27,10 @@ end describe "#find!" do + it "returns criteria" do + Shape.find!.should == @shapes + end + it "finds existing document" do Shape.find!(@shapes.first.id).should == @shapes.first end From a3fb16cbe436a6f86d579f809f30621ef1d93655 Mon Sep 17 00:00:00 2001 From: Dusty Date: Wed, 16 Feb 2011 13:12:51 -0300 Subject: [PATCH 12/56] add missing error class --- lib/mongo_odm/errors.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/mongo_odm/errors.rb b/lib/mongo_odm/errors.rb index 4e8ff71..f847b55 100644 --- a/lib/mongo_odm/errors.rb +++ b/lib/mongo_odm/errors.rb @@ -13,6 +13,8 @@ def initialize(field_name, klass) super "can't localize field #{field_name}; it has to be declared as a Hash, was #{klass}" end end + + class UnknownFieldError < StandardError; end end end \ No newline at end of file From b0368203cb8e2e5651d97ea9f5133463c60d0007 Mon Sep 17 00:00:00 2001 From: Dusty Date: Wed, 16 Feb 2011 13:14:16 -0300 Subject: [PATCH 13/56] update Mongo::Collection.new calls --- README.rdoc | 2 +- lib/mongo_odm/document/persistence.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.rdoc b/README.rdoc index ca2614f..62c7edc 100644 --- a/README.rdoc +++ b/README.rdoc @@ -102,7 +102,7 @@ By default, mongo_odm stores data on a collection with the same name than the cl Alternatively, you can pass a MongoODM::Collection instance to set_collection, to indicate not only a collection name, but also a different connection and/or database: class Shape - set_collection MongoODM::Collection.new(MongoODM.connection.db('another_database'), 'my_shapes') + set_collection MongoODM::Collection.new('my_shapes', MongoODM.connection.db('another_database')) end = Associations diff --git a/lib/mongo_odm/document/persistence.rb b/lib/mongo_odm/document/persistence.rb index ce21f96..47895c2 100644 --- a/lib/mongo_odm/document/persistence.rb +++ b/lib/mongo_odm/document/persistence.rb @@ -69,7 +69,7 @@ def collection @collection ||= if self.superclass.included_modules.include?(MongoODM::Document) self.superclass.collection else - MongoODM::Collection.new(MongoODM.database, name.tableize) + MongoODM::Collection.new(name.tableize, MongoODM.database) end end @@ -78,7 +78,7 @@ def set_collection(name_or_collection) when MongoODM::Collection name_or_collection when String, Symbol - MongoODM::Collection.new(MongoODM.database, name_or_collection) + MongoODM::Collection.new(name_or_collection, MongoODM.database) else raise "MongoODM::Collection instance or valid collection name required" end From a16a1da416e1eb37ef3b265d326e62ea5d6c2e50 Mon Sep 17 00:00:00 2001 From: Dusty Date: Wed, 16 Feb 2011 21:39:53 -0300 Subject: [PATCH 14/56] before_validate callback --- lib/mongo_odm/document/callbacks.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/mongo_odm/document/callbacks.rb b/lib/mongo_odm/document/callbacks.rb index 89cadda..52da2ab 100644 --- a/lib/mongo_odm/document/callbacks.rb +++ b/lib/mongo_odm/document/callbacks.rb @@ -14,6 +14,7 @@ module Callbacks define_model_callbacks :initialize, :only => :after define_model_callbacks :save, :destroy + define_model_callbacks :validate, :only => :before end module InstanceMethods From dbed85def7efbfbead3629972b4929b6e35e1607 Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Mon, 14 Mar 2011 16:41:43 -0700 Subject: [PATCH 15/56] Updated gems, added Bundler to spec_helper. --- Gemfile | 18 +++++++++--------- Gemfile.lock | 8 +++++++- spec/spec_helper.rb | 5 +++-- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/Gemfile b/Gemfile index e4c63f1..45657d3 100644 --- a/Gemfile +++ b/Gemfile @@ -1,20 +1,20 @@ # Use `bundle install` in order to install these gems source 'http://rubygems.org' -gem "activesupport", "~>3.0.5" -gem "activemodel", "~>3.0.5" -gem "mongo", "~>1.2.4" -gem "bson_ext", "~>1.2.4" -gem "tzinfo", "~>0.3.24" +gem 'activesupport', '~> 3.0.5' +gem 'activemodel', '~> 3.0.5' +gem 'mongo', '~> 1.2.4' +gem 'bson_ext', '~> 1.2.4' +gem 'tzinfo', '~> 0.3.24' group :development do gem 'autotest' gem 'database_cleaner' - gem 'jeweler', '~>1.5.2' - gem 'rcov', '~>0.9.9' + gem 'jeweler', '~> 1.5.2' + gem 'rcov', '~> 0.9.9' gem 'rspec', '~> 2.5.0' - gem 'yard', '~>0.6.5' - gem 'watchr', '~>0.7' + gem 'yard', '~> 0.6.5' + gem 'watchr', '~> 0.7' if RUBY_VERSION =~ /1.9/ gem 'ruby-debug19', :require => 'ruby-debug' diff --git a/Gemfile.lock b/Gemfile.lock index 9a584e6..fcdb897 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,14 +1,18 @@ GEM remote: http://rubygems.org/ specs: + ZenTest (4.5.0) activemodel (3.0.5) activesupport (= 3.0.5) builder (~> 2.1.2) i18n (~> 0.4) activesupport (3.0.5) + autotest (4.4.6) + ZenTest (>= 4.4.1) bson (1.2.4) bson_ext (1.2.4) builder (2.1.2) + database_cleaner (0.6.5) diff-lcs (1.1.2) git (1.2.5) i18n (0.5.0) @@ -28,7 +32,7 @@ GEM rspec-expectations (2.5.0) diff-lcs (~> 1.1.2) rspec-mocks (2.5.0) - tzinfo (0.3.24) + tzinfo (0.3.25) watchr (0.7) yard (0.6.5) @@ -38,7 +42,9 @@ PLATFORMS DEPENDENCIES activemodel (~> 3.0.5) activesupport (~> 3.0.5) + autotest bson_ext (~> 1.2.4) + database_cleaner jeweler (~> 1.5.2) mongo (~> 1.2.4) rcov (~> 0.9.9) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b685814..d8fcb7d 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -3,10 +3,11 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib")) require 'rubygems' -require 'rspec' +require 'bundler' +Bundler.setup + require 'mongo_odm' require 'database_cleaner' -#require File.expand_path('../../config/environment', __FILE__) MongoODM.config.database = 'mongo_odm-test' From e17dd90d0de1cc415a2cec923e07d878ddc3898f Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Thu, 17 Mar 2011 17:42:44 -0700 Subject: [PATCH 16/56] Fixed code style. --- lib/mongo_odm.rb | 2 +- lib/mongo_odm/config.rb | 4 ++-- lib/mongo_odm/document/finders.rb | 26 ++++++++++----------- lib/mongo_odm/document/persistence.rb | 2 +- lib/mongo_odm/document/timestamps.rb | 2 +- lib/mongo_odm/errors.rb | 2 +- lib/mongo_odm/railtie.rb | 4 ++-- spec/mongo_odm/document/finders_spec.rb | 16 ++++++------- spec/mongo_odm/document/persistence_spec.rb | 6 ++--- spec/mongo_odm/mongo_odm_spec.rb | 10 ++++---- 10 files changed, 37 insertions(+), 37 deletions(-) diff --git a/lib/mongo_odm.rb b/lib/mongo_odm.rb index 81d6571..8639505 100644 --- a/lib/mongo_odm.rb +++ b/lib/mongo_odm.rb @@ -27,7 +27,7 @@ def self.connection=(value) end def self.database - Thread.current[:mongo_odm_database] ||= self.connection.db( config.database ) + Thread.current[:mongo_odm_database] ||= self.connection.db(config.database) end def self.config diff --git a/lib/mongo_odm/config.rb b/lib/mongo_odm/config.rb index 63ba42a..e16b7bc 100644 --- a/lib/mongo_odm/config.rb +++ b/lib/mongo_odm/config.rb @@ -12,7 +12,7 @@ def initialize(opts = {}) @database ||= 'test' @host ||= 'localhost' @port || 27017 - @logger ||= Rails.logger if defined?(Rails) + @logger ||= Rails.logger if defined?(Rails) @pool_size ||= 1 end @@ -22,7 +22,7 @@ def uri=(uri) @host, @port, @username, @password = uri.host, uri.port, uri.user, uri.password end - def from_hash( opts ) + def from_hash(opts) opts = opts.with_indifferent_access if opts[:uri].present? diff --git a/lib/mongo_odm/document/finders.rb b/lib/mongo_odm/document/finders.rb index c8a7081..6c5cb45 100644 --- a/lib/mongo_odm/document/finders.rb +++ b/lib/mongo_odm/document/finders.rb @@ -5,11 +5,11 @@ module Finders extend ActiveSupport::Concern module ClassMethods - def where( selector = {}, opts = {} ) + def where(selector = {}, opts = {}) MongoODM::Criteria.new(self, :selector => selector, :opts => opts) end - def find( *args ) + def find(*args) if (args.size == 1) && (criteria = find_one_by_id(args.first)) criteria.first elsif (args.size >= 2) && (criteria = find_many_by_ids(args)) @@ -19,21 +19,21 @@ def find( *args ) end end - def find!( *ids ) - documents = Array( find(*ids) ) + def find!(*ids) + documents = Array(find(*ids)) found = documents.map(&:id) missing = ids.reject { |id| found.include?(id) } missing.empty? or raise Errors::DocumentNotFound.new(self.class, missing) (documents.count == 1) ? documents.first : documents end - def find_or_initialize_by( attr ) + def find_or_initialize_by(attr) self.find(attr).first || self.new(attr) end - def find_or_create_by( attr ) + def find_or_create_by(attr) doc = find_or_initialize_by(attr) - doc.save if doc.new_record? + doc.save if doc.new_record? doc end @@ -63,8 +63,8 @@ def method_missing(method_name, *args, &block) end protected - def find_one_by_id( id ) - return nil if id.kind_of?(Hash) + def find_one_by_id(id) + return nil if id.kind_of?(Hash) unless id.kind_of? BSON::ObjectId begin id = BSON::ObjectId.from_string(id.to_s) @@ -72,11 +72,11 @@ def find_one_by_id( id ) return nil end end - self.where( :_id => id ) + self.where(:_id => id) end - def find_many_by_ids( ids ) - return nil if ids.any? { |id| id.kind_of?(Hash) } + def find_many_by_ids(ids) + return nil if ids.any? { |id| id.kind_of?(Hash) } ids = ids.map do |id| if id.kind_of?(BSON::ObjectId) id @@ -88,7 +88,7 @@ def find_many_by_ids( ids ) end end end.compact - ids.present? && self.where( :_id => { :$in => ids } ) + ids.present? && self.where(:_id => { :$in => ids }) end end end diff --git a/lib/mongo_odm/document/persistence.rb b/lib/mongo_odm/document/persistence.rb index 942b1a0..2ae10e7 100644 --- a/lib/mongo_odm/document/persistence.rb +++ b/lib/mongo_odm/document/persistence.rb @@ -45,7 +45,7 @@ def save(options = {}) end def save!(options = {}) - valid? or raise Errors::Validation.new(self) + valid? or raise(Errors::Validation, self) save(options) end diff --git a/lib/mongo_odm/document/timestamps.rb b/lib/mongo_odm/document/timestamps.rb index 1ce62c8..2b818dd 100644 --- a/lib/mongo_odm/document/timestamps.rb +++ b/lib/mongo_odm/document/timestamps.rb @@ -14,7 +14,7 @@ module Timestamps module InstanceMethods def set_timestamps now = Time.now.utc - self.created_at ||= now if self.new_record? + self.created_at ||= now if self.new_record? self.updated_at = now end end diff --git a/lib/mongo_odm/errors.rb b/lib/mongo_odm/errors.rb index 7fc141a..a8ea40b 100644 --- a/lib/mongo_odm/errors.rb +++ b/lib/mongo_odm/errors.rb @@ -30,7 +30,7 @@ def initialize(ids, klass) class Validation < Error attr_reader :document - def initialize( doc ) + def initialize(doc) @document = doc end end diff --git a/lib/mongo_odm/railtie.rb b/lib/mongo_odm/railtie.rb index 24b73c8..c02f61b 100644 --- a/lib/mongo_odm/railtie.rb +++ b/lib/mongo_odm/railtie.rb @@ -10,8 +10,8 @@ class Railtie < Rails::Railtie #:nodoc: initializer 'configure database' do config_file = Rails.root.join 'config', 'mongo.yml' if config_file.file? - config = YAML.load( ERB.new( config_file.read ).result )[Rails.env] - ::MongoODM.config = config if config.present? + config = YAML.load(ERB.new(config_file.read).result)[Rails.env] + ::MongoODM.config = config if config.present? end end end diff --git a/spec/mongo_odm/document/finders_spec.rb b/spec/mongo_odm/document/finders_spec.rb index 3cb19df..39c49ac 100644 --- a/spec/mongo_odm/document/finders_spec.rb +++ b/spec/mongo_odm/document/finders_spec.rb @@ -18,11 +18,11 @@ end it "finds multiple by ids" do - Shape.find( *@shapes.map(&:id) ).should == @shapes + Shape.find(*@shapes.map(&:id)).should == @shapes end it "finds by criteria" do - Shape.find( :color => @shapes.last.color ).should == [@shapes.last] + Shape.find(:color => @shapes.last.color).should == [@shapes.last] end end @@ -37,19 +37,19 @@ it "errors when document missing" do lambda { - Shape.find!( BSON::ObjectId.new ) - }.should raise_error( MongoODM::Errors::DocumentNotFound ) + Shape.find!(BSON::ObjectId.new) + }.should raise_error(MongoODM::Errors::DocumentNotFound) end end describe "#find_or_initialize_by" do it "finds existing document" do - Shape.find_or_initialize_by( :color => @shapes.last.color ).should == @shapes.last + Shape.find_or_initialize_by(:color => @shapes.last.color).should == @shapes.last end it "initializes new document" do color = 'purple' - shape = Shape.find_or_initialize_by( :color => color ) + shape = Shape.find_or_initialize_by(:color => color) shape.color.should == color shape.should be_new_record end @@ -57,12 +57,12 @@ describe "#find_or_create_by" do it "finds existing document" do - Shape.find_or_create_by( :color => @shapes.last.color ).should == @shapes.last + Shape.find_or_create_by(:color => @shapes.last.color).should == @shapes.last end it "creates new document" do color = 'purple' - shape = Shape.find_or_create_by( :color => color ) + shape = Shape.find_or_create_by(:color => color) shape.color.should == color shape.should_not be_new_record end diff --git a/spec/mongo_odm/document/persistence_spec.rb b/spec/mongo_odm/document/persistence_spec.rb index 26b48fc..a75458f 100644 --- a/spec/mongo_odm/document/persistence_spec.rb +++ b/spec/mongo_odm/document/persistence_spec.rb @@ -4,7 +4,7 @@ describe MongoODM::Document::Persistence do describe "#save" do it "saves a document" do - shape = Shape.new( :color => 'magenta' ) + shape = Shape.new(:color => 'magenta') shape.save.should == shape shape.id.should_not be_nil end @@ -18,13 +18,13 @@ class TestDocument < Shape it "saves a document" do doc = TestDocument.new :color => 'blue' doc.should be_valid - proc { doc.save! }.should_not raise_error(MongoODM::Errors::Validation) + proc { doc.save! }.should_not raise_error(MongoODM::Errors::Validation) end it "raises error on invalid document" do doc = TestDocument.new :color => nil doc.should_not be_valid - proc { doc.save! }.should raise_error(MongoODM::Errors::Validation) + proc { doc.save! }.should raise_error(MongoODM::Errors::Validation) end end end \ No newline at end of file diff --git a/spec/mongo_odm/mongo_odm_spec.rb b/spec/mongo_odm/mongo_odm_spec.rb index 8a937f1..e7e1d11 100644 --- a/spec/mongo_odm/mongo_odm_spec.rb +++ b/spec/mongo_odm/mongo_odm_spec.rb @@ -7,7 +7,7 @@ context "when connection does not exist" do before do - conn = Mongo::Connection.new( nil, nil, :connect => false ) + conn = Mongo::Connection.new(nil, nil, :connect => false) Mongo::Connection.stub(:new).and_return(conn) MongoODM.connection = nil end @@ -20,7 +20,7 @@ context "when connection already exists" do before do - conn = Mongo::Connection.new( nil, nil, :connect => false ) + conn = Mongo::Connection.new(nil, nil, :connect => false) Mongo::Connection.stub(:new).and_return(conn) @connection = MongoODM.connection end @@ -28,14 +28,14 @@ it "returns the current Mongo::Connection instance" do MongoODM.connection.should == @connection end - + it "returns a different Mongo::Connection instance per thread" do thread = Thread.new { @connection.should_not == MongoODM.connection } thread.join end - + it "returns a different Mongo::Connection when configuration changes" do - MongoODM.config = {:port => 9000} + MongoODM.config = {:port => 9000 } MongoODM.connection.should_not == @connection end From c01f3b6dafe577be7a34cd8193f60ba0f0e237e8 Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Thu, 17 Mar 2011 23:54:12 -0700 Subject: [PATCH 17/56] Refactored instanciate. --- lib/mongo_odm.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/mongo_odm.rb b/lib/mongo_odm.rb index 8639505..4549c81 100644 --- a/lib/mongo_odm.rb +++ b/lib/mongo_odm.rb @@ -40,12 +40,13 @@ def self.config=(value) end def self.instanciate(value) - return value if value.is_a?(MongoODM::Document) - if value.is_a?(Hash) - klass = value["_class"] || value[:_class] - return klass.constantize.new(value) if klass + if value.is_a?(MongoODM::Document) + value + elsif value.is_a?(Hash) && (klass = value["_class"] || value[:_class]) + klass.constantize.new(value) + else + value.class.type_cast(value.to_mongo) end - value.class.type_cast(value.to_mongo) end def self.dereference(value) From f941ed5006860d66b22c387c3d43fff52494c4b4 Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Fri, 18 Mar 2011 10:56:21 -0700 Subject: [PATCH 18/56] Refactored Document#to_dbref to Referable module. --- lib/mongo_odm/document.rb | 2 ++ lib/mongo_odm/document/persistence.rb | 4 ---- lib/mongo_odm/document/referable.rb | 17 +++++++++++++++++ spec/mongo_odm/document/referable_spec.rb | 11 +++++++++++ 4 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 lib/mongo_odm/document/referable.rb create mode 100644 spec/mongo_odm/document/referable_spec.rb diff --git a/lib/mongo_odm/document.rb b/lib/mongo_odm/document.rb index b463671..bb38bb2 100644 --- a/lib/mongo_odm/document.rb +++ b/lib/mongo_odm/document.rb @@ -13,6 +13,7 @@ module Document autoload :Finders autoload :Inspect autoload :Persistence + autoload :Referable autoload :Timestamps autoload :Validations autoload :Equality @@ -31,6 +32,7 @@ module Document include Document::Fields include Document::Inspect include Document::Callbacks + include Document::Referable include Document::Validations include Document::Equality end diff --git a/lib/mongo_odm/document/persistence.rb b/lib/mongo_odm/document/persistence.rb index 2ae10e7..9b86040 100644 --- a/lib/mongo_odm/document/persistence.rb +++ b/lib/mongo_odm/document/persistence.rb @@ -67,10 +67,6 @@ def to_mongo attrs end end - - def to_dbref - BSON::DBRef.new(self.class.collection.name, _id) - end end module ClassMethods diff --git a/lib/mongo_odm/document/referable.rb b/lib/mongo_odm/document/referable.rb new file mode 100644 index 0000000..c8d329b --- /dev/null +++ b/lib/mongo_odm/document/referable.rb @@ -0,0 +1,17 @@ +# encoding: utf-8 + +module MongoODM + autoload :Reference + + module Document + module Referable + extend ActiveSupport::Concern + + module InstanceMethods + def to_dbref + BSON::DBRef.new(self.class.collection.name, _id) + end + end + end + end +end diff --git a/spec/mongo_odm/document/referable_spec.rb b/spec/mongo_odm/document/referable_spec.rb new file mode 100644 index 0000000..564ed67 --- /dev/null +++ b/spec/mongo_odm/document/referable_spec.rb @@ -0,0 +1,11 @@ +# encoding: utf-8 +require "spec_helper" + +describe MongoODM::Document::Referable do + it "#to_dbref" do + dbref = Shape.new.save.to_dbref + dbref.should be_a(BSON::DBRef) + dbref.namespace.should == Shape.collection.name + dbref.object_id.should be_a(BSON::ObjectId) + end +end \ No newline at end of file From 31ff193a784838e8c53f3925417b25d4229003ad Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Tue, 28 Dec 2010 23:37:29 -0800 Subject: [PATCH 19/56] Added Reference type. --- lib/mongo_odm.rb | 2 +- lib/mongo_odm/document/referable.rb | 7 +++ lib/mongo_odm/reference.rb | 72 +++++++++++++++++++++++ spec/mongo_odm/document/referable_spec.rb | 8 +++ spec/mongo_odm/reference_spec.rb | 58 ++++++++++++++++++ 5 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 lib/mongo_odm/reference.rb create mode 100644 spec/mongo_odm/reference_spec.rb diff --git a/lib/mongo_odm.rb b/lib/mongo_odm.rb index 4549c81..0586892 100644 --- a/lib/mongo_odm.rb +++ b/lib/mongo_odm.rb @@ -40,7 +40,7 @@ def self.config=(value) end def self.instanciate(value) - if value.is_a?(MongoODM::Document) + if value.is_a?(MongoODM::Document) || value.is_a?(MongoODM::Reference) value elsif value.is_a?(Hash) && (klass = value["_class"] || value[:_class]) klass.constantize.new(value) diff --git a/lib/mongo_odm/document/referable.rb b/lib/mongo_odm/document/referable.rb index c8d329b..fdc01ea 100644 --- a/lib/mongo_odm/document/referable.rb +++ b/lib/mongo_odm/document/referable.rb @@ -8,9 +8,16 @@ module Referable extend ActiveSupport::Concern module InstanceMethods + Reference = MongoODM::Reference + def to_dbref BSON::DBRef.new(self.class.collection.name, _id) end + + def reference + Reference.new self.to_dbref + end + alias ref reference end end end diff --git a/lib/mongo_odm/reference.rb b/lib/mongo_odm/reference.rb new file mode 100644 index 0000000..3020517 --- /dev/null +++ b/lib/mongo_odm/reference.rb @@ -0,0 +1,72 @@ +# encoding: utf-8 + +module MongoODM + class Reference + attr_reader :ref + alias to_dbref ref + + def initialize(attr = {}) + @ref, @target = case attr + when Reference then [attr.ref, attr.target] + when BSON::DBRef then [attr, nil] + when Document then [nil, attr] + else + raise "Can only reference object of type Reference, DBRef, or Document, not #{attr.class.inspect}" + end + end + + def target + @target ||= @ref.dereference + end + alias dereference target + + def reload + @target = nil + self + end + + # Object instance methods: + # :class, :clone, :dup, :enum_for, :instance_exec, :instance_of?, :instance_variable_defined?, :instance_variable_get, + # :instance_variable_set, :instance_variables, :method, :methods, :nil?, :object_id, :private_methods, :protected_methods, + # :public_method, :public_methods, :public_send, :respond_to_missing?, :singleton_class, :singleton_methods, :taint, + # :tainted?, :tap, :to_enum, :trust, :untaint, :untrust, :untrusted?, :equal? + + delegate :!=, :!~, :<=>, :===, :=~, :eql?, :to => :target + #delegate :inspect, :to_s, :instance_eval, :hash, :is_a?, :kind_of?, :respond_to?, :__send__, :to => :target + delegate :send, :to => :target + def method_missing(method, *args) + target.send(method, *args) + end + + def eql?(that) + if that.is_a?(BSON::DBRef) + that.eql?(@ref) + elsif that.is_a?(Reference) + that.ref.eql?(@ref) + elsif that.respond_to?(:to_dbref) + that.to_dbref.eql?(@ref) + else + that.eql?(self) + end + end + alias :== :eql? + + def to_mongo + @ref ||= @target && @target.to_dbref + end + + def self.type_cast(value) + return value if value.is_a?(MongoODM::Reference) + return MongoODM::Reference.new(value) if value.is_a?(BSON::DBRef) + return MongoODM::Reference.new(value.to_dbref) if value.respond_to?(:to_dbref) + nil + end + end +end + +# Override DBRef to always instantiate as a Reference +class BSON::DBRef + def self.type_cast(value) + MongoODM::Reference.type_cast(value) + end +end diff --git a/spec/mongo_odm/document/referable_spec.rb b/spec/mongo_odm/document/referable_spec.rb index 564ed67..5ef824d 100644 --- a/spec/mongo_odm/document/referable_spec.rb +++ b/spec/mongo_odm/document/referable_spec.rb @@ -8,4 +8,12 @@ dbref.namespace.should == Shape.collection.name dbref.object_id.should be_a(BSON::ObjectId) end + + it "#reference" do + shape = Shape.new.save + ref = shape.ref + ref.should be_a(Shape::Reference) + ref.target.should == shape + ref.to_dbref.should == shape.to_dbref + end end \ No newline at end of file diff --git a/spec/mongo_odm/reference_spec.rb b/spec/mongo_odm/reference_spec.rb new file mode 100644 index 0000000..921bda4 --- /dev/null +++ b/spec/mongo_odm/reference_spec.rb @@ -0,0 +1,58 @@ +# encoding: utf-8 +require 'spec_helper' + +describe MongoODM::Reference do + class TestDocument + include MongoODM::Document + field :thing, Reference # has one + field :things, Array # has many + end + + before do + @doc = TestDocument.new + @shape = Shape.new(:color => 'red').save + end + + it "refers on fields assignment" do + @doc.thing = @shape + @doc.thing.should be_kind_of(MongoODM::Reference) + end + + it "#to_mongo" do + @shape.reference.to_mongo.to_hash.should == BSON::DBRef.new('shapes', @shape.id).to_hash + end + + it "has one" do + @doc.thing = @shape + @doc.save + @doc.reload + + @doc.thing.target.should == @shape + @doc.thing.should == @shape + @doc.thing.color.should == @shape.color + end + + it "has many" do + shapes = [ @shape, Shape.new(:color => 'green').save, Shape.new(:color => 'blue').save ] + @doc.things = shapes.map(&:reference) + @doc.save + @doc.reload + + @doc.things.each.with_index do |thing, idx| + thing.target.should == shapes[idx] + thing.color.should == shapes[idx].color + end + end + + it "has many (mixed)" do + shapes = [ @shape, Shape.new(:color => 'green').save.reference, Shape.new(:color => 'blue').save ] + @doc.things = shapes + @doc.save + @doc.reload + + @doc.things[1].should be_a(Shape::Reference) + @doc.things.each.with_index do |thing, idx| + thing.color.should == shapes[idx].color + end + end +end \ No newline at end of file From 3bdbca6ffd80be8378c427ac40d8589e1e083ae5 Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Thu, 24 Mar 2011 11:05:25 -0700 Subject: [PATCH 20/56] Added spec for Timestamp. --- lib/mongo_odm/document/timestamps.rb | 2 +- spec/mongo_odm/document/timestamps_spec.rb | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/mongo_odm/document/timestamps.rb b/lib/mongo_odm/document/timestamps.rb index 2b818dd..b8c5265 100644 --- a/lib/mongo_odm/document/timestamps.rb +++ b/lib/mongo_odm/document/timestamps.rb @@ -13,7 +13,7 @@ module Timestamps module InstanceMethods def set_timestamps - now = Time.now.utc + now = Time.now self.created_at ||= now if self.new_record? self.updated_at = now end diff --git a/spec/mongo_odm/document/timestamps_spec.rb b/spec/mongo_odm/document/timestamps_spec.rb index 3ec015e..d7c926c 100644 --- a/spec/mongo_odm/document/timestamps_spec.rb +++ b/spec/mongo_odm/document/timestamps_spec.rb @@ -2,5 +2,17 @@ require "spec_helper" describe MongoODM::Document::Timestamps do + class TimestampedShape < Shape + include MongoODM::Document::Timestamps + end + it "serializes" do + now = Time.now + Time.stub!(:now).and_return(now) + + shape = TimestampedShape.new.save! + shape.created_at.should == shape.updated_at + shape.created_at.should be_within(0.001).of(now) + Shape.find(shape.id).should == shape + end end \ No newline at end of file From 74a08aa45e6a5b553093c0b87c685c234db141c0 Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Thu, 24 Mar 2011 11:05:42 -0700 Subject: [PATCH 21/56] Automatically include railtie if Rails defined. --- lib/mongo_odm.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/mongo_odm.rb b/lib/mongo_odm.rb index 0586892..e5e3ed0 100644 --- a/lib/mongo_odm.rb +++ b/lib/mongo_odm.rb @@ -54,3 +54,8 @@ def self.dereference(value) end end + +if defined?(Rails) + require 'mongo_odm/railtie' +end + From 1dfa2a467414b4e195bfbcca9df6062560a3ea15 Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Thu, 24 Mar 2011 11:06:23 -0700 Subject: [PATCH 22/56] Added rounding of Time value to fix serialization. BSON serializes Time values by rounding to the nearest millisecond. To keep the pre-serialized value the same as post-serialized, round the value on assignment. --- lib/mongo_odm/core_ext/conversions.rb | 51 ++++++++++++++------------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/lib/mongo_odm/core_ext/conversions.rb b/lib/mongo_odm/core_ext/conversions.rb index 647d8dd..8ccaefb 100644 --- a/lib/mongo_odm/core_ext/conversions.rb +++ b/lib/mongo_odm/core_ext/conversions.rb @@ -20,7 +20,7 @@ def self.type_cast(value) return value if value.is_a?(BSON::ObjectId) BSON::ObjectId(value) end - + def to_mongo self end @@ -33,19 +33,19 @@ def self.type_cast(value) return value.to_dbref if value.respond_to?(:to_dbref) nil end - + def to_mongo self end - + def dereference MongoODM.instanciate(MongoODM.database.dereference(self)) end - + def inspect "BSON::DBRef(namespace:\"#{namespace}\", id: \"#{object_id}\")" end - + def eql?(other) to_hash == other.to_hash end @@ -58,11 +58,11 @@ def self.type_cast(value) return nil if value.nil? value.to_a.map {|elem| MongoODM.instanciate(elem)} end - + def to_mongo self.map {|elem| elem.to_mongo} end - + def dereference MongoODM.instanciate(self.map{|value| MongoODM.dereference(value)}) end @@ -74,7 +74,7 @@ def self.type_cast(value) return nil if value.nil? value.to_s.constantize end - + def to_mongo self.name end @@ -90,7 +90,7 @@ def self.type_cast(value) else value.inspect.intern end end - + def to_mongo self end @@ -102,7 +102,7 @@ def self.type_cast(value) return nil if value.nil? value.to_i end - + def to_mongo self end @@ -114,7 +114,7 @@ def self.type_cast(value) return nil if value.nil? value.to_f end - + def to_mongo self end @@ -126,7 +126,7 @@ def self.type_cast(value) return nil if value.nil? value.is_a?(BigDecimal) ? value : new(value.to_s) end - + def to_mongo self.to_s end @@ -141,7 +141,7 @@ def self.type_cast(value) else value.inspect end end - + def to_mongo self end @@ -153,7 +153,7 @@ def self.type_cast(value) return nil if value.nil? value.to_date end - + def to_mongo Time.utc(self.year, self.month, self.day) end @@ -165,7 +165,7 @@ def self.type_cast(value) return nil if value.nil? value.to_datetime end - + def to_mongo datetime = self.utc Time.utc(datetime.year, datetime.month, datetime.day, datetime.hour, datetime.min, datetime.sec) @@ -190,7 +190,7 @@ def self.type_cast(value) return nil if value.nil? false end - + def to_mongo self end @@ -200,11 +200,14 @@ def to_mongo class Time def self.type_cast(value) return nil if value.nil? - value.to_time + + # BSON rounds times to milliseconds + # (note: bson_ext rounds, bson truncates) + ::Time.at(value.to_time.to_f.round(3)) end - + def to_mongo - self.utc + self.dup.utc end end @@ -245,11 +248,11 @@ def self.type_cast(value) return nil if value.nil? Hash[value.to_hash.map{|k,v| [MongoODM.instanciate(k), MongoODM.instanciate(v)]}] end - + def to_mongo Hash[self.map{|k,v| [k.to_mongo, v.to_mongo]}] end - + def dereference Hash[self.map{|k,v| [MongoODM.instanciate(MongoODM.dereference(k)), MongoODM.instanciate(MongoODM.dereference(v))]}] end @@ -260,7 +263,7 @@ class HashWithIndifferentAccess def self.type_cast(value) Hash.type_cast(value).with_indifferent_access end - + def to_mongo Hash[self.map{|k,v| [k.to_mongo, v.to_mongo]}] end @@ -272,7 +275,7 @@ def self.type_cast(value) return nil if value.nil? new(value) end - + def to_mongo self end @@ -283,7 +286,7 @@ class NilClass def self.type_cast(value) nil end - + def to_mongo nil end From a5117c70049df113cf6ae3adfb0ce003e5863083 Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Thu, 24 Mar 2011 11:09:05 -0700 Subject: [PATCH 23/56] Fixed #sort/#skip/#limit in finders. --- lib/mongo_odm/document/finders.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/mongo_odm/document/finders.rb b/lib/mongo_odm/document/finders.rb index 6c5cb45..a7838ac 100644 --- a/lib/mongo_odm/document/finders.rb +++ b/lib/mongo_odm/document/finders.rb @@ -39,15 +39,15 @@ def find_or_create_by(attr) def sort(key_or_list, direction = nil) order = key_or_list.is_a?(Array) ? key_or_list : direction.nil? ? [key_or_list, :asc] : [key_or_list, direction] - where(:sort => order) + MongoODM::Criteria.new(self, :sort => order) end def skip(number_to_skip = nil) - where(:skip => number_to_skip) + MongoODM::Criteria.new(self, :skip => number_to_skip) end def limit(number_to_return = nil) - where(:limit => number_to_return) + MongoODM::Criteria.new(self, :limit => number_to_return) end def cursor From e102039b2d2214c6631be43d4cfb628e158afb33 Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Thu, 24 Mar 2011 11:09:46 -0700 Subject: [PATCH 24/56] Document#reload now returns self. --- lib/mongo_odm/document/persistence.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/mongo_odm/document/persistence.rb b/lib/mongo_odm/document/persistence.rb index 9b86040..7d74616 100644 --- a/lib/mongo_odm/document/persistence.rb +++ b/lib/mongo_odm/document/persistence.rb @@ -27,6 +27,7 @@ def persisted? def reload self.load_attributes_or_defaults(self.class.find_one(:_id => id).attributes) unless new_record? + self end # Save a document to its collection. From a135c915605fe59a3548b1fabda7365450eb102f Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Thu, 24 Mar 2011 11:11:13 -0700 Subject: [PATCH 25/56] Document finders now properly reuse/rewind cursor. --- lib/mongo_odm/document/finders.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/mongo_odm/document/finders.rb b/lib/mongo_odm/document/finders.rb index a7838ac..83d0f7d 100644 --- a/lib/mongo_odm/document/finders.rb +++ b/lib/mongo_odm/document/finders.rb @@ -50,12 +50,17 @@ def limit(number_to_return = nil) MongoODM::Criteria.new(self, :limit => number_to_return) end - def cursor - @cursor ||= find.to_cursor + def to_cursor + if @cursor + @cursor.rewind! + else + @cursor = where.to_cursor + end + @cursor end def method_missing(method_name, *args, &block) - if cursor.respond_to?(method_name) + if (cursor = to_cursor).respond_to?(method_name) cursor.send(method_name, *args, &block) else super From 6d6191d8a521778e0790293ffd3840f61c11d297 Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Thu, 24 Mar 2011 11:43:11 -0700 Subject: [PATCH 26/56] Removed redundant validation from #save!. --- lib/mongo_odm/document/persistence.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mongo_odm/document/persistence.rb b/lib/mongo_odm/document/persistence.rb index 7d74616..9e58c59 100644 --- a/lib/mongo_odm/document/persistence.rb +++ b/lib/mongo_odm/document/persistence.rb @@ -47,7 +47,7 @@ def save(options = {}) def save!(options = {}) valid? or raise(Errors::Validation, self) - save(options) + save(options.merge(:validate => false)) end def update_attributes(attributes) From 4cc4090426d6935359cf291b5cf048aa49305433 Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Thu, 24 Mar 2011 11:43:46 -0700 Subject: [PATCH 27/56] Fixed Validation exception. --- lib/mongo_odm/errors.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/mongo_odm/errors.rb b/lib/mongo_odm/errors.rb index a8ea40b..11f7903 100644 --- a/lib/mongo_odm/errors.rb +++ b/lib/mongo_odm/errors.rb @@ -29,9 +29,8 @@ def initialize(ids, klass) end class Validation < Error - attr_reader :document def initialize(doc) - @document = doc + super "validation failure: #{doc.errors.full_messages}" end end end From 46df891d3e723f3b9cdf6c89d11a37c0f0a31c56 Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Thu, 24 Mar 2011 11:44:37 -0700 Subject: [PATCH 28/56] Refactored Criteria to construct cursor lazily and reuse, added #reload. --- lib/mongo_odm/criteria.rb | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/lib/mongo_odm/criteria.rb b/lib/mongo_odm/criteria.rb index e8353c6..a5363bd 100644 --- a/lib/mongo_odm/criteria.rb +++ b/lib/mongo_odm/criteria.rb @@ -12,7 +12,7 @@ def initialize(klass, options = {}) @sort = options[:sort] || [] @limit = options[:limit] || nil @skip = options[:skip] || nil - _set_cursor + reload end def ==(other) @@ -29,15 +29,20 @@ def ==(other) end def to_cursor + if @cursor + @cursor.rewind! + else + @cursor = @klass.collection.find(@selector, @opts) + @cursor = @cursor.sort(@sort) unless @sort.blank? + @cursor = @cursor.limit(@limit) unless @limit.blank? + @cursor = @cursor.skip(@skip) unless @skip.blank? + end @cursor end - def _set_cursor - @cursor = @klass.collection.find(@selector, @opts) - @cursor = @cursor.sort(@sort) unless @sort.blank? - @cursor = @cursor.limit(@limit) unless @limit.blank? - @cursor = @cursor.skip(@skip) unless @skip.blank? - @cursor + def reload + @cursor = nil + self end def _merge_criteria(criteria) @@ -46,16 +51,15 @@ def _merge_criteria(criteria) @sort << criteria.instance_variable_get(:@sort) unless criteria.instance_variable_get(:@sort).blank? @limit = criteria.instance_variable_get(:@limit) unless criteria.instance_variable_get(:@limit).blank? @skip = criteria.instance_variable_get(:@skip) unless criteria.instance_variable_get(:@skip).blank? - _set_cursor - self + reload end def method_missing(method_name, *args, &block) if @klass.respond_to?(method_name) result = @klass.send(method_name, *args, &block) result.is_a?(Criteria) ? _merge_criteria(result) : result - elsif @cursor.respond_to?(method_name) - @cursor.send(method_name, *args, &block) + elsif (cursor = to_cursor).respond_to?(method_name) + cursor.send(method_name, *args, &block) elsif [].respond_to?(method_name) to_a.send(method_name, *args, &block) else From d0a06e6fc7489eec7dad527698620e827af1cf86 Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Thu, 24 Mar 2011 11:45:25 -0700 Subject: [PATCH 29/56] Don't delegate Criteria#inspect. --- lib/mongo_odm/criteria.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mongo_odm/criteria.rb b/lib/mongo_odm/criteria.rb index a5363bd..f8ade79 100644 --- a/lib/mongo_odm/criteria.rb +++ b/lib/mongo_odm/criteria.rb @@ -3,7 +3,7 @@ module MongoODM class Criteria delegate :to_a, :count, :collect, :map, :each, :all?, :include?, :to => :to_cursor - delegate :inspect, :to_xml, :to_yaml, :to_json, :include?, :length, :to => :to_a + delegate :to_xml, :to_yaml, :to_json, :include?, :length, :to => :to_a def initialize(klass, options = {}) @klass = klass From ad0b4e5fe1a7ee0ea891a13c75a2b45119afd20b Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Thu, 24 Mar 2011 11:47:51 -0700 Subject: [PATCH 30/56] Added #referenced_in for associations. --- lib/mongo_odm/document/referable.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lib/mongo_odm/document/referable.rb b/lib/mongo_odm/document/referable.rb index fdc01ea..22fab16 100644 --- a/lib/mongo_odm/document/referable.rb +++ b/lib/mongo_odm/document/referable.rb @@ -7,6 +7,28 @@ module Document module Referable extend ActiveSupport::Concern + module ClassMethods + def referenced_in(model, field = nil, opts = {}) + model = model.to_s.camelize.constantize + + n = self.name.underscore + field_name = field || + (model.fields.include?(f = n.singularize.to_sym) && f) || + (model.fields.include?(f = n.pluralize.to_sym) && f) + raise "Missing field" unless field_name + raise(UnknownFieldError, field_name, model) unless model.fields.include?(field_name) + + method_name = opts[:as] || model.to_s.underscore.pluralize + raise "Field \"#{method_name}\" already defined in #{self.name}" if method_defined?(method_name.to_sym) + + self.class_eval <<-EOT + def #{method_name} + #{model}.where('#{field_name}' => self.to_dbref ) + end + EOT + end + end + module InstanceMethods Reference = MongoODM::Reference From dd5e722d6d9a7615f8e01c19a3a9a2b5f6aef23a Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Thu, 24 Mar 2011 18:11:41 -0700 Subject: [PATCH 31/56] Fixed Criteria to call #to_mongo on selector. --- lib/mongo_odm/criteria.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mongo_odm/criteria.rb b/lib/mongo_odm/criteria.rb index f8ade79..9feb9fe 100644 --- a/lib/mongo_odm/criteria.rb +++ b/lib/mongo_odm/criteria.rb @@ -7,7 +7,7 @@ class Criteria def initialize(klass, options = {}) @klass = klass - @selector = options[:selector] || {} + @selector = options[:selector].to_mongo || {} @opts = options[:opts] || {} @sort = options[:sort] || [] @limit = options[:limit] || nil From d3f6a70fc869f770196cf39a183f934217d63fbd Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Tue, 29 Mar 2011 17:03:48 -0700 Subject: [PATCH 32/56] Fixed Document#find! when passed string IDs. --- lib/mongo_odm/document/finders.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/mongo_odm/document/finders.rb b/lib/mongo_odm/document/finders.rb index 83d0f7d..0ef628e 100644 --- a/lib/mongo_odm/document/finders.rb +++ b/lib/mongo_odm/document/finders.rb @@ -20,10 +20,11 @@ def find(*args) end def find!(*ids) + ids.map! { |id| id.is_a?(BSON::ObjectId) ? id : BSON::ObjectId(id) } documents = Array(find(*ids)) found = documents.map(&:id) missing = ids.reject { |id| found.include?(id) } - missing.empty? or raise Errors::DocumentNotFound.new(self.class, missing) + missing.empty? or raise Errors::DocumentNotFound.new(missing, self.class) (documents.count == 1) ? documents.first : documents end From f5588d8a967cfde322bb1b0daf5283b8faf37008 Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Tue, 29 Mar 2011 17:04:20 -0700 Subject: [PATCH 33/56] Force Reference#as_json to delegate. --- lib/mongo_odm/reference.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mongo_odm/reference.rb b/lib/mongo_odm/reference.rb index 3020517..2280463 100644 --- a/lib/mongo_odm/reference.rb +++ b/lib/mongo_odm/reference.rb @@ -33,7 +33,7 @@ def reload delegate :!=, :!~, :<=>, :===, :=~, :eql?, :to => :target #delegate :inspect, :to_s, :instance_eval, :hash, :is_a?, :kind_of?, :respond_to?, :__send__, :to => :target - delegate :send, :to => :target + delegate :send, :as_json, :to => :target def method_missing(method, *args) target.send(method, *args) end From 1903df16fd92056b6965eb86ae32144174ad0b0f Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Mon, 11 Apr 2011 16:05:16 -0700 Subject: [PATCH 34/56] Removed version specs from gem dependencies, updated mongo/bson_ext. These should only exist for gems where the version really matters. --- Gemfile | 20 +++--- Gemfile.lock | 29 ++++----- mongo_odm.gemspec | 154 +++++++++++++++++++++++++++------------------- 3 files changed, 117 insertions(+), 86 deletions(-) diff --git a/Gemfile b/Gemfile index 45657d3..71e84e8 100644 --- a/Gemfile +++ b/Gemfile @@ -1,20 +1,20 @@ # Use `bundle install` in order to install these gems source 'http://rubygems.org' -gem 'activesupport', '~> 3.0.5' -gem 'activemodel', '~> 3.0.5' -gem 'mongo', '~> 1.2.4' -gem 'bson_ext', '~> 1.2.4' -gem 'tzinfo', '~> 0.3.24' +gem 'activesupport' +gem 'activemodel' +gem 'mongo' +gem 'bson_ext' +gem 'tzinfo' group :development do gem 'autotest' gem 'database_cleaner' - gem 'jeweler', '~> 1.5.2' - gem 'rcov', '~> 0.9.9' - gem 'rspec', '~> 2.5.0' - gem 'yard', '~> 0.6.5' - gem 'watchr', '~> 0.7' + gem 'jeweler' + gem 'rcov' + gem 'rspec' + gem 'yard' + gem 'watchr' if RUBY_VERSION =~ /1.9/ gem 'ruby-debug19', :require => 'ruby-debug' diff --git a/Gemfile.lock b/Gemfile.lock index fcdb897..298f1c8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -9,8 +9,8 @@ GEM activesupport (3.0.5) autotest (4.4.6) ZenTest (>= 4.4.1) - bson (1.2.4) - bson_ext (1.2.4) + bson (1.3.0) + bson_ext (1.3.0) builder (2.1.2) database_cleaner (0.6.5) diff-lcs (1.1.2) @@ -20,8 +20,8 @@ GEM bundler (~> 1.0.0) git (>= 1.2.5) rake - mongo (1.2.4) - bson (>= 1.2.4) + mongo (1.3.0) + bson (>= 1.3.0) rake (0.8.7) rcov (0.9.9) rspec (2.5.0) @@ -40,15 +40,16 @@ PLATFORMS ruby DEPENDENCIES - activemodel (~> 3.0.5) - activesupport (~> 3.0.5) + activemodel + activesupport autotest - bson_ext (~> 1.2.4) + bson_ext database_cleaner - jeweler (~> 1.5.2) - mongo (~> 1.2.4) - rcov (~> 0.9.9) - rspec (~> 2.5.0) - tzinfo (~> 0.3.24) - watchr (~> 0.7) - yard (~> 0.6.5) + jeweler + mongo + rcov + rspec + ruby-debug19 + tzinfo + watchr + yard diff --git a/mongo_odm.gemspec b/mongo_odm.gemspec index e3ad47c..beab00b 100644 --- a/mongo_odm.gemspec +++ b/mongo_odm.gemspec @@ -9,7 +9,7 @@ Gem::Specification.new do |s| s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["Carlos Paramio"] - s.date = %q{2011-03-30} + s.date = %q{2011-04-11} s.description = %q{mongo_odm is a flexible persistence module for any Ruby class to MongoDB.} s.email = %q{carlos@evolve.st} s.extra_rdoc_files = [ @@ -41,14 +41,18 @@ Gem::Specification.new do |s| "lib/mongo_odm/document/callbacks.rb", "lib/mongo_odm/document/equality.rb", "lib/mongo_odm/document/fields.rb", + "lib/mongo_odm/document/finders.rb", "lib/mongo_odm/document/indexes.rb", "lib/mongo_odm/document/inspect.rb", "lib/mongo_odm/document/persistence.rb", + "lib/mongo_odm/document/referable.rb", "lib/mongo_odm/document/timestamps.rb", "lib/mongo_odm/document/validations.rb", "lib/mongo_odm/document/validations/uniqueness_validator.rb", "lib/mongo_odm/errors.rb", "lib/mongo_odm/railtie.rb", + "lib/mongo_odm/railties/database.rake", + "lib/mongo_odm/reference.rb", "lib/mongo_odm/version.rb", "spec/models/00-blank_slate.rb", "spec/models/01-shape.rb", @@ -57,6 +61,7 @@ Gem::Specification.new do |s| "spec/mongo_odm/collection_spec.rb", "spec/mongo_odm/config_spec.rb", "spec/mongo_odm/core_ext/conversions_spec.rb", + "spec/mongo_odm/criteria_spec.rb", "spec/mongo_odm/cursor_spec.rb", "spec/mongo_odm/document/attribute_methods/dirty_spec.rb", "spec/mongo_odm/document/attribute_methods/localization_spec.rb", @@ -66,17 +71,20 @@ Gem::Specification.new do |s| "spec/mongo_odm/document/attribute_methods_spec.rb", "spec/mongo_odm/document/callbacks_spec.rb", "spec/mongo_odm/document/fields_spec.rb", + "spec/mongo_odm/document/finders_spec.rb", "spec/mongo_odm/document/inspect_spec.rb", "spec/mongo_odm/document/persistence_spec.rb", + "spec/mongo_odm/document/referable_spec.rb", "spec/mongo_odm/document/timestamps_spec.rb", "spec/mongo_odm/document/validations_spec.rb", "spec/mongo_odm/document_spec.rb", "spec/mongo_odm/mongo_odm_spec.rb", + "spec/mongo_odm/reference_spec.rb", "spec/spec_helper.rb" ] s.homepage = %q{http://github.com/carlosparamio/mongo_odm} s.require_paths = ["lib"] - s.rubygems_version = %q{1.5.2} + s.rubygems_version = %q{1.7.2} s.summary = %q{mongo_odm is a flexible persistence module for any Ruby class to MongoDB.} s.test_files = [ "spec/models/00-blank_slate.rb", @@ -86,6 +94,7 @@ Gem::Specification.new do |s| "spec/mongo_odm/collection_spec.rb", "spec/mongo_odm/config_spec.rb", "spec/mongo_odm/core_ext/conversions_spec.rb", + "spec/mongo_odm/criteria_spec.rb", "spec/mongo_odm/cursor_spec.rb", "spec/mongo_odm/document/attribute_methods/dirty_spec.rb", "spec/mongo_odm/document/attribute_methods/localization_spec.rb", @@ -95,12 +104,15 @@ Gem::Specification.new do |s| "spec/mongo_odm/document/attribute_methods_spec.rb", "spec/mongo_odm/document/callbacks_spec.rb", "spec/mongo_odm/document/fields_spec.rb", + "spec/mongo_odm/document/finders_spec.rb", "spec/mongo_odm/document/inspect_spec.rb", "spec/mongo_odm/document/persistence_spec.rb", + "spec/mongo_odm/document/referable_spec.rb", "spec/mongo_odm/document/timestamps_spec.rb", "spec/mongo_odm/document/validations_spec.rb", "spec/mongo_odm/document_spec.rb", "spec/mongo_odm/mongo_odm_spec.rb", + "spec/mongo_odm/reference_spec.rb", "spec/spec_helper.rb" ] @@ -108,69 +120,87 @@ Gem::Specification.new do |s| s.specification_version = 3 if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then - s.add_runtime_dependency(%q, ["~> 3.0.5"]) - s.add_runtime_dependency(%q, ["~> 3.0.5"]) - s.add_runtime_dependency(%q, ["~> 1.2.4"]) - s.add_runtime_dependency(%q, ["~> 1.2.4"]) - s.add_runtime_dependency(%q, ["~> 0.3.24"]) - s.add_development_dependency(%q, ["~> 1.5.2"]) - s.add_development_dependency(%q, ["~> 0.6.5"]) - s.add_development_dependency(%q, ["~> 0.9.9"]) - s.add_development_dependency(%q, ["~> 2.5.0"]) - s.add_development_dependency(%q, ["~> 0.7"]) - s.add_runtime_dependency(%q, ["~> 3.0.5"]) - s.add_runtime_dependency(%q, ["~> 3.0.5"]) - s.add_runtime_dependency(%q, ["~> 1.2.4"]) - s.add_runtime_dependency(%q, ["~> 1.2.4"]) - s.add_runtime_dependency(%q, ["~> 0.3.24"]) - s.add_development_dependency(%q, ["~> 1.5.2"]) - s.add_development_dependency(%q, ["~> 0.6.5"]) - s.add_development_dependency(%q, ["~> 0.9.9"]) - s.add_development_dependency(%q, ["~> 2.5.0"]) - s.add_development_dependency(%q, ["~> 0.7"]) + s.add_runtime_dependency(%q, [">= 0"]) + s.add_runtime_dependency(%q, [">= 0"]) + s.add_runtime_dependency(%q, [">= 0"]) + s.add_runtime_dependency(%q, [">= 0"]) + s.add_runtime_dependency(%q, [">= 0"]) + s.add_development_dependency(%q, [">= 0"]) + s.add_development_dependency(%q, [">= 0"]) + s.add_development_dependency(%q, [">= 0"]) + s.add_development_dependency(%q, [">= 0"]) + s.add_development_dependency(%q, [">= 0"]) + s.add_development_dependency(%q, [">= 0"]) + s.add_development_dependency(%q, [">= 0"]) + s.add_development_dependency(%q, [">= 0"]) + s.add_runtime_dependency(%q, [">= 0"]) + s.add_runtime_dependency(%q, [">= 0"]) + s.add_runtime_dependency(%q, [">= 0"]) + s.add_runtime_dependency(%q, [">= 0"]) + s.add_runtime_dependency(%q, [">= 0"]) + s.add_development_dependency(%q, [">= 0"]) + s.add_development_dependency(%q, [">= 0"]) + s.add_development_dependency(%q, [">= 0"]) + s.add_development_dependency(%q, [">= 0"]) + s.add_development_dependency(%q, [">= 0"]) + s.add_development_dependency(%q, [">= 0"]) + s.add_development_dependency(%q, [">= 0"]) + s.add_development_dependency(%q, [">= 0"]) else - s.add_dependency(%q, ["~> 3.0.5"]) - s.add_dependency(%q, ["~> 3.0.5"]) - s.add_dependency(%q, ["~> 1.2.4"]) - s.add_dependency(%q, ["~> 1.2.4"]) - s.add_dependency(%q, ["~> 0.3.24"]) - s.add_dependency(%q, ["~> 1.5.2"]) - s.add_dependency(%q, ["~> 0.6.5"]) - s.add_dependency(%q, ["~> 0.9.9"]) - s.add_dependency(%q, ["~> 2.5.0"]) - s.add_dependency(%q, ["~> 0.7"]) - s.add_dependency(%q, ["~> 3.0.5"]) - s.add_dependency(%q, ["~> 3.0.5"]) - s.add_dependency(%q, ["~> 1.2.4"]) - s.add_dependency(%q, ["~> 1.2.4"]) - s.add_dependency(%q, ["~> 0.3.24"]) - s.add_dependency(%q, ["~> 1.5.2"]) - s.add_dependency(%q, ["~> 0.6.5"]) - s.add_dependency(%q, ["~> 0.9.9"]) - s.add_dependency(%q, ["~> 2.5.0"]) - s.add_dependency(%q, ["~> 0.7"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) end else - s.add_dependency(%q, ["~> 3.0.5"]) - s.add_dependency(%q, ["~> 3.0.5"]) - s.add_dependency(%q, ["~> 1.2.4"]) - s.add_dependency(%q, ["~> 1.2.4"]) - s.add_dependency(%q, ["~> 0.3.24"]) - s.add_dependency(%q, ["~> 1.5.2"]) - s.add_dependency(%q, ["~> 0.6.5"]) - s.add_dependency(%q, ["~> 0.9.9"]) - s.add_dependency(%q, ["~> 2.5.0"]) - s.add_dependency(%q, ["~> 0.7"]) - s.add_dependency(%q, ["~> 3.0.5"]) - s.add_dependency(%q, ["~> 3.0.5"]) - s.add_dependency(%q, ["~> 1.2.4"]) - s.add_dependency(%q, ["~> 1.2.4"]) - s.add_dependency(%q, ["~> 0.3.24"]) - s.add_dependency(%q, ["~> 1.5.2"]) - s.add_dependency(%q, ["~> 0.6.5"]) - s.add_dependency(%q, ["~> 0.9.9"]) - s.add_dependency(%q, ["~> 2.5.0"]) - s.add_dependency(%q, ["~> 0.7"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) end end From b7034c87fdcf17e6270f891a76a5f37330c1e3b0 Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Tue, 3 May 2011 15:50:18 -0700 Subject: [PATCH 35/56] Force Reference to delegate #to_param to target. --- lib/mongo_odm/criteria.rb | 4 ++-- lib/mongo_odm/reference.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/mongo_odm/criteria.rb b/lib/mongo_odm/criteria.rb index 5ccb467..5c3dd19 100644 --- a/lib/mongo_odm/criteria.rb +++ b/lib/mongo_odm/criteria.rb @@ -34,11 +34,11 @@ def reload def to_a @_result ||= @_cursor.rewind! && @_cursor.to_a end - + def to_cursor @_cursor end - + def _set_cursor @_cursor ||= @_klass.collection.find(@_selector, @_opts.dup) end diff --git a/lib/mongo_odm/reference.rb b/lib/mongo_odm/reference.rb index 2280463..7a567a7 100644 --- a/lib/mongo_odm/reference.rb +++ b/lib/mongo_odm/reference.rb @@ -33,7 +33,7 @@ def reload delegate :!=, :!~, :<=>, :===, :=~, :eql?, :to => :target #delegate :inspect, :to_s, :instance_eval, :hash, :is_a?, :kind_of?, :respond_to?, :__send__, :to => :target - delegate :send, :as_json, :to => :target + delegate :send, :as_json, :to_param, :to => :target def method_missing(method, *args) target.send(method, *args) end From b15a7ce0daf011b940e53eebe9278d47f804e80f Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Mon, 6 Jun 2011 21:00:01 -0700 Subject: [PATCH 36/56] Minor fix for Rails 3.1. --- lib/mongo_odm/railtie.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/mongo_odm/railtie.rb b/lib/mongo_odm/railtie.rb index c02f61b..3b1ac3f 100644 --- a/lib/mongo_odm/railtie.rb +++ b/lib/mongo_odm/railtie.rb @@ -1,7 +1,8 @@ module Rails #:nodoc: module MongoODM #:nodoc: class Railtie < Rails::Railtie #:nodoc: - config.generators.orm :mongo_odm, :migration => false + config.send( config.respond_to?(:app_generators) ? :app_generators : :generators ). + orm(:mongo_odm, :migration => false) rake_tasks do load 'mongo_odm/railties/database.rake' From 4242d5b973b04dad1a2a61a5041b37847135ad83 Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Tue, 7 Jun 2011 15:05:25 -0700 Subject: [PATCH 37/56] Fix for Document[attr]. --- lib/mongo_odm/document/attribute_methods/read.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mongo_odm/document/attribute_methods/read.rb b/lib/mongo_odm/document/attribute_methods/read.rb index 11ef1aa..faf8d44 100644 --- a/lib/mongo_odm/document/attribute_methods/read.rb +++ b/lib/mongo_odm/document/attribute_methods/read.rb @@ -24,7 +24,7 @@ def read_attribute(attr_name) end def [](attr_name) - read_attribute[attr_name] + read_attribute(attr_name) end def attribute(attr_name) From f925ba3a956ead3b3f531027317460876391b2d7 Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Wed, 8 Jun 2011 12:12:20 -0700 Subject: [PATCH 38/56] Fixed two cases of bad syntax on raising an exception. --- lib/mongo_odm/document/attribute_methods.rb | 2 +- lib/mongo_odm/document/referable.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/mongo_odm/document/attribute_methods.rb b/lib/mongo_odm/document/attribute_methods.rb index 02faab1..fb564ef 100644 --- a/lib/mongo_odm/document/attribute_methods.rb +++ b/lib/mongo_odm/document/attribute_methods.rb @@ -44,7 +44,7 @@ def attributes=(new_attributes, auto_generate_attributes = false) if respond_to?(:"#{name}=") send(:"#{name}=", value) else - auto_generate_attributes ? write_attribute(name, value) : raise(MongoODM::Errors::UnknownFieldError, name, self.class) + auto_generate_attributes ? write_attribute(name, value) : raise(MongoODM::Errors::UnknownFieldError.new(name, self.class)) end end end diff --git a/lib/mongo_odm/document/referable.rb b/lib/mongo_odm/document/referable.rb index 22fab16..8ebff0c 100644 --- a/lib/mongo_odm/document/referable.rb +++ b/lib/mongo_odm/document/referable.rb @@ -16,7 +16,7 @@ def referenced_in(model, field = nil, opts = {}) (model.fields.include?(f = n.singularize.to_sym) && f) || (model.fields.include?(f = n.pluralize.to_sym) && f) raise "Missing field" unless field_name - raise(UnknownFieldError, field_name, model) unless model.fields.include?(field_name) + raise(UnknownFieldError.new(field_name, model)) unless model.fields.include?(field_name) method_name = opts[:as] || model.to_s.underscore.pluralize raise "Field \"#{method_name}\" already defined in #{self.name}" if method_defined?(method_name.to_sym) From d2a75687baa12e5356a0af4c24d17620dbee43d9 Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Wed, 22 Jun 2011 12:38:49 -0700 Subject: [PATCH 39/56] Reference: delegate #id to DBRef instead of dereferencing. --- lib/mongo_odm/reference.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/mongo_odm/reference.rb b/lib/mongo_odm/reference.rb index 7a567a7..2866944 100644 --- a/lib/mongo_odm/reference.rb +++ b/lib/mongo_odm/reference.rb @@ -15,6 +15,10 @@ def initialize(attr = {}) end end + def id + @target ? @target.id : @ref.object_id + end + def target @target ||= @ref.dereference end From f4a55fef6522f94feb50024192883fd4e74c7b48 Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Fri, 1 Jul 2011 16:25:30 -0700 Subject: [PATCH 40/56] Fixed Criteria#count to delegate to cursor (instead of result array). --- lib/mongo_odm/criteria.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/mongo_odm/criteria.rb b/lib/mongo_odm/criteria.rb index 5c3dd19..6667fe1 100644 --- a/lib/mongo_odm/criteria.rb +++ b/lib/mongo_odm/criteria.rb @@ -4,7 +4,8 @@ module MongoODM class Criteria - delegate :to_xml, :to_yaml, :to_json, :include?, :first, :length, :count, :collect, :map, :each, :all?, :include?, :to => :to_a + delegate :to_xml, :to_yaml, :to_json, :include?, :first, :length, :collect, :map, :each, :all?, :include?, :to => :to_a + delegate :count, :to => :to_cursor def initialize(klass, selector = {}, opts = {}) @_klass = klass @@ -65,4 +66,4 @@ def method_missing(method_name, *args, &block) end -end \ No newline at end of file +end From 9b2f3a44d7b9535f8b44fda765faa3e7cd9a6b9b Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Thu, 7 Jul 2011 14:24:55 -0700 Subject: [PATCH 41/56] Fix for ActiveModel 3.1 API change. --- lib/mongo_odm/document/attribute_methods.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/mongo_odm/document/attribute_methods.rb b/lib/mongo_odm/document/attribute_methods.rb index fb564ef..66cedda 100644 --- a/lib/mongo_odm/document/attribute_methods.rb +++ b/lib/mongo_odm/document/attribute_methods.rb @@ -69,7 +69,8 @@ def remove_attribute(name) def method_missing(method_id, *args, &block) # If we haven't generated any methods yet, generate them, then # see if we've created the method we're looking for. - if !self.class.attribute_methods_generated? + method = self.class.respond_to?(:attribute_methods_generated?) ? :attribute_methods_generated? : :generated_attribute_methods + if !self.class.send(method) self.class.define_attribute_methods_for_fields method_name = method_id.to_s guard_private_attribute_method!(method_name, args) From bd16977e8509e1ddffed41f2a419a73c3c4410f0 Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Thu, 7 Jul 2011 14:25:27 -0700 Subject: [PATCH 42/56] Document#find can now take an Array of IDs and returns an Array of documents. --- lib/mongo_odm/document/finders.rb | 6 ++++-- spec/mongo_odm/document/finders_spec.rb | 5 ++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/mongo_odm/document/finders.rb b/lib/mongo_odm/document/finders.rb index 463ddfe..2ac6cae 100644 --- a/lib/mongo_odm/document/finders.rb +++ b/lib/mongo_odm/document/finders.rb @@ -12,6 +12,8 @@ def where(selector = {}, opts = {}) def find(*args) if (args.size == 1) && (criteria = find_one_by_id(args.first)) criteria.first + elsif (args.size == 1) && args[0].is_a?(Array) && (criteria = find_many_by_ids(args[0])) + criteria elsif (args.size >= 2) && (criteria = find_many_by_ids(args)) criteria else @@ -94,9 +96,9 @@ def find_many_by_ids(ids) end end end.compact - ids.present? && self.where(:_id => { :$in => ids }) + self.where(:_id => { :$in => ids }) end end end end -end \ No newline at end of file +end diff --git a/spec/mongo_odm/document/finders_spec.rb b/spec/mongo_odm/document/finders_spec.rb index 39c49ac..c5a155f 100644 --- a/spec/mongo_odm/document/finders_spec.rb +++ b/spec/mongo_odm/document/finders_spec.rb @@ -15,10 +15,13 @@ it "finds one by id" do Shape.find(@shapes.first.id).should == @shapes.first + Shape.find([@shapes.first.id]).should == [@shapes.first] end it "finds multiple by ids" do + Shape.find([]).should == [] Shape.find(*@shapes.map(&:id)).should == @shapes + Shape.find(@shapes.map(&:id)).should == @shapes end it "finds by criteria" do @@ -67,4 +70,4 @@ shape.should_not be_new_record end end -end \ No newline at end of file +end From f5d2970d42e2a304c71e6f143bb5babe85ed9e99 Mon Sep 17 00:00:00 2001 From: Dusty Doris Date: Fri, 8 Jul 2011 16:07:45 -0400 Subject: [PATCH 43/56] don't mark an attribute as dirty if it doesn't change --- .../document/attribute_methods/dirty.rb | 12 ++++++----- .../document/attribute_methods/dirty_spec.rb | 20 ++++++++++++++++++- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/lib/mongo_odm/document/attribute_methods/dirty.rb b/lib/mongo_odm/document/attribute_methods/dirty.rb index b6d9e72..5754370 100644 --- a/lib/mongo_odm/document/attribute_methods/dirty.rb +++ b/lib/mongo_odm/document/attribute_methods/dirty.rb @@ -16,19 +16,21 @@ module Dirty alias_method_chain :save, :dirty alias_method_chain :reload, :dirty end - + module InstanceMethods def initialize_with_dirty(*args) initialize_without_dirty(*args) changed_attributes.clear self end - + def write_attribute_with_dirty(attr_name, value) - send("#{attr_name}_will_change!") if respond_to?("#{attr_name}_will_change!") + if respond_to?("#{attr_name}_will_change!") && (read_attribute(attr_name) != value) + send("#{attr_name}_will_change!") + end write_attribute_without_dirty(attr_name, value) end - + def save_with_dirty(*args) if status = save_without_dirty(*args) @previously_changed = changes @@ -36,7 +38,7 @@ def save_with_dirty(*args) end status end - + def reload_with_dirty(*args) reload_without_dirty(*args).tap do @previously_changed = nil diff --git a/spec/mongo_odm/document/attribute_methods/dirty_spec.rb b/spec/mongo_odm/document/attribute_methods/dirty_spec.rb index fad020d..e1b7c54 100644 --- a/spec/mongo_odm/document/attribute_methods/dirty_spec.rb +++ b/spec/mongo_odm/document/attribute_methods/dirty_spec.rb @@ -2,5 +2,23 @@ require "spec_helper" describe MongoODM::Document::AttributeMethods::Dirty do - + + class TestDocument < Shape + validates_presence_of :color + end + + it "marks as dirty when changed" do + doc = TestDocument.new :color => 'blue' + doc.color_changed?.should == false + doc.color = 'red' + doc.color_changed?.should == true + end + + it "does not mark as dirty when the same" do + doc = TestDocument.new :color => 'blue' + doc.color_changed?.should == false + doc.color = 'blue' + doc.color_changed?.should == false + end + end \ No newline at end of file From 27a6d99b798e67045d90d864a2e4b47c622950b1 Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Tue, 6 Sep 2011 13:37:18 -0700 Subject: [PATCH 44/56] Delegate Criteria#each to cursor instead of #to_a. --- lib/mongo_odm/criteria.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/mongo_odm/criteria.rb b/lib/mongo_odm/criteria.rb index 6667fe1..0eee384 100644 --- a/lib/mongo_odm/criteria.rb +++ b/lib/mongo_odm/criteria.rb @@ -4,8 +4,8 @@ module MongoODM class Criteria - delegate :to_xml, :to_yaml, :to_json, :include?, :first, :length, :collect, :map, :each, :all?, :include?, :to => :to_a - delegate :count, :to => :to_cursor + delegate :to_xml, :to_yaml, :to_json, :include?, :first, :length, :collect, :map, :all?, :include?, :to => :to_a + delegate :count, :each, :to => :to_cursor def initialize(klass, selector = {}, opts = {}) @_klass = klass From 341a0934be501816ac424cae56cb9f682c084ec7 Mon Sep 17 00:00:00 2001 From: Michael Saffitz Date: Wed, 21 Sep 2011 14:39:41 -0700 Subject: [PATCH 45/56] track attribute_methods_generated to ensure define_attribute_methods_for_fields isn't called excessively. --- lib/mongo_odm/document/attribute_methods.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/mongo_odm/document/attribute_methods.rb b/lib/mongo_odm/document/attribute_methods.rb index 66cedda..579b0e6 100644 --- a/lib/mongo_odm/document/attribute_methods.rb +++ b/lib/mongo_odm/document/attribute_methods.rb @@ -69,8 +69,7 @@ def remove_attribute(name) def method_missing(method_id, *args, &block) # If we haven't generated any methods yet, generate them, then # see if we've created the method we're looking for. - method = self.class.respond_to?(:attribute_methods_generated?) ? :attribute_methods_generated? : :generated_attribute_methods - if !self.class.send(method) + unless self.class.attribute_methods_generated? self.class.define_attribute_methods_for_fields method_name = method_id.to_s guard_private_attribute_method!(method_name, args) @@ -81,7 +80,7 @@ def method_missing(method_id, *args, &block) end def respond_to?(*args) - self.class.define_attribute_methods_for_fields + self.class.define_attribute_methods_for_fields unless self.class.attribute_methods_generated? super end @@ -92,12 +91,17 @@ def attribute_method?(attr_name) end module ClassMethods + def attribute_methods_generated? + @attribute_methods_generated ||= nil + end + def default_attributes HashWithIndifferentAccess[fields.values.map{|field| [field.name, field.default.respond_to?(:call) ? field.default.call : field.default]}] end def define_attribute_methods_for_fields define_attribute_methods(fields.keys + [:_id, :_class]) + @attribute_methods_generated = true end end From f24c9d84b0ba60313e0ea1f9f122d5a93b801660 Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Tue, 15 Nov 2011 13:24:31 -0800 Subject: [PATCH 46/56] Fix for comparison of BSON::DBRef with nil. --- lib/mongo_odm/core_ext/conversions.rb | 4 ++-- spec/mongo_odm/core_ext/conversions_spec.rb | 14 +++++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/mongo_odm/core_ext/conversions.rb b/lib/mongo_odm/core_ext/conversions.rb index 9dee456..38fa8e0 100644 --- a/lib/mongo_odm/core_ext/conversions.rb +++ b/lib/mongo_odm/core_ext/conversions.rb @@ -47,7 +47,7 @@ def inspect end def eql?(other) - to_hash == other.to_hash + other.respond_to?(:to_hash) && (self.to_hash == other.to_hash) end alias :== :eql? end @@ -306,4 +306,4 @@ def to_mongo def dereference MongoODM.instanciate(self.map{|value| MongoODM.dereference(value)}) end -end \ No newline at end of file +end diff --git a/spec/mongo_odm/core_ext/conversions_spec.rb b/spec/mongo_odm/core_ext/conversions_spec.rb index 81a7cba..30e9c3c 100644 --- a/spec/mongo_odm/core_ext/conversions_spec.rb +++ b/spec/mongo_odm/core_ext/conversions_spec.rb @@ -6,6 +6,18 @@ before do Time.zone = 'UTC' end + + describe BSON::DBRef do + + describe "==" do + + it "compares with nil" do + BSON::DBRef.new('collection', BSON::ObjectId.new).should_not == nil + end + + end + + end describe Array do @@ -436,4 +448,4 @@ end -end \ No newline at end of file +end From 9929a7427bbb08037d89be43e98ebc9b2bbc3bf4 Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Wed, 7 Dec 2011 15:02:56 -0800 Subject: [PATCH 47/56] Added check to throw exception when referencing a new record. --- lib/mongo_odm/document/referable.rb | 1 + lib/mongo_odm/errors.rb | 11 ++++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/mongo_odm/document/referable.rb b/lib/mongo_odm/document/referable.rb index 8ebff0c..c98421f 100644 --- a/lib/mongo_odm/document/referable.rb +++ b/lib/mongo_odm/document/referable.rb @@ -33,6 +33,7 @@ module InstanceMethods Reference = MongoODM::Reference def to_dbref + raise Errors::ReferenceNewRecord if new_record? BSON::DBRef.new(self.class.collection.name, _id) end diff --git a/lib/mongo_odm/errors.rb b/lib/mongo_odm/errors.rb index 11f7903..1be5bb2 100644 --- a/lib/mongo_odm/errors.rb +++ b/lib/mongo_odm/errors.rb @@ -16,13 +16,13 @@ def initialize(field_name, klass) end end - class UnknownFieldError < StandardError + class UnknownFieldError < Error def initialize(field_name, klass) super "unknown field #{field_name} on class #{klass.name}" end end - class DocumentNotFound < StandardError + class DocumentNotFound < Error def initialize(ids, klass) super "can't find document for class #{klass} with id(s) #{ids}" end @@ -33,6 +33,11 @@ def initialize(doc) super "validation failure: #{doc.errors.full_messages}" end end - end + class ReferenceNewRecord < Error + def initialize + super "can't reference new record" + end + end + end end From 5df240416e80e48c34e036bb4207910d8e9551d8 Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Wed, 7 Dec 2011 15:03:09 -0800 Subject: [PATCH 48/56] Replaced autotest with guard. --- Gemfile | 2 +- Gemfile.lock | 10 ++++++---- Guardfile | 7 +++++++ autotest/discover.rb | 1 - 4 files changed, 14 insertions(+), 6 deletions(-) create mode 100644 Guardfile delete mode 100644 autotest/discover.rb diff --git a/Gemfile b/Gemfile index 71e84e8..6f06e83 100644 --- a/Gemfile +++ b/Gemfile @@ -8,8 +8,8 @@ gem 'bson_ext' gem 'tzinfo' group :development do - gem 'autotest' gem 'database_cleaner' + gem 'guard-rspec' gem 'jeweler' gem 'rcov' gem 'rspec' diff --git a/Gemfile.lock b/Gemfile.lock index 298f1c8..32c2d27 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,20 +1,21 @@ GEM remote: http://rubygems.org/ specs: - ZenTest (4.5.0) activemodel (3.0.5) activesupport (= 3.0.5) builder (~> 2.1.2) i18n (~> 0.4) activesupport (3.0.5) - autotest (4.4.6) - ZenTest (>= 4.4.1) bson (1.3.0) bson_ext (1.3.0) builder (2.1.2) database_cleaner (0.6.5) diff-lcs (1.1.2) git (1.2.5) + guard (0.8.8) + thor (~> 0.14.6) + guard-rspec (0.5.9) + guard (>= 0.8.4) i18n (0.5.0) jeweler (1.5.2) bundler (~> 1.0.0) @@ -32,6 +33,7 @@ GEM rspec-expectations (2.5.0) diff-lcs (~> 1.1.2) rspec-mocks (2.5.0) + thor (0.14.6) tzinfo (0.3.25) watchr (0.7) yard (0.6.5) @@ -42,9 +44,9 @@ PLATFORMS DEPENDENCIES activemodel activesupport - autotest bson_ext database_cleaner + guard-rspec jeweler mongo rcov diff --git a/Guardfile b/Guardfile new file mode 100644 index 0000000..16fc2f0 --- /dev/null +++ b/Guardfile @@ -0,0 +1,7 @@ +group 'specs' do + guard 'rspec', all_after_pass: false, cli: '--color --format nested' do # --fail-fast --drb + watch(%r{(^|/)\.'}) {} # ignore dot files + watch('spec/spec_helper.rb') { 'spec' } + watch(%r{^spec/(.+)_spec\.rb}) + end +end \ No newline at end of file diff --git a/autotest/discover.rb b/autotest/discover.rb deleted file mode 100644 index 47355c9..0000000 --- a/autotest/discover.rb +++ /dev/null @@ -1 +0,0 @@ -Autotest.add_discovery { 'rspec2' } From c27203a0b56e1579f1a6b52bfd71966e2e1e383a Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Wed, 7 Dec 2011 23:20:16 -0800 Subject: [PATCH 49/56] Added Criteria#first (uses Collection#find_one). --- lib/mongo_odm/criteria.rb | 8 ++++++-- spec/mongo_odm/criteria_spec.rb | 1 - 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/mongo_odm/criteria.rb b/lib/mongo_odm/criteria.rb index 0eee384..5cbe00a 100644 --- a/lib/mongo_odm/criteria.rb +++ b/lib/mongo_odm/criteria.rb @@ -4,7 +4,7 @@ module MongoODM class Criteria - delegate :to_xml, :to_yaml, :to_json, :include?, :first, :length, :collect, :map, :all?, :include?, :to => :to_a + delegate :to_xml, :to_yaml, :to_json, :include?, :length, :collect, :map, :all?, :include?, :to => :to_a delegate :count, :each, :to => :to_cursor def initialize(klass, selector = {}, opts = {}) @@ -32,6 +32,10 @@ def reload self end + def first + @_klass.collection.find_one(@_selector, @_opts) + end + def to_a @_result ||= @_cursor.rewind! && @_cursor.to_a end @@ -41,7 +45,7 @@ def to_cursor end def _set_cursor - @_cursor ||= @_klass.collection.find(@_selector, @_opts.dup) + @_cursor ||= @_klass.collection.find(@_selector, @_opts) end def _merge_criteria(criteria) diff --git a/spec/mongo_odm/criteria_spec.rb b/spec/mongo_odm/criteria_spec.rb index 20ee942..4f99526 100644 --- a/spec/mongo_odm/criteria_spec.rb +++ b/spec/mongo_odm/criteria_spec.rb @@ -9,7 +9,6 @@ it "#first" do criteria = Shape.where(:color => 'red') criteria.should be_a(MongoODM::Criteria) -p criteria.first criteria.first.should == @shapes.first end From d4ff72f694fcd92c60d96b5b162adfcf9df2eb83 Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Wed, 7 Dec 2011 23:20:51 -0800 Subject: [PATCH 50/56] Moved #sort, #skip, and #limit to Criteria, added #all (alias for #where) --- lib/mongo_odm/criteria.rb | 15 +++++++++++++++ lib/mongo_odm/document/finders.rb | 16 +++------------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/lib/mongo_odm/criteria.rb b/lib/mongo_odm/criteria.rb index 5cbe00a..a22a1d0 100644 --- a/lib/mongo_odm/criteria.rb +++ b/lib/mongo_odm/criteria.rb @@ -26,6 +26,21 @@ def ==(other) end end + def sort(key_or_list, direction = nil) + @_opts[:sort] = key_or_list.is_a?(Array) ? key_or_list : direction.nil? ? [key_or_list, :asc] : [key_or_list, direction] + self + end + + def skip(number_to_skip = nil) + @_opts[:skip] = number_to_skip + self + end + + def limit(number_to_return = nil) + @_opts[:limit] = number_to_return + self + end + def reload @_cursor = nil _set_cursor diff --git a/lib/mongo_odm/document/finders.rb b/lib/mongo_odm/document/finders.rb index 2ac6cae..f6412c6 100644 --- a/lib/mongo_odm/document/finders.rb +++ b/lib/mongo_odm/document/finders.rb @@ -5,9 +5,12 @@ module Finders extend ActiveSupport::Concern module ClassMethods + delegate :sort, :skip, :limit, :to => :where + def where(selector = {}, opts = {}) MongoODM::Criteria.new(self, selector, opts) end + alias all where def find(*args) if (args.size == 1) && (criteria = find_one_by_id(args.first)) @@ -40,19 +43,6 @@ def find_or_create_by(attr) doc end - def sort(key_or_list, direction = nil) - order = key_or_list.is_a?(Array) ? key_or_list : direction.nil? ? [key_or_list, :asc] : [key_or_list, direction] - MongoODM::Criteria.new(self, {}, :sort => order) - end - - def skip(number_to_skip = nil) - MongoODM::Criteria.new(self, {}, :skip => number_to_skip) - end - - def limit(number_to_return = nil) - MongoODM::Criteria.new(self, {}, :limit => number_to_return) - end - def to_cursor if @cursor @cursor.rewind! From ab6661a89f3149d9c6a76da1487970a8859174b1 Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Thu, 8 Dec 2011 10:52:31 -0800 Subject: [PATCH 51/56] Updated all gems (especially mongo to 1.5). --- Gemfile.lock | 54 +++++++++++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 32c2d27..fcdf7ca 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,42 +1,44 @@ GEM remote: http://rubygems.org/ specs: - activemodel (3.0.5) - activesupport (= 3.0.5) - builder (~> 2.1.2) - i18n (~> 0.4) - activesupport (3.0.5) - bson (1.3.0) - bson_ext (1.3.0) - builder (2.1.2) - database_cleaner (0.6.5) - diff-lcs (1.1.2) + activemodel (3.1.3) + activesupport (= 3.1.3) + builder (~> 3.0.0) + i18n (~> 0.6) + activesupport (3.1.3) + multi_json (~> 1.0) + bson (1.5.1) + bson_ext (1.5.1) + builder (3.0.0) + database_cleaner (0.7.0) + diff-lcs (1.1.3) git (1.2.5) guard (0.8.8) thor (~> 0.14.6) guard-rspec (0.5.9) guard (>= 0.8.4) - i18n (0.5.0) - jeweler (1.5.2) - bundler (~> 1.0.0) + i18n (0.6.0) + jeweler (1.6.4) + bundler (~> 1.0) git (>= 1.2.5) rake - mongo (1.3.0) - bson (>= 1.3.0) - rake (0.8.7) - rcov (0.9.9) - rspec (2.5.0) - rspec-core (~> 2.5.0) - rspec-expectations (~> 2.5.0) - rspec-mocks (~> 2.5.0) - rspec-core (2.5.1) - rspec-expectations (2.5.0) + mongo (1.5.1) + bson (= 1.5.1) + multi_json (1.0.4) + rake (0.9.2.2) + rcov (0.9.11) + rspec (2.7.0) + rspec-core (~> 2.7.0) + rspec-expectations (~> 2.7.0) + rspec-mocks (~> 2.7.0) + rspec-core (2.7.1) + rspec-expectations (2.7.0) diff-lcs (~> 1.1.2) - rspec-mocks (2.5.0) + rspec-mocks (2.7.0) thor (0.14.6) - tzinfo (0.3.25) + tzinfo (0.3.31) watchr (0.7) - yard (0.6.5) + yard (0.7.4) PLATFORMS ruby From c00947588099c72c79f0622fa00c7aff79c9a897 Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Thu, 8 Dec 2011 10:53:21 -0800 Subject: [PATCH 52/56] Collection delegates missing methods to Criteria (instead of a cursor), Criteria's cursor now generated lazily. --- lib/mongo_odm/criteria.rb | 22 ++++++++-------------- lib/mongo_odm/cursor.rb | 3 +-- lib/mongo_odm/document/finders.rb | 13 ++----------- 3 files changed, 11 insertions(+), 27 deletions(-) diff --git a/lib/mongo_odm/criteria.rb b/lib/mongo_odm/criteria.rb index a22a1d0..de51b59 100644 --- a/lib/mongo_odm/criteria.rb +++ b/lib/mongo_odm/criteria.rb @@ -5,13 +5,12 @@ module MongoODM class Criteria delegate :to_xml, :to_yaml, :to_json, :include?, :length, :collect, :map, :all?, :include?, :to => :to_a - delegate :count, :each, :to => :to_cursor + delegate :count, :each, :to => :cursor def initialize(klass, selector = {}, opts = {}) @_klass = klass @_selector = selector.to_mongo @_opts = opts - _set_cursor end attr_reader :_klass, :_selector, :_opts @@ -28,22 +27,21 @@ def ==(other) def sort(key_or_list, direction = nil) @_opts[:sort] = key_or_list.is_a?(Array) ? key_or_list : direction.nil? ? [key_or_list, :asc] : [key_or_list, direction] - self + reload end def skip(number_to_skip = nil) @_opts[:skip] = number_to_skip - self + reload end def limit(number_to_return = nil) @_opts[:limit] = number_to_return - self + reload end def reload @_cursor = nil - _set_cursor self end @@ -52,14 +50,10 @@ def first end def to_a - @_result ||= @_cursor.rewind! && @_cursor.to_a - end - - def to_cursor - @_cursor + @_result ||= cursor.rewind! && cursor.to_a end - def _set_cursor + def cursor @_cursor ||= @_klass.collection.find(@_selector, @_opts) end @@ -74,8 +68,8 @@ def method_missing(method_name, *args, &block) if @_klass.respond_to?(method_name) result = @_klass.send(method_name, *args, &block) result.is_a?(Criteria) ? _merge_criteria(result) : result - elsif @_cursor.respond_to?(method_name) - @_cursor.send(method_name, *args, &block) + elsif cursor.respond_to?(method_name) + cursor.send(method_name, *args, &block) elsif [].respond_to?(method_name) to_a.send(method_name, *args, &block) else diff --git a/lib/mongo_odm/cursor.rb b/lib/mongo_odm/cursor.rb index 7d035b1..d25686d 100644 --- a/lib/mongo_odm/cursor.rb +++ b/lib/mongo_odm/cursor.rb @@ -1,13 +1,12 @@ # encoding: utf-8 module MongoODM - delegate :inspect, :to_xml, :to_yaml, :to_json, :include?, :to => :to_a - class Cursor < Mongo::Cursor def next_document doc = super MongoODM.instanciate(doc) end + alias next next_document end end \ No newline at end of file diff --git a/lib/mongo_odm/document/finders.rb b/lib/mongo_odm/document/finders.rb index f6412c6..3cda0a3 100644 --- a/lib/mongo_odm/document/finders.rb +++ b/lib/mongo_odm/document/finders.rb @@ -43,18 +43,9 @@ def find_or_create_by(attr) doc end - def to_cursor - if @cursor - @cursor.rewind! - else - @cursor = where.to_cursor - end - @cursor - end - def method_missing(method_name, *args, &block) - if (cursor = to_cursor).respond_to?(method_name) - cursor.send(method_name, *args, &block) + if (criteria = self.where).respond_to?(method_name) + criteria.send(method_name, *args, &block) else super end From 652d8c913f28397735dfeb1f6192e690942f5540 Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Sun, 15 Jan 2012 13:14:14 -0800 Subject: [PATCH 53/56] Updated ruby-debug 1.9 support for 1.9.3. --- Gemfile | 7 ++++--- Gemfile.lock | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index 6f06e83..543e4d8 100644 --- a/Gemfile +++ b/Gemfile @@ -16,9 +16,10 @@ group :development do gem 'yard' gem 'watchr' - if RUBY_VERSION =~ /1.9/ + gem 'ruby-debug', :platforms => :ruby_18 + platforms :ruby_19 do + gem 'linecache19', :git => 'git://github.com/mark-moseley/linecache' + gem 'ruby-debug-base19x', '~> 0.11.30.pre4' gem 'ruby-debug19', :require => 'ruby-debug' - else - gem 'ruby-debug' end end diff --git a/Gemfile.lock b/Gemfile.lock index fcdf7ca..190ab4e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,3 +1,10 @@ +GIT + remote: git://github.com/mark-moseley/linecache + revision: 869c6a65155068415925067e480741bd0a71527e + specs: + linecache19 (0.5.12) + ruby_core_source (>= 0.1.4) + GEM remote: http://rubygems.org/ specs: @@ -7,9 +14,11 @@ GEM i18n (~> 0.6) activesupport (3.1.3) multi_json (~> 1.0) + archive-tar-minitar (0.5.2) bson (1.5.1) bson_ext (1.5.1) builder (3.0.0) + columnize (0.3.5) database_cleaner (0.7.0) diff-lcs (1.1.3) git (1.2.5) @@ -22,10 +31,13 @@ GEM bundler (~> 1.0) git (>= 1.2.5) rake + linecache (0.46) + rbx-require-relative (> 0.0.4) mongo (1.5.1) bson (= 1.5.1) multi_json (1.0.4) rake (0.9.2.2) + rbx-require-relative (0.0.5) rcov (0.9.11) rspec (2.7.0) rspec-core (~> 2.7.0) @@ -35,6 +47,26 @@ GEM rspec-expectations (2.7.0) diff-lcs (~> 1.1.2) rspec-mocks (2.7.0) + ruby-debug (0.10.4) + columnize (>= 0.1) + ruby-debug-base (~> 0.10.4.0) + ruby-debug-base (0.10.4) + linecache (>= 0.3) + ruby-debug-base19 (0.11.25) + columnize (>= 0.3.1) + linecache19 (>= 0.5.11) + ruby_core_source (>= 0.1.4) + ruby-debug-base19x (0.11.30.pre4) + columnize (>= 0.3.1) + linecache19 (>= 0.5.11) + rake (>= 0.8.1) + ruby_core_source (>= 0.1.4) + ruby-debug19 (0.11.6) + columnize (>= 0.3.1) + linecache19 (>= 0.5.11) + ruby-debug-base19 (>= 0.11.19) + ruby_core_source (0.1.5) + archive-tar-minitar (>= 0.5.2) thor (0.14.6) tzinfo (0.3.31) watchr (0.7) @@ -50,9 +82,12 @@ DEPENDENCIES database_cleaner guard-rspec jeweler + linecache19! mongo rcov rspec + ruby-debug + ruby-debug-base19x (~> 0.11.30.pre4) ruby-debug19 tzinfo watchr From 7e2f6c49b6341a4f9c4779915932c58dde1d920e Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Sat, 26 May 2012 12:57:01 -0700 Subject: [PATCH 54/56] Moved module instance methods to conform to new ActiveSupport::Concern (removes deprecation warning). --- lib/mongo_odm/document/attribute_methods.rb | 112 +++++++++--------- .../document/attribute_methods/dirty.rb | 42 ++++--- .../document/attribute_methods/inspect.rb | 8 +- .../attribute_methods/localization.rb | 32 +++-- .../document/attribute_methods/query.rb | 14 +-- .../document/attribute_methods/read.rb | 26 ++-- .../document/attribute_methods/write.rb | 18 ++- lib/mongo_odm/document/callbacks.rb | 10 +- lib/mongo_odm/document/equality.rb | 16 ++- lib/mongo_odm/document/inspect.rb | 14 +-- lib/mongo_odm/document/persistence.rb | 94 +++++++-------- lib/mongo_odm/document/referable.rb | 18 ++- lib/mongo_odm/document/timestamps.rb | 10 +- lib/mongo_odm/document/validations.rb | 42 ++++--- 14 files changed, 212 insertions(+), 244 deletions(-) diff --git a/lib/mongo_odm/document/attribute_methods.rb b/lib/mongo_odm/document/attribute_methods.rb index 579b0e6..ac05a81 100644 --- a/lib/mongo_odm/document/attribute_methods.rb +++ b/lib/mongo_odm/document/attribute_methods.rb @@ -7,7 +7,7 @@ module AttributeMethods extend ActiveSupport::Concern extend ActiveSupport::Autoload - + autoload :Read autoload :Write autoload :Query @@ -25,71 +25,69 @@ module AttributeMethods include AttributeMethods::Localization end - module InstanceMethods - attr_reader :attributes + attr_reader :attributes - def initialize(attrs = {}) - @attributes = {:_class => self.class.name}.with_indifferent_access - load_attributes_or_defaults(attrs) - self - end + def initialize(attrs = {}) + @attributes = {:_class => self.class.name}.with_indifferent_access + load_attributes_or_defaults(attrs) + self + end - def force_attributes=(new_attributes) - send(:attributes=, new_attributes, true) - end + def force_attributes=(new_attributes) + send(:attributes=, new_attributes, true) + end - def attributes=(new_attributes, auto_generate_attributes = false) - return if new_attributes.blank? - new_attributes.each do |name, value| - if respond_to?(:"#{name}=") - send(:"#{name}=", value) - else - auto_generate_attributes ? write_attribute(name, value) : raise(MongoODM::Errors::UnknownFieldError.new(name, self.class)) - end + def attributes=(new_attributes, auto_generate_attributes = false) + return if new_attributes.blank? + new_attributes.each do |name, value| + if respond_to?(:"#{name}=") + send(:"#{name}=", value) + else + auto_generate_attributes ? write_attribute(name, value) : raise(MongoODM::Errors::UnknownFieldError.new(name, self.class)) end end + end - def freeze - @attributes.freeze; super - end + def freeze + @attributes.freeze; super + end - def has_attribute?(name) - @attributes.has_key?(name) - end - - def load_attributes_or_defaults(attrs) - attrs = self.class.default_attributes.merge(attrs) - self.force_attributes = attrs - end - - def remove_attribute(name) - @attributes.delete(name) - end + def has_attribute?(name) + @attributes.has_key?(name) + end - def method_missing(method_id, *args, &block) - # If we haven't generated any methods yet, generate them, then - # see if we've created the method we're looking for. - unless self.class.attribute_methods_generated? - self.class.define_attribute_methods_for_fields - method_name = method_id.to_s - guard_private_attribute_method!(method_name, args) - send(method_id, *args, &block) - else - super - end - end - - def respond_to?(*args) - self.class.define_attribute_methods_for_fields unless self.class.attribute_methods_generated? + def load_attributes_or_defaults(attrs) + attrs = self.class.default_attributes.merge(attrs) + self.force_attributes = attrs + end + + def remove_attribute(name) + @attributes.delete(name) + end + + def method_missing(method_id, *args, &block) + # If we haven't generated any methods yet, generate them, then + # see if we've created the method we're looking for. + unless self.class.attribute_methods_generated? + self.class.define_attribute_methods_for_fields + method_name = method_id.to_s + guard_private_attribute_method!(method_name, args) + send(method_id, *args, &block) + else super end - - def attribute_method?(attr_name) - attr_name == '_id' || attributes.include?(attr_name) - end - protected :attribute_method? end - + + def respond_to?(*args) + self.class.define_attribute_methods_for_fields unless self.class.attribute_methods_generated? + super + end + + def attribute_method?(attr_name) + attr_name == '_id' || attributes.include?(attr_name) + end + protected :attribute_method? + module ClassMethods def attribute_methods_generated? @attribute_methods_generated ||= nil @@ -98,13 +96,13 @@ def attribute_methods_generated? def default_attributes HashWithIndifferentAccess[fields.values.map{|field| [field.name, field.default.respond_to?(:call) ? field.default.call : field.default]}] end - + def define_attribute_methods_for_fields define_attribute_methods(fields.keys + [:_id, :_class]) @attribute_methods_generated = true end end - + end end end diff --git a/lib/mongo_odm/document/attribute_methods/dirty.rb b/lib/mongo_odm/document/attribute_methods/dirty.rb index 5754370..eb5bd65 100644 --- a/lib/mongo_odm/document/attribute_methods/dirty.rb +++ b/lib/mongo_odm/document/attribute_methods/dirty.rb @@ -17,33 +17,31 @@ module Dirty alias_method_chain :reload, :dirty end - module InstanceMethods - def initialize_with_dirty(*args) - initialize_without_dirty(*args) - changed_attributes.clear - self - end + def initialize_with_dirty(*args) + initialize_without_dirty(*args) + changed_attributes.clear + self + end - def write_attribute_with_dirty(attr_name, value) - if respond_to?("#{attr_name}_will_change!") && (read_attribute(attr_name) != value) - send("#{attr_name}_will_change!") - end - write_attribute_without_dirty(attr_name, value) + def write_attribute_with_dirty(attr_name, value) + if respond_to?("#{attr_name}_will_change!") && (read_attribute(attr_name) != value) + send("#{attr_name}_will_change!") end + write_attribute_without_dirty(attr_name, value) + end - def save_with_dirty(*args) - if status = save_without_dirty(*args) - @previously_changed = changes - changed_attributes.clear - end - status + def save_with_dirty(*args) + if status = save_without_dirty(*args) + @previously_changed = changes + changed_attributes.clear end + status + end - def reload_with_dirty(*args) - reload_without_dirty(*args).tap do - @previously_changed = nil - changed_attributes.clear - end + def reload_with_dirty(*args) + reload_without_dirty(*args).tap do + @previously_changed = nil + changed_attributes.clear end end diff --git a/lib/mongo_odm/document/attribute_methods/inspect.rb b/lib/mongo_odm/document/attribute_methods/inspect.rb index 2ba86be..bc28abc 100644 --- a/lib/mongo_odm/document/attribute_methods/inspect.rb +++ b/lib/mongo_odm/document/attribute_methods/inspect.rb @@ -6,11 +6,9 @@ module AttributeMethods module Inspect extend ActiveSupport::Concern - - module InstanceMethods - def inspect_attribute(attr_name) - "#{attr_name}: #{read_attribute(attr_name).inspect}" - end + + def inspect_attribute(attr_name) + "#{attr_name}: #{read_attribute(attr_name).inspect}" end end diff --git a/lib/mongo_odm/document/attribute_methods/localization.rb b/lib/mongo_odm/document/attribute_methods/localization.rb index 904d370..18048b7 100644 --- a/lib/mongo_odm/document/attribute_methods/localization.rb +++ b/lib/mongo_odm/document/attribute_methods/localization.rb @@ -44,24 +44,22 @@ def define_method_attribute_localized=(attr_name) protected :define_method_attribute_localized= end - module InstanceMethods - def read_localized_attribute(attr_name, locale = nil) - locale = I18n.locale || I18n.default_locale if locale.nil? - (read_attribute(attr_name) || {}.with_indifferent_access)[locale] - end + def read_localized_attribute(attr_name, locale = nil) + locale = I18n.locale || I18n.default_locale if locale.nil? + (read_attribute(attr_name) || {}.with_indifferent_access)[locale] + end - def write_localized_attribute(attr_name, value, locale = nil) - locale = I18n.locale || I18n.default_locale if locale.nil? - current_value = read_attribute(attr_name) || {}.with_indifferent_access - write_attribute(attr_name, current_value.merge(locale => value)) - end - - def inspect_attribute_with_localization(attr_name) - if self.class.localized_attribute?(attr_name) - "#{attr_name}[:#{I18n.locale}]: #{read_localized_attribute(attr_name).inspect}" - else - inspect_attribute_without_localization(attr_name) - end + def write_localized_attribute(attr_name, value, locale = nil) + locale = I18n.locale || I18n.default_locale if locale.nil? + current_value = read_attribute(attr_name) || {}.with_indifferent_access + write_attribute(attr_name, current_value.merge(locale => value)) + end + + def inspect_attribute_with_localization(attr_name) + if self.class.localized_attribute?(attr_name) + "#{attr_name}[:#{I18n.locale}]: #{read_localized_attribute(attr_name).inspect}" + else + inspect_attribute_without_localization(attr_name) end end diff --git a/lib/mongo_odm/document/attribute_methods/query.rb b/lib/mongo_odm/document/attribute_methods/query.rb index 1ad93dd..9ab9a86 100644 --- a/lib/mongo_odm/document/attribute_methods/query.rb +++ b/lib/mongo_odm/document/attribute_methods/query.rb @@ -18,16 +18,14 @@ def define_method_attribute?(attr_name) protected :define_method_attribute? end - module InstanceMethods - def query_attribute(attr_name) - !!@attributes[attr_name] - end + def query_attribute(attr_name) + !!@attributes[attr_name] + end - def attribute?(attr_name) - query_attribute(attr_name) - end - private :attribute? + def attribute?(attr_name) + query_attribute(attr_name) end + private :attribute? end end diff --git a/lib/mongo_odm/document/attribute_methods/read.rb b/lib/mongo_odm/document/attribute_methods/read.rb index faf8d44..1169012 100644 --- a/lib/mongo_odm/document/attribute_methods/read.rb +++ b/lib/mongo_odm/document/attribute_methods/read.rb @@ -6,10 +6,6 @@ module AttributeMethods module Read extend ActiveSupport::Concern - - included do - attribute_method_suffix "" - end module ClassMethods def define_method_attribute(attr_name) @@ -18,20 +14,18 @@ def define_method_attribute(attr_name) protected :define_method_attribute end - module InstanceMethods - def read_attribute(attr_name) - @attributes[attr_name] - end - - def [](attr_name) - read_attribute(attr_name) - end + def read_attribute(attr_name) + @attributes[attr_name] + end - def attribute(attr_name) - read_attribute(attr_name) - end - private :attribute + def [](attr_name) + read_attribute(attr_name) + end + + def attribute(attr_name) + read_attribute(attr_name) end + private :attribute end end diff --git a/lib/mongo_odm/document/attribute_methods/write.rb b/lib/mongo_odm/document/attribute_methods/write.rb index 95b0b6f..b645be9 100644 --- a/lib/mongo_odm/document/attribute_methods/write.rb +++ b/lib/mongo_odm/document/attribute_methods/write.rb @@ -18,18 +18,16 @@ def define_method_attribute=(attr_name) protected :define_method_attribute= end - module InstanceMethods - def write_attribute(attr_name, value) - field = self.class.fields[attr_name] - type = field ? field.type : value.class - @attributes[attr_name] = attr_name.to_sym == :_id ? value : type.type_cast(value) - end + def write_attribute(attr_name, value) + field = self.class.fields[attr_name] + type = field ? field.type : value.class + @attributes[attr_name] = attr_name.to_sym == :_id ? value : type.type_cast(value) + end - def attribute=(attr_name, value) - write_attribute(attr_name, value) - end - private :attribute= + def attribute=(attr_name, value) + write_attribute(attr_name, value) end + private :attribute= end end diff --git a/lib/mongo_odm/document/callbacks.rb b/lib/mongo_odm/document/callbacks.rb index 001a83e..ad4cebc 100644 --- a/lib/mongo_odm/document/callbacks.rb +++ b/lib/mongo_odm/document/callbacks.rb @@ -17,12 +17,10 @@ module Callbacks define_model_callbacks :validate, :only => :before end - module InstanceMethods - def initialize_with_callbacks(*args) - initialize_without_callbacks(*args) - run_callbacks(:initialize) - self - end + def initialize_with_callbacks(*args) + initialize_without_callbacks(*args) + run_callbacks(:initialize) + self end end diff --git a/lib/mongo_odm/document/equality.rb b/lib/mongo_odm/document/equality.rb index d41064d..a366f40 100644 --- a/lib/mongo_odm/document/equality.rb +++ b/lib/mongo_odm/document/equality.rb @@ -5,15 +5,13 @@ module Equality extend ActiveSupport::Concern - module InstanceMethods - def eql?(other) - other.is_a?(self.class) && _id == other._id && attributes == other.attributes - end - alias :== :eql? - - def hash - _id.hash - end + def eql?(other) + other.is_a?(self.class) && _id == other._id && attributes == other.attributes + end + alias :== :eql? + + def hash + _id.hash end end diff --git a/lib/mongo_odm/document/inspect.rb b/lib/mongo_odm/document/inspect.rb index ca03332..d273c41 100644 --- a/lib/mongo_odm/document/inspect.rb +++ b/lib/mongo_odm/document/inspect.rb @@ -13,14 +13,12 @@ def inspect end end - module InstanceMethods - def private_attribute_names - %w(_class) - end - - def inspect - "#<#{self.class.name} #{attributes.except(*private_attribute_names).keys.sort.map{|k| inspect_attribute(k)}.join(", ")}>" - end + def private_attribute_names + %w(_class) + end + + def inspect + "#<#{self.class.name} #{attributes.except(*private_attribute_names).keys.sort.map{|k| inspect_attribute(k)}.join(", ")}>" end end diff --git a/lib/mongo_odm/document/persistence.rb b/lib/mongo_odm/document/persistence.rb index 9e58c59..a423795 100644 --- a/lib/mongo_odm/document/persistence.rb +++ b/lib/mongo_odm/document/persistence.rb @@ -8,65 +8,63 @@ module Persistence extend ActiveSupport::Concern - module InstanceMethods - def id - attributes[:_id] - end + def id + attributes[:_id] + end - def to_param - id.to_s - end + def to_param + id.to_s + end - def new_record? - id.nil? - end + def new_record? + id.nil? + end - def persisted? - !new_record? - end + def persisted? + !new_record? + end - def reload - self.load_attributes_or_defaults(self.class.find_one(:_id => id).attributes) unless new_record? - self - end + def reload + self.load_attributes_or_defaults(self.class.find_one(:_id => id).attributes) unless new_record? + self + end - # Save a document to its collection. - # - # @return [ObjectId] the _id of the saved document. - # - # @option opts [Boolean] :safe (+false+) - # If true, check that the save succeeded. OperationFailure - # will be raised on an error. Note that a safe check requires an extra - # round-trip to the database. - def save(options = {}) - _run_save_callbacks do - write_attribute(:_id, self.class.save(to_mongo, options)) - end - self + # Save a document to its collection. + # + # @return [ObjectId] the _id of the saved document. + # + # @option opts [Boolean] :safe (+false+) + # If true, check that the save succeeded. OperationFailure + # will be raised on an error. Note that a safe check requires an extra + # round-trip to the database. + def save(options = {}) + _run_save_callbacks do + write_attribute(:_id, self.class.save(to_mongo, options)) end + self + end - def save!(options = {}) - valid? or raise(Errors::Validation, self) - save(options.merge(:validate => false)) - end + def save!(options = {}) + valid? or raise(Errors::Validation, self) + save(options.merge(:validate => false)) + end - def update_attributes(attributes) - self.attributes = attributes - save - end + def update_attributes(attributes) + self.attributes = attributes + save + end - def destroy - return false if new_record? - _run_destroy_callbacks do - self.class.remove({ :_id => self.id }) - end + def destroy + return false if new_record? + _run_destroy_callbacks do + self.class.remove({ :_id => self.id }) end + end - def to_mongo - attributes.inject({}) do |attrs, (key, value)| - attrs[key] = value.to_mongo unless value.nil? # self.class.fields[key].default == value - attrs - end + def to_mongo + attributes.inject({}) do |attrs, (key, value)| + attrs[key] = value.to_mongo unless value.nil? # self.class.fields[key].default == value + attrs end end diff --git a/lib/mongo_odm/document/referable.rb b/lib/mongo_odm/document/referable.rb index c98421f..8bbef5b 100644 --- a/lib/mongo_odm/document/referable.rb +++ b/lib/mongo_odm/document/referable.rb @@ -29,19 +29,17 @@ def #{method_name} end end - module InstanceMethods - Reference = MongoODM::Reference + Reference = MongoODM::Reference - def to_dbref - raise Errors::ReferenceNewRecord if new_record? - BSON::DBRef.new(self.class.collection.name, _id) - end + def to_dbref + raise Errors::ReferenceNewRecord if new_record? + BSON::DBRef.new(self.class.collection.name, _id) + end - def reference - Reference.new self.to_dbref - end - alias ref reference + def reference + Reference.new self.to_dbref end + alias ref reference end end end diff --git a/lib/mongo_odm/document/timestamps.rb b/lib/mongo_odm/document/timestamps.rb index b8c5265..ef072e1 100644 --- a/lib/mongo_odm/document/timestamps.rb +++ b/lib/mongo_odm/document/timestamps.rb @@ -11,12 +11,10 @@ module Timestamps before_save :set_timestamps end - module InstanceMethods - def set_timestamps - now = Time.now - self.created_at ||= now if self.new_record? - self.updated_at = now - end + def set_timestamps + now = Time.now + self.created_at ||= now if self.new_record? + self.updated_at = now end end diff --git a/lib/mongo_odm/document/validations.rb b/lib/mongo_odm/document/validations.rb index 03a3544..78a4059 100644 --- a/lib/mongo_odm/document/validations.rb +++ b/lib/mongo_odm/document/validations.rb @@ -16,31 +16,29 @@ module Validations alias_method_chain :save, :validation end - module InstanceMethods - # The validation process on save can be skipped by passing false. The regular Base#save method is - # replaced with this when the validations module is mixed in, which it is by default. - def save_with_validation(options=nil) - perform_validation = case options - when NilClass - true - when Hash - options[:validate] != false - end - - if perform_validation && valid? || !perform_validation - save_without_validation - else - false - end + # The validation process on save can be skipped by passing false. The regular Base#save method is + # replaced with this when the validations module is mixed in, which it is by default. + def save_with_validation(options=nil) + perform_validation = case options + when NilClass + true + when Hash + options[:validate] != false end - - # Runs all the specified validations and returns true if no errors were added otherwise false. - def valid? - errors.clear - run_callbacks(:validate) - errors.empty? + + if perform_validation && valid? || !perform_validation + save_without_validation + else + false end end + + # Runs all the specified validations and returns true if no errors were added otherwise false. + def valid? + errors.clear + run_callbacks(:validate) + errors.empty? + end module ClassMethods # Validates whether or not a field is unique against the documents in the From e9df9eaa915b4b06b02562c3b290a9adfa5652b1 Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Mon, 28 May 2012 09:53:22 -0700 Subject: [PATCH 55/56] Changed Document.field to take type as option (compatible with Mongoid). --- lib/mongo_odm/document/fields.rb | 11 +++++++++-- spec/mongo_odm/document/fields_spec.rb | 15 ++++++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/mongo_odm/document/fields.rb b/lib/mongo_odm/document/fields.rb index 3ad1ba4..8a5bc1f 100644 --- a/lib/mongo_odm/document/fields.rb +++ b/lib/mongo_odm/document/fields.rb @@ -56,8 +56,15 @@ def fields @fields ||= {}.with_indifferent_access end - def field(name, type = String, options = {}) - fields[name] = Field.new(name, type, options) + def field(name, type_or_opts = nil, options = nil) + if !options && type_or_opts.is_a?(Hash) + options = type_or_opts + type = options[:type] + else + type = type_or_opts + end + + fields[name] = Field.new( name, (type || String), (options || {}) ) fields[name] end end diff --git a/spec/mongo_odm/document/fields_spec.rb b/spec/mongo_odm/document/fields_spec.rb index 84b0c16..5d39577 100644 --- a/spec/mongo_odm/document/fields_spec.rb +++ b/spec/mongo_odm/document/fields_spec.rb @@ -30,7 +30,20 @@ class TestingFloat < BlankSlate; field :testing_float, Float; end end end - + + context "with a name and type option" do + + before do + class TestingFloat < BlankSlate; field :testing_float, type: Float; end + end + + it "adds the typed field to the class" do + TestingFloat.fields[:testing_float].should be_kind_of(MongoODM::Document::Fields::Field) + TestingFloat.fields[:testing_float].type.should == Float + end + + end + end describe ".fields" do From c3ad2306c2bdbda0792c254984e656cd14294255 Mon Sep 17 00:00:00 2001 From: Steve Sloan Date: Mon, 28 May 2012 09:54:01 -0700 Subject: [PATCH 56/56] Fixed RSpec module name. --- Rakefile | 6 +++--- lib/mongo_odm/document/validations.rb | 6 +++--- spec/mongo_odm/document/fields_spec.rb | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Rakefile b/Rakefile index ca18475..a8987b1 100644 --- a/Rakefile +++ b/Rakefile @@ -34,14 +34,14 @@ if Gem.available? 'rspec' require "rspec" require "rspec/core/rake_task" desc "Run all specs" - Rspec::Core::RakeTask.new(:spec) do |spec| + RSpec::Core::RakeTask.new(:spec) do |spec| spec.pattern = "spec/**/*_spec.rb" end ## rcov task if Gem.available? 'rcov' require 'rcov/rcovtask' - Rspec::Core::RakeTask.new(:rcov) do |spec| + RSpec::Core::RakeTask.new(:rcov) do |spec| spec.pattern = "spec/**/*_spec.rb" spec.rcov = true end @@ -70,4 +70,4 @@ task :release => :build do end ## default task -task :default => :spec \ No newline at end of file +task :default => :spec diff --git a/lib/mongo_odm/document/validations.rb b/lib/mongo_odm/document/validations.rb index 78a4059..d6da100 100644 --- a/lib/mongo_odm/document/validations.rb +++ b/lib/mongo_odm/document/validations.rb @@ -7,7 +7,7 @@ module Validations extend ActiveSupport::Concern extend ActiveSupport::Autoload - + autoload :UniquenessValidator included do @@ -15,7 +15,7 @@ module Validations alias_method_chain :save, :validation end - + # The validation process on save can be skipped by passing false. The regular Base#save method is # replaced with this when the validations module is mixed in, which it is by default. def save_with_validation(options=nil) @@ -39,7 +39,7 @@ def valid? run_callbacks(:validate) errors.empty? end - + module ClassMethods # Validates whether or not a field is unique against the documents in the # database. diff --git a/spec/mongo_odm/document/fields_spec.rb b/spec/mongo_odm/document/fields_spec.rb index 5d39577..a0fe405 100644 --- a/spec/mongo_odm/document/fields_spec.rb +++ b/spec/mongo_odm/document/fields_spec.rb @@ -179,4 +179,4 @@ class InvalidClass; end end -end \ No newline at end of file +end