ign-mongo_mapper 0.8.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE +20 -0
- data/README.rdoc +33 -0
- data/UPGRADES +7 -0
- data/bin/mmconsole +60 -0
- data/examples/attr_accessible.rb +22 -0
- data/examples/attr_protected.rb +22 -0
- data/examples/cache_key.rb +24 -0
- data/examples/custom_types.rb +24 -0
- data/examples/identity_map.rb +33 -0
- data/examples/identity_map/automatic.rb +8 -0
- data/examples/keys.rb +40 -0
- data/examples/modifiers/set.rb +25 -0
- data/examples/plugins.rb +41 -0
- data/examples/querying.rb +35 -0
- data/examples/safe.rb +43 -0
- data/examples/scopes.rb +52 -0
- data/examples/validating/embedded_docs.rb +29 -0
- data/lib/mongo_mapper.rb +83 -0
- data/lib/mongo_mapper/connection.rb +83 -0
- data/lib/mongo_mapper/document.rb +41 -0
- data/lib/mongo_mapper/embedded_document.rb +31 -0
- data/lib/mongo_mapper/exceptions.rb +27 -0
- data/lib/mongo_mapper/extensions/array.rb +19 -0
- data/lib/mongo_mapper/extensions/binary.rb +22 -0
- data/lib/mongo_mapper/extensions/boolean.rb +44 -0
- data/lib/mongo_mapper/extensions/date.rb +25 -0
- data/lib/mongo_mapper/extensions/float.rb +14 -0
- data/lib/mongo_mapper/extensions/hash.rb +14 -0
- data/lib/mongo_mapper/extensions/integer.rb +19 -0
- data/lib/mongo_mapper/extensions/kernel.rb +9 -0
- data/lib/mongo_mapper/extensions/nil_class.rb +18 -0
- data/lib/mongo_mapper/extensions/object.rb +27 -0
- data/lib/mongo_mapper/extensions/object_id.rb +30 -0
- data/lib/mongo_mapper/extensions/set.rb +20 -0
- data/lib/mongo_mapper/extensions/string.rb +18 -0
- data/lib/mongo_mapper/extensions/time.rb +29 -0
- data/lib/mongo_mapper/middleware/identity_map.rb +16 -0
- data/lib/mongo_mapper/plugins.rb +15 -0
- data/lib/mongo_mapper/plugins/accessible.rb +44 -0
- data/lib/mongo_mapper/plugins/associations.rb +134 -0
- data/lib/mongo_mapper/plugins/associations/base.rb +124 -0
- data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +29 -0
- data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +24 -0
- data/lib/mongo_mapper/plugins/associations/collection.rb +27 -0
- data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +40 -0
- data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +151 -0
- data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +28 -0
- data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +109 -0
- data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +32 -0
- data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +24 -0
- data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +14 -0
- data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +41 -0
- data/lib/mongo_mapper/plugins/associations/one_proxy.rb +68 -0
- data/lib/mongo_mapper/plugins/associations/proxy.rb +139 -0
- data/lib/mongo_mapper/plugins/caching.rb +21 -0
- data/lib/mongo_mapper/plugins/callbacks.rb +243 -0
- data/lib/mongo_mapper/plugins/clone.rb +22 -0
- data/lib/mongo_mapper/plugins/descendants.rb +17 -0
- data/lib/mongo_mapper/plugins/dirty.rb +124 -0
- data/lib/mongo_mapper/plugins/document.rb +41 -0
- data/lib/mongo_mapper/plugins/dynamic_querying.rb +43 -0
- data/lib/mongo_mapper/plugins/dynamic_querying/dynamic_finder.rb +44 -0
- data/lib/mongo_mapper/plugins/embedded_document.rb +48 -0
- data/lib/mongo_mapper/plugins/equality.rb +17 -0
- data/lib/mongo_mapper/plugins/identity_map.rb +128 -0
- data/lib/mongo_mapper/plugins/indexes.rb +12 -0
- data/lib/mongo_mapper/plugins/inspect.rb +15 -0
- data/lib/mongo_mapper/plugins/keys.rb +311 -0
- data/lib/mongo_mapper/plugins/keys/key.rb +65 -0
- data/lib/mongo_mapper/plugins/logger.rb +18 -0
- data/lib/mongo_mapper/plugins/modifiers.rb +112 -0
- data/lib/mongo_mapper/plugins/pagination.rb +14 -0
- data/lib/mongo_mapper/plugins/persistence.rb +69 -0
- data/lib/mongo_mapper/plugins/protected.rb +53 -0
- data/lib/mongo_mapper/plugins/querying.rb +176 -0
- data/lib/mongo_mapper/plugins/querying/decorator.rb +46 -0
- data/lib/mongo_mapper/plugins/querying/plucky_methods.rb +15 -0
- data/lib/mongo_mapper/plugins/rails.rb +58 -0
- data/lib/mongo_mapper/plugins/safe.rb +28 -0
- data/lib/mongo_mapper/plugins/sci.rb +32 -0
- data/lib/mongo_mapper/plugins/scopes.rb +21 -0
- data/lib/mongo_mapper/plugins/serialization.rb +76 -0
- data/lib/mongo_mapper/plugins/timestamps.rb +22 -0
- data/lib/mongo_mapper/plugins/userstamps.rb +15 -0
- data/lib/mongo_mapper/plugins/validations.rb +50 -0
- data/lib/mongo_mapper/support/descendant_appends.rb +45 -0
- data/lib/mongo_mapper/version.rb +4 -0
- data/rails/init.rb +15 -0
- data/test/_NOTE_ON_TESTING +1 -0
- data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +64 -0
- data/test/functional/associations/test_belongs_to_proxy.rb +117 -0
- data/test/functional/associations/test_in_array_proxy.rb +349 -0
- data/test/functional/associations/test_many_documents_as_proxy.rb +229 -0
- data/test/functional/associations/test_many_documents_proxy.rb +615 -0
- data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +176 -0
- data/test/functional/associations/test_many_embedded_proxy.rb +256 -0
- data/test/functional/associations/test_many_polymorphic_proxy.rb +303 -0
- data/test/functional/associations/test_one_embedded_proxy.rb +100 -0
- data/test/functional/associations/test_one_proxy.rb +206 -0
- data/test/functional/test_accessible.rb +168 -0
- data/test/functional/test_associations.rb +46 -0
- data/test/functional/test_binary.rb +27 -0
- data/test/functional/test_caching.rb +76 -0
- data/test/functional/test_callbacks.rb +151 -0
- data/test/functional/test_dirty.rb +163 -0
- data/test/functional/test_document.rb +272 -0
- data/test/functional/test_dynamic_querying.rb +75 -0
- data/test/functional/test_embedded_document.rb +210 -0
- data/test/functional/test_identity_map.rb +513 -0
- data/test/functional/test_indexes.rb +42 -0
- data/test/functional/test_logger.rb +20 -0
- data/test/functional/test_modifiers.rb +416 -0
- data/test/functional/test_pagination.rb +91 -0
- data/test/functional/test_protected.rb +175 -0
- data/test/functional/test_querying.rb +873 -0
- data/test/functional/test_safe.rb +76 -0
- data/test/functional/test_sci.rb +230 -0
- data/test/functional/test_scopes.rb +171 -0
- data/test/functional/test_string_id_compatibility.rb +67 -0
- data/test/functional/test_timestamps.rb +62 -0
- data/test/functional/test_userstamps.rb +27 -0
- data/test/functional/test_validations.rb +342 -0
- data/test/models.rb +233 -0
- data/test/test_active_model_lint.rb +13 -0
- data/test/test_helper.rb +102 -0
- data/test/unit/associations/test_base.rb +212 -0
- data/test/unit/associations/test_proxy.rb +105 -0
- data/test/unit/serializers/test_json_serializer.rb +217 -0
- data/test/unit/test_clone.rb +69 -0
- data/test/unit/test_descendant_appends.rb +71 -0
- data/test/unit/test_document.rb +208 -0
- data/test/unit/test_dynamic_finder.rb +125 -0
- data/test/unit/test_embedded_document.rb +639 -0
- data/test/unit/test_extensions.rb +376 -0
- data/test/unit/test_identity_map_middleware.rb +34 -0
- data/test/unit/test_inspect.rb +22 -0
- data/test/unit/test_key.rb +205 -0
- data/test/unit/test_keys.rb +89 -0
- data/test/unit/test_mongo_mapper.rb +110 -0
- data/test/unit/test_pagination.rb +11 -0
- data/test/unit/test_plugins.rb +50 -0
- data/test/unit/test_rails.rb +181 -0
- data/test/unit/test_rails_compatibility.rb +52 -0
- data/test/unit/test_serialization.rb +51 -0
- data/test/unit/test_time_zones.rb +39 -0
- data/test/unit/test_validations.rb +564 -0
- metadata +385 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 John Nunemaker
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
= MongoMapper
|
2
|
+
|
3
|
+
A Ruby Object Mapper for Mongo.
|
4
|
+
|
5
|
+
== Install
|
6
|
+
|
7
|
+
$ gem install mongo_mapper
|
8
|
+
|
9
|
+
== Note on Patches/Pull Requests
|
10
|
+
|
11
|
+
* Fork the project.
|
12
|
+
* Make your feature addition or bug fix.
|
13
|
+
* Add tests for it. This is important so I don't break it in a future version unintentionally.
|
14
|
+
* Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself in another branch so I can ignore when I pull)
|
15
|
+
* Send me a pull request. Bonus points for topic branches.
|
16
|
+
|
17
|
+
== Development
|
18
|
+
|
19
|
+
$ gem install bundler (if you don't have it)
|
20
|
+
$ bundle install
|
21
|
+
$ bundle exec rake
|
22
|
+
|
23
|
+
== Problems or Questions?
|
24
|
+
|
25
|
+
Hit up the google group:
|
26
|
+
http://groups.google.com/group/mongomapper
|
27
|
+
|
28
|
+
Hop on IRC:
|
29
|
+
irc://chat.freenode.net/#mongomapper
|
30
|
+
|
31
|
+
== Copyright
|
32
|
+
|
33
|
+
See LICENSE for details.
|
data/UPGRADES
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
0.7.6 => 0.8
|
2
|
+
* Proxy#owner has been removed in favor of Proxy#proxy_owner
|
3
|
+
* @new instance variable renamed to @_new (you shouldn't be using this anyway)
|
4
|
+
* Removed undefining of object_id in proxies and equal? method in Equality. This means checking equal? for a proxy and a regular document will always be false even if proxy refers to same document. Check Proxy#target instead. (ie: root.equal?(item.root.target))
|
5
|
+
* find no longer takes options as a last argument. It only works with id, multiple ids, array of ids.
|
6
|
+
* MongoMapper::MongoMapperError is now MongoMapper::Error
|
7
|
+
* metaclass, meta_eval, meta_def, class_def removed as they were not being used
|
data/bin/mmconsole
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'mongo_mapper'
|
6
|
+
require 'irb'
|
7
|
+
rescue LoadError
|
8
|
+
require 'rubygems'
|
9
|
+
retry
|
10
|
+
end
|
11
|
+
|
12
|
+
IRB.setup(nil)
|
13
|
+
irb = IRB::Irb.new
|
14
|
+
|
15
|
+
IRB.conf[:MAIN_CONTEXT] = irb.context
|
16
|
+
|
17
|
+
irb.context.evaluate("require 'irb/completion'", 0)
|
18
|
+
irb.context.evaluate(%@
|
19
|
+
include MongoMapper
|
20
|
+
|
21
|
+
MongoMapper.database = "mm-test"
|
22
|
+
$db = MongoMapper.database
|
23
|
+
|
24
|
+
@, 0)
|
25
|
+
|
26
|
+
puts %@
|
27
|
+
Welcome to the MongoMapper Console!
|
28
|
+
|
29
|
+
Example 1:
|
30
|
+
things = $db.collection("things")
|
31
|
+
things.insert("name" => "Raw Thing")
|
32
|
+
things.insert("name" => "Another Thing", "date" => Time.now)
|
33
|
+
|
34
|
+
cursor = things.find("name" => "Raw Thing")
|
35
|
+
puts cursor.next_object.inspect
|
36
|
+
|
37
|
+
Example 2:
|
38
|
+
class Thing
|
39
|
+
include MongoMapper::Document
|
40
|
+
key :name, String, :required => true
|
41
|
+
key :date, Time
|
42
|
+
end
|
43
|
+
|
44
|
+
thing = Thing.new
|
45
|
+
thing.name = "My thing"
|
46
|
+
thing.date = Time.now
|
47
|
+
thing.save
|
48
|
+
|
49
|
+
all_things = Thing.all
|
50
|
+
puts all_things.map { |object| object.name }.inspect
|
51
|
+
@
|
52
|
+
|
53
|
+
trap("SIGINT") do
|
54
|
+
irb.signal_handle
|
55
|
+
end
|
56
|
+
|
57
|
+
catch(:IRB_EXIT) do
|
58
|
+
irb.eval_input
|
59
|
+
end
|
60
|
+
|
@@ -0,0 +1,22 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
|
2
|
+
require 'mongo_mapper'
|
3
|
+
|
4
|
+
MongoMapper.database = 'testing'
|
5
|
+
|
6
|
+
class User
|
7
|
+
include MongoMapper::Document
|
8
|
+
key :email, String
|
9
|
+
key :admin, Boolean, :default => false
|
10
|
+
|
11
|
+
# Only accessible or protected can be used, they cannot be used together
|
12
|
+
attr_accessible :email
|
13
|
+
end
|
14
|
+
|
15
|
+
# only accessible are set on new/create/etc.
|
16
|
+
user = User.create(:email => 'IDontLowerCaseThings@gmail.com', :admin => true)
|
17
|
+
puts user.admin # false
|
18
|
+
|
19
|
+
# can be set using accessor
|
20
|
+
user.admin = true
|
21
|
+
user.save
|
22
|
+
puts user.admin # true
|
@@ -0,0 +1,22 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
|
2
|
+
require 'mongo_mapper'
|
3
|
+
|
4
|
+
MongoMapper.database = 'testing'
|
5
|
+
|
6
|
+
class User
|
7
|
+
include MongoMapper::Document
|
8
|
+
key :email, String
|
9
|
+
key :admin, Boolean, :default => false
|
10
|
+
|
11
|
+
# Only accessible or protected can be used, they cannot be used together
|
12
|
+
attr_protected :admin
|
13
|
+
end
|
14
|
+
|
15
|
+
# protected are ignored on new/create/etc.
|
16
|
+
user = User.create(:email => 'IDontLowerCaseThings@gmail.com', :admin => true)
|
17
|
+
puts user.admin # false
|
18
|
+
|
19
|
+
# can be set using accessor
|
20
|
+
user.admin = true
|
21
|
+
user.save
|
22
|
+
puts user.admin # true
|
@@ -0,0 +1,24 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
|
2
|
+
require 'mongo_mapper'
|
3
|
+
|
4
|
+
MongoMapper.database = 'testing'
|
5
|
+
|
6
|
+
class User
|
7
|
+
include MongoMapper::Document
|
8
|
+
end
|
9
|
+
|
10
|
+
# New Documents
|
11
|
+
puts User.new.cache_key # User/new
|
12
|
+
|
13
|
+
# Created Documents
|
14
|
+
puts User.create.cache_key # User/:id (ie: User/4c7a940cbcd1b3319b000003)
|
15
|
+
|
16
|
+
# With Suffix
|
17
|
+
puts User.create.cache_key(:foo) # User/:id/foo
|
18
|
+
|
19
|
+
# With Multiple Suffixes
|
20
|
+
puts User.create.cache_key(:foo, :bar, :baz) # User/:id/foo/bar/baz
|
21
|
+
|
22
|
+
# When updated_at key exists it will be used
|
23
|
+
User.timestamps!
|
24
|
+
puts User.create.cache_key # User/:id-:updated_at (ie: User/4c7a940cbcd1b3319b000003-20100829170828)
|
@@ -0,0 +1,24 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
|
2
|
+
require 'mongo_mapper'
|
3
|
+
require 'pp'
|
4
|
+
|
5
|
+
MongoMapper.database = 'testing'
|
6
|
+
|
7
|
+
class DowncasedString
|
8
|
+
# to_mongo gets called anytime a value is assigned
|
9
|
+
def self.to_mongo(value)
|
10
|
+
value.nil? ? nil : value.to_s.downcase
|
11
|
+
end
|
12
|
+
|
13
|
+
# from mongo gets called anytime a value is read
|
14
|
+
def self.from_mongo(value)
|
15
|
+
value.nil? ? nil : value.to_s.downcase
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class User
|
20
|
+
include MongoMapper::Document
|
21
|
+
key :email, DowncasedString
|
22
|
+
end
|
23
|
+
|
24
|
+
pp User.create(:email => 'IDontLowerCaseThings@gmail.com')
|
@@ -0,0 +1,33 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
|
2
|
+
require 'mongo_mapper'
|
3
|
+
require 'pp'
|
4
|
+
|
5
|
+
MongoMapper.database = 'testing'
|
6
|
+
|
7
|
+
class User
|
8
|
+
include MongoMapper::Document
|
9
|
+
plugin MongoMapper::Plugins::IdentityMap
|
10
|
+
|
11
|
+
key :name, String
|
12
|
+
end
|
13
|
+
User.delete_all
|
14
|
+
|
15
|
+
user = User.create(:name => 'John')
|
16
|
+
|
17
|
+
# User gets added to map on save
|
18
|
+
pp User.identity_map[user.id]
|
19
|
+
|
20
|
+
# Does not matter how you find user, it is always the same object until the identity map is cleared
|
21
|
+
puts "#{User.identity_map[user.id].object_id} == #{user.object_id}"
|
22
|
+
puts "#{User.find(user.id).object_id} == #{user.object_id}"
|
23
|
+
puts "#{User.all[0].object_id} == #{user.object_id}"
|
24
|
+
|
25
|
+
MongoMapper::Plugins::IdentityMap.clear
|
26
|
+
puts "#{User.find(user.id).object_id} != #{user.object_id}"
|
27
|
+
|
28
|
+
# User gets removed from map on destroy
|
29
|
+
user = User.create
|
30
|
+
user.destroy
|
31
|
+
puts "Should be nil: " + User.identity_map[user.id].inspect
|
32
|
+
|
33
|
+
|
data/examples/keys.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
|
2
|
+
require 'mongo_mapper'
|
3
|
+
require 'pp'
|
4
|
+
|
5
|
+
MongoMapper.database = 'testing'
|
6
|
+
|
7
|
+
class User
|
8
|
+
include MongoMapper::Document
|
9
|
+
|
10
|
+
key :first_name, String, :required => true
|
11
|
+
key :last_name, String, :required => true
|
12
|
+
key :token, String, :default => lambda { 'some random string' }
|
13
|
+
key :age, Integer
|
14
|
+
key :skills, Array
|
15
|
+
key :friend_ids, Array, :typecast => 'ObjectId'
|
16
|
+
key :links, Hash
|
17
|
+
timestamps!
|
18
|
+
end
|
19
|
+
User.collection.remove # empties collection
|
20
|
+
|
21
|
+
john = User.create({
|
22
|
+
:first_name => 'John',
|
23
|
+
:last_name => 'Nunemaker',
|
24
|
+
:age => 28,
|
25
|
+
:skills => ['ruby', 'mongo', 'javascript'],
|
26
|
+
:links => {"Google" => "http://www.google.com"}
|
27
|
+
})
|
28
|
+
|
29
|
+
steve = User.create({
|
30
|
+
:first_name => 'Steve',
|
31
|
+
:last_name => 'Smith',
|
32
|
+
:age => 29,
|
33
|
+
:skills => ['html', 'css', 'javascript', 'design'],
|
34
|
+
})
|
35
|
+
|
36
|
+
john.friend_ids << steve.id.to_s # will get typecast to ObjectId
|
37
|
+
john.links["Ruby on Rails"] = "http://www.rubyonrails.com"
|
38
|
+
john.save
|
39
|
+
|
40
|
+
pp john
|
@@ -0,0 +1,25 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.expand_path('../../../lib', __FILE__))
|
2
|
+
require 'mongo_mapper'
|
3
|
+
require 'pp'
|
4
|
+
|
5
|
+
MongoMapper.database = 'testing'
|
6
|
+
|
7
|
+
class User
|
8
|
+
include MongoMapper::Document
|
9
|
+
|
10
|
+
key :name, String
|
11
|
+
key :tags, Array
|
12
|
+
end
|
13
|
+
User.collection.remove # empties collection
|
14
|
+
|
15
|
+
john = User.create(:name => 'John', :tags => %w[ruby mongo], :age => 28)
|
16
|
+
bill = User.create(:name => 'Bill', :tags => %w[ruby mongo], :age => 30)
|
17
|
+
|
18
|
+
User.set({:name => 'John'}, :tags => %[ruby])
|
19
|
+
pp john.reload
|
20
|
+
|
21
|
+
User.set(john.id, :tags => %w[ruby mongo])
|
22
|
+
pp john.reload
|
23
|
+
|
24
|
+
john.set(:tags => %w[something different])
|
25
|
+
pp john.reload
|
data/examples/plugins.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
|
2
|
+
require 'mongo_mapper'
|
3
|
+
require 'pp'
|
4
|
+
|
5
|
+
MongoMapper.database = 'testing'
|
6
|
+
|
7
|
+
# To create your own plugin, just create a module.
|
8
|
+
module FooPlugin
|
9
|
+
|
10
|
+
# ClassMethods module will automatically get extended
|
11
|
+
module ClassMethods
|
12
|
+
def foo
|
13
|
+
'Foo class method!'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
# InstanceMethods module will automatically get included
|
18
|
+
module InstanceMethods
|
19
|
+
def foo
|
20
|
+
'Foo instance method!'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# If present, configure method will be called and passed the
|
25
|
+
# model as an argument. Feel free to class_eval or add keys.
|
26
|
+
# if method is not present, it doesn't call it.
|
27
|
+
def self.configure(model)
|
28
|
+
puts "Configuring #{model}..."
|
29
|
+
model.key :foo, String
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
class User
|
35
|
+
include MongoMapper::Document
|
36
|
+
plugin FooPlugin
|
37
|
+
end
|
38
|
+
|
39
|
+
puts User.foo
|
40
|
+
puts User.new.foo
|
41
|
+
puts User.key?(:foo)
|
@@ -0,0 +1,35 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
|
2
|
+
require 'mongo_mapper'
|
3
|
+
require 'pp'
|
4
|
+
|
5
|
+
MongoMapper.database = 'testing'
|
6
|
+
|
7
|
+
class User
|
8
|
+
include MongoMapper::Document
|
9
|
+
|
10
|
+
key :name, String
|
11
|
+
key :tags, Array
|
12
|
+
end
|
13
|
+
User.collection.remove # empties collection
|
14
|
+
|
15
|
+
User.create(:name => 'John', :tags => %w[ruby mongo], :age => 28)
|
16
|
+
User.create(:name => 'Bill', :tags => %w[ruby mongo], :age => 30)
|
17
|
+
User.create(:name => 'Frank', :tags => %w[mongo], :age => 35)
|
18
|
+
User.create(:name => 'Steve', :tags => %w[html5 css3], :age => 27)
|
19
|
+
|
20
|
+
[
|
21
|
+
|
22
|
+
User.all(:name => 'John'),
|
23
|
+
User.all(:tags => %w[mongo]),
|
24
|
+
User.all(:tags.all => %w[ruby mongo]),
|
25
|
+
User.all(:age.gte => 30),
|
26
|
+
|
27
|
+
User.where(:age.gt => 27).sort(:age).all,
|
28
|
+
User.where(:age.gt => 27).sort(:age.desc).all,
|
29
|
+
User.where(:age.gt => 27).sort(:age).limit(1).all,
|
30
|
+
User.where(:age.gt => 27).sort(:age).skip(1).limit(1).all,
|
31
|
+
|
32
|
+
].each do |result|
|
33
|
+
pp result
|
34
|
+
puts
|
35
|
+
end
|
data/examples/safe.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
|
2
|
+
require 'mongo_mapper'
|
3
|
+
MongoMapper.database = 'testing'
|
4
|
+
|
5
|
+
class User
|
6
|
+
include MongoMapper::Document
|
7
|
+
key :email, String
|
8
|
+
end
|
9
|
+
|
10
|
+
# Drop collection and ensure unique index on email
|
11
|
+
User.collection.drop
|
12
|
+
User.ensure_index(:email, :unique => true)
|
13
|
+
|
14
|
+
User.create(:email => 'nunemaker@gmail.com')
|
15
|
+
User.create(:email => 'nunemaker@gmail.com')
|
16
|
+
|
17
|
+
puts "Count should only be one since the second create failed, count is: #{User.count}" # 1 because second was not created, but no exception raised
|
18
|
+
puts
|
19
|
+
|
20
|
+
# save method also takes options, including :safe
|
21
|
+
# which will force raise when duplicate is hit
|
22
|
+
begin
|
23
|
+
user = User.new(:email => 'nunemaker@gmail.com')
|
24
|
+
user.save(:safe => true)
|
25
|
+
rescue Mongo::OperationFailure => e
|
26
|
+
puts 'Mongo Operation failure raised because duplicate email was entered'
|
27
|
+
puts e.inspect
|
28
|
+
puts
|
29
|
+
end
|
30
|
+
|
31
|
+
# Mark user model as safe, same as doing this...
|
32
|
+
# class User
|
33
|
+
# include MongoMapper::Document
|
34
|
+
# safe
|
35
|
+
# end
|
36
|
+
User.safe
|
37
|
+
|
38
|
+
begin
|
39
|
+
User.create(:email => 'nunemaker@gmail.com')
|
40
|
+
rescue Mongo::OperationFailure => e
|
41
|
+
puts 'Mongo Operation failure raised because duplicate email was entered'
|
42
|
+
puts e.inspect
|
43
|
+
end
|