zentradi 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/zentradi.rb +3 -4
- data/lib/zentradi/collection.rb +10 -9
- data/lib/zentradi/document_helpers.rb +27 -0
- data/lib/zentradi/document_wrapper.rb +39 -23
- data/spec/zentradi/document_spec.rb +5 -4
- data/zentradi.gemspec +3 -3
- metadata +4 -4
- data/lib/zentradi/delegate.rb +0 -32
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.2
|
data/lib/zentradi.rb
CHANGED
@@ -2,7 +2,7 @@ require 'hashie'
|
|
2
2
|
require 'mongo'
|
3
3
|
|
4
4
|
module Zdi
|
5
|
-
autoload :
|
5
|
+
autoload :DocumentHelpers , 'zentradi/document_helpers'
|
6
6
|
autoload :DocumentWrapper , 'zentradi/document_wrapper'
|
7
7
|
autoload :Collection , 'zentradi/collection'
|
8
8
|
|
@@ -10,9 +10,8 @@ module Zdi
|
|
10
10
|
attr_accessor :default_database
|
11
11
|
|
12
12
|
def Collection(collection, database = Zdi.default_database)
|
13
|
-
|
14
|
-
|
15
|
-
end
|
13
|
+
raise ArgumentError, "please provide a database. The default one is #{database.inspect}" unless database.is_a?(Mongo::DB)
|
14
|
+
Class.new(Zdi::Collection) { instance_variable_set("@collection", database[collection]) }
|
16
15
|
end
|
17
16
|
end
|
18
17
|
end
|
data/lib/zentradi/collection.rb
CHANGED
@@ -1,9 +1,5 @@
|
|
1
1
|
module Zdi
|
2
2
|
class Collection
|
3
|
-
class << self
|
4
|
-
extend Zdi::Delegate
|
5
|
-
delegate *(Mongo::Collection.public_instance_methods - public_instance_methods), :to => :collection
|
6
|
-
end
|
7
3
|
|
8
4
|
module ClassMethods
|
9
5
|
attr_accessor :collection
|
@@ -29,9 +25,7 @@ module Zdi
|
|
29
25
|
end
|
30
26
|
|
31
27
|
def create(attributes = {})
|
32
|
-
|
33
|
-
doc.save
|
34
|
-
doc
|
28
|
+
build(attributes).tap { |o| o.save }
|
35
29
|
end
|
36
30
|
|
37
31
|
def find_or_build(attributes)
|
@@ -42,15 +36,22 @@ module Zdi
|
|
42
36
|
update(attributes, attributes, :upsert => true)
|
43
37
|
end
|
44
38
|
|
45
|
-
def
|
46
|
-
|
39
|
+
def delegate_to_collection(methods)
|
40
|
+
Array(methods).each { |meth| extend(Module.new { module_eval(<<-METHOD, __FILE__, __LINE__ + 1) }) }
|
41
|
+
def #{ meth }(*args, &block)
|
42
|
+
Zdi::DocumentWrapper.mongo_collection_method_wrap(collection, #{ meth.inspect }, self, *args, &block)
|
43
|
+
end
|
44
|
+
METHOD
|
47
45
|
end
|
48
46
|
end
|
49
47
|
|
50
48
|
extend ClassMethods
|
51
49
|
|
50
|
+
delegate_to_collection(Mongo::Collection.public_instance_methods - public_instance_methods)
|
51
|
+
|
52
52
|
class << self
|
53
53
|
alias_method :first, :find_one
|
54
|
+
alias_method :all, :find
|
54
55
|
end
|
55
56
|
end
|
56
57
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Zdi
|
2
|
+
module DocumentHelpers
|
3
|
+
|
4
|
+
def id
|
5
|
+
super || _id
|
6
|
+
end
|
7
|
+
|
8
|
+
def new?
|
9
|
+
id.nil? || @collection.find("_id" => id).count == 0
|
10
|
+
end
|
11
|
+
|
12
|
+
def save
|
13
|
+
if id.nil?
|
14
|
+
self.id = @collection.save(self)
|
15
|
+
else
|
16
|
+
@collection.update({"_id" => id}, self)
|
17
|
+
end
|
18
|
+
raise RuntimeError, "document lacks an id after save!" if id.nil?
|
19
|
+
self
|
20
|
+
end
|
21
|
+
|
22
|
+
def destroy
|
23
|
+
@collection.remove("_id" => id)
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
@@ -1,37 +1,53 @@
|
|
1
1
|
module Zdi
|
2
2
|
class DocumentWrapper < Hashie::Mash
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
4
|
+
module ClassMethods
|
5
|
+
# Sends a method to a mongo collection.
|
6
|
+
# On send, all DocumentWrapper args are converted to hash.
|
7
|
+
# On return, all hashes in the result are converted to DocumentWrappers.
|
8
|
+
def mongo_collection_method_wrap(mongo_collection, method, zdi_collection_class, *args, &block)
|
9
|
+
new_args = Zdi::DocumentWrapper.unwrap(args)
|
10
|
+
result = mongo_collection.send(method, *new_args, &block)
|
11
|
+
Zdi::DocumentWrapper.wrap(zdi_collection_class, result)
|
12
|
+
end
|
10
13
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
+
# Convert DocumentWrapper instance/s to normal hashes.
|
15
|
+
def unwrap(object)
|
16
|
+
case object
|
17
|
+
when DocumentWrapper then object.to_hash
|
18
|
+
when Array then object.map { |e| DocumentWrapper.unwrap(e) }
|
19
|
+
else
|
20
|
+
object
|
21
|
+
end
|
22
|
+
end
|
14
23
|
|
15
|
-
|
16
|
-
|
24
|
+
# Convert hashes instance/s to normal DocumentWrapper instances.
|
25
|
+
def wrap(zdi_collection_class, object)
|
26
|
+
case object
|
27
|
+
when Hash then DocumentWrapper.new(zdi_collection_class, object)
|
28
|
+
when Array then object.map { |e| DocumentWrapper.wrap(zdi_collection_class, e) }
|
29
|
+
when Mongo::Cursor then object.extend(MongoCursorMethods).zentradi_collection = zdi_collection_class; object
|
30
|
+
else
|
31
|
+
object
|
32
|
+
end
|
33
|
+
end
|
17
34
|
end
|
18
35
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
else
|
23
|
-
@collection.update({"_id" => id}, self)
|
36
|
+
module MongoCursorMethods
|
37
|
+
def next_document
|
38
|
+
Zdi::DocumentWrapper.wrap(zentradi_collection, super)
|
24
39
|
end
|
25
|
-
|
40
|
+
attr_accessor :zentradi_collection
|
26
41
|
end
|
27
42
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
43
|
+
extend ClassMethods
|
44
|
+
include DocumentHelpers
|
45
|
+
|
46
|
+
attr_reader :collection
|
32
47
|
|
33
|
-
def
|
34
|
-
|
48
|
+
def initialize(collection, hash = {})
|
49
|
+
super(hash)
|
50
|
+
@collection = collection
|
35
51
|
end
|
36
52
|
|
37
53
|
end
|
@@ -39,16 +39,17 @@ describe Zdi::Collection do
|
|
39
39
|
post.save
|
40
40
|
|
41
41
|
post.update(:title => post.title + " DEF")
|
42
|
+
post.save
|
42
43
|
|
43
44
|
from_db = Post.first("_id" => post.id)
|
44
45
|
from_db.title.should == "ABC DEF"
|
45
46
|
end
|
46
47
|
|
47
48
|
it "destroys a document" do
|
48
|
-
|
49
|
-
id =
|
50
|
-
|
51
|
-
|
49
|
+
post = Post.create(:title => "Hola Mundo", :body => "Hi.")
|
50
|
+
id = post.id
|
51
|
+
post.should_not be_new
|
52
|
+
post.destroy
|
52
53
|
Post.first(id).should be_nil
|
53
54
|
end
|
54
55
|
|
data/zentradi.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{zentradi}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Emmanuel Oga"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-04-04}
|
13
13
|
s.description = %q{minimalistic tools for accessing mongodb}
|
14
14
|
s.email = %q{EmmanuelOga@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -26,7 +26,7 @@ Gem::Specification.new do |s|
|
|
26
26
|
"VERSION",
|
27
27
|
"lib/zentradi.rb",
|
28
28
|
"lib/zentradi/collection.rb",
|
29
|
-
"lib/zentradi/
|
29
|
+
"lib/zentradi/document_helpers.rb",
|
30
30
|
"lib/zentradi/document_wrapper.rb",
|
31
31
|
"spec/spec_helper.rb",
|
32
32
|
"spec/zentradi/document_spec.rb",
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 2
|
9
|
+
version: 0.0.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Emmanuel Oga
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-04-04 00:00:00 -03:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -98,7 +98,7 @@ files:
|
|
98
98
|
- VERSION
|
99
99
|
- lib/zentradi.rb
|
100
100
|
- lib/zentradi/collection.rb
|
101
|
-
- lib/zentradi/
|
101
|
+
- lib/zentradi/document_helpers.rb
|
102
102
|
- lib/zentradi/document_wrapper.rb
|
103
103
|
- spec/spec_helper.rb
|
104
104
|
- spec/zentradi/document_spec.rb
|
data/lib/zentradi/delegate.rb
DELETED
@@ -1,32 +0,0 @@
|
|
1
|
-
module Zdi
|
2
|
-
module Delegate
|
3
|
-
def delegate(*methods)
|
4
|
-
include Wrapper unless ancestors.include?(Wrapper)
|
5
|
-
to = methods.pop[:to]
|
6
|
-
methods.each do |name|
|
7
|
-
module_eval(<<-EOMETHOD, __FILE__, __LINE__ + 1)
|
8
|
-
def #{ name }(*args, &block)
|
9
|
-
new_args = args.map { |a| a.is_a?(DocumentWrapper) ? a.to_hash : a }
|
10
|
-
|
11
|
-
result = #{to}.send :#{name}, *new_args, &block
|
12
|
-
|
13
|
-
return wrap_if_document(result)
|
14
|
-
end
|
15
|
-
EOMETHOD
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
module Wrapper
|
20
|
-
def wrap_if_document(object)
|
21
|
-
if object.is_a?(OrderedHash)
|
22
|
-
DocumentWrapper.new(self, object)
|
23
|
-
elsif object.respond_to?(:map)
|
24
|
-
object.map { |e| wrap_if_document(e) }
|
25
|
-
else
|
26
|
-
return object
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
end
|
32
|
-
end
|