Skip to content
Browse files

switch :convert to the :transformer concept of the mongo ruby driver …

…v1.3
  • Loading branch information...
1 parent 6644da0 commit 4ec5bc4153410d2b25ab61308a58d80cabadf017 @mislav committed Apr 26, 2011
Showing with 25 additions and 37 deletions.
  1. +8 −8 Gemfile.lock
  2. +10 −3 lib/mingo.rb
  3. +5 −24 lib/mingo/cursor.rb
  4. +1 −1 lib/mingo/many_proxy.rb
  5. +1 −1 lib/mingo/persistence.rb
View
16 Gemfile.lock
@@ -1,19 +1,19 @@
GEM
remote: http://rubygems.org/
specs:
- activemodel (3.0.5)
- activesupport (= 3.0.5)
+ activemodel (3.0.7)
+ activesupport (= 3.0.7)
builder (~> 2.1.2)
- i18n (~> 0.4)
- activesupport (3.0.5)
- bson (1.2.4)
- bson_ext (1.2.4)
+ i18n (~> 0.5.0)
+ activesupport (3.0.7)
+ bson (1.3.0)
+ bson_ext (1.3.0)
builder (2.1.2)
diff-lcs (1.1.2)
hashie (0.4.0)
i18n (0.5.0)
- mongo (1.2.4)
- bson (>= 1.2.4)
+ mongo (1.3.0)
+ bson (>= 1.3.0)
mongo_ext (0.19.3)
rspec (2.0.1)
rspec-core (~> 2.0.1)
View
13 lib/mingo.rb
@@ -55,11 +55,13 @@ def first(id_or_selector = nil, options = {})
unless id_or_selector.nil? or Hash === id_or_selector
id_or_selector = BSON::ObjectId[id_or_selector]
end
- collection.find_one(id_or_selector, {:convert => self}.update(options))
+ options = { :transformer => lambda {|doc| self.new(doc)} }.update(options)
+ collection.find_one(id_or_selector, options)
end
def find(selector = {}, options = {}, &block)
- collection.find(selector, {:convert => self}.update(options), &block)
+ options = { :transformer => lambda {|doc| self.new(doc)} }.update(options)
+ collection.find(selector, options, &block)
end
def find_by_ids(object_ids, query = {}, options = {})
@@ -252,6 +254,11 @@ class User < Mingo
one_dup.should == one
end
+ it "returns a custom cursor" do
+ cursor = described_class.collection.find({})
+ cursor.should respond_to(:empty?)
+ end
+
def build(*args)
described_class.new(*args)
end
@@ -261,7 +268,7 @@ def create(*args)
end
def raw_doc(selector)
- described_class.first(selector, :convert => nil)
+ described_class.first(selector, :transformer => nil)
end
end
end
View
29 lib/mingo/cursor.rb
@@ -1,11 +1,10 @@
class Mingo
+ # Custom Cursor subclass.
# TODO: contribute this to the official driver
class Cursor < Mongo::Cursor
module CollectionPlugin
- def find(selector={}, opts={})
- opts = opts.dup
- convert = opts.delete(:convert)
- cursor = Cursor.from_mongo(super(selector, opts), convert)
+ def find(*args)
+ cursor = Cursor.from_mongo(super(*args))
if block_given?
yield cursor
@@ -17,34 +16,16 @@ def find(selector={}, opts={})
end
end
- def self.from_mongo(cursor, convert)
- new(cursor.collection, :convert => convert).tap do |sub|
+ def self.from_mongo(cursor)
+ new(cursor.collection).tap do |sub|
cursor.instance_variables.each { |ivar|
sub.instance_variable_set(ivar, cursor.instance_variable_get(ivar))
}
end
end
- def initialize(collection, options={})
- super
- @convert = options[:convert]
- end
-
- def next_document
- convert_document super
- end
-
def empty?
!has_next?
end
-
- private
-
- def convert_document(doc)
- if @convert.nil? or doc.nil? then doc
- elsif @convert.respond_to?(:call) then @convert.call(doc)
- else @convert.new(doc)
- end
- end
end
end
View
2 lib/mingo/many_proxy.rb
@@ -122,7 +122,7 @@ def find_options
decorate_block = self.class.decorate_each
if decorator or decorate_block
- {:convert => lambda { |doc|
+ {:transformer => lambda { |doc|
@model.new(doc).tap do |obj|
obj.extend decorator if decorator
if decorate_block
View
2 lib/mingo/persistence.rb
@@ -39,7 +39,7 @@ def update(doc, options = {})
end
def reload
- doc = self.class.first(id, :convert => nil)
+ doc = self.class.first(id, :transformer => nil)
replace doc
end

0 comments on commit 4ec5bc4

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