mongodb_model 0.0.12 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +2 -2
- data/readme.md +61 -35
- metadata +10 -10
data/Rakefile
CHANGED
@@ -2,10 +2,10 @@ require 'rake_ext'
|
|
2
2
|
|
3
3
|
project(
|
4
4
|
name: "mongodb_model",
|
5
|
-
|
5
|
+
version: '0.2.1',
|
6
6
|
gem: true,
|
7
7
|
summary: "Object Model for MongoDB",
|
8
8
|
|
9
9
|
author: "Alexey Petrushin",
|
10
|
-
homepage: "http://github.com/
|
10
|
+
homepage: "http://alexeypetrushin.github.com/mongodb_model"
|
11
11
|
)
|
data/readme.md
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
**Documentation:** http://alexeypetrushin.github.com/mongodb_model
|
2
|
+
|
1
3
|
Object Model for MongoDB (callbacks, validations, mass-assignment, finders, ...).
|
2
4
|
|
3
5
|
- The same API for pure driver and Models.
|
@@ -14,61 +16,85 @@ Object Model for MongoDB (callbacks, validations, mass-assignment, finders, ...)
|
|
14
16
|
Other ODM usually try to cover simple but non-standard API of MongoDB behind complex ORM-like abstractions. This tool **exposes simplicity and power of MongoDB and leverages its differences**.
|
15
17
|
|
16
18
|
``` ruby
|
17
|
-
#
|
19
|
+
# Basic example of working with [Mongo Model][mongodb_model].
|
20
|
+
#
|
21
|
+
# In this example we'll create simple model and examine basic CRUD and
|
22
|
+
# querying operations.
|
18
23
|
require 'mongo/model'
|
19
|
-
Mongo.defaults.merge! symbolize: true, multi: true, safe: true
|
20
|
-
connection = Mongo::Connection.new
|
21
|
-
db = connection.db 'default_test'
|
22
|
-
db.units.drop
|
23
|
-
Mongo::Model.db = db
|
24
24
|
|
25
|
-
#
|
25
|
+
# Connecting to test database and cleaning it before starting.
|
26
|
+
Mongo::Model.default_database_name = :default_test
|
27
|
+
Mongo::Model.default_database.clear
|
28
|
+
|
29
|
+
# Let's define Game Unit.
|
30
|
+
# Models are just plain Ruby Objects, there's no any Attribute Scheme,
|
31
|
+
# Types, Proxies, or other complex stuff, just use standard Ruby practices.
|
26
32
|
class Unit
|
33
|
+
# Inheriting our Unit Class from Mongo::Model (the `inherit` keyword is
|
34
|
+
# just a simple shortcut including Module and its ClassMethods).
|
27
35
|
inherit Mongo::Model
|
36
|
+
|
37
|
+
# You can specify collection name explicitly or omit it and it will be
|
38
|
+
# guessed from the class name.
|
28
39
|
collection :units
|
29
40
|
|
41
|
+
# There's no need to define attributes, just use plain old Ruby technics to
|
42
|
+
# of working with objects.
|
30
43
|
attr_accessor :name, :status, :stats
|
31
44
|
|
32
|
-
|
45
|
+
def inspect; name end
|
46
|
+
end
|
47
|
+
|
48
|
+
# Stats conaining statistics about Unit (it will be embedded into the
|
49
|
+
# Unit).
|
50
|
+
#
|
51
|
+
# There are no difference between main and embedded objects, all of them
|
52
|
+
# are just standard Ruby objects.
|
53
|
+
class Unit::Stats
|
54
|
+
inherit Mongo::Model
|
33
55
|
|
34
|
-
|
35
|
-
inherit Mongo::Model
|
36
|
-
attr_accessor :attack, :life, :shield
|
37
|
-
end
|
56
|
+
attr_accessor :attack, :life, :shield
|
38
57
|
end
|
39
58
|
|
40
|
-
#
|
41
|
-
zeratul = Unit.
|
42
|
-
|
59
|
+
# Let's create two great Heroes.
|
60
|
+
zeratul = Unit.new name: 'Zeratul', status: 'alive'
|
61
|
+
zeratul.stats = Unit::Stats.new attack: 85, life: 300, shield: 100
|
62
|
+
|
63
|
+
tassadar = Unit.new name: 'Tassadar', status: 'dead'
|
64
|
+
tassadar.stats = Unit::Stats.new attack: 0, life: 80, shield: 300
|
43
65
|
|
44
|
-
|
45
|
-
|
66
|
+
# Saving units to database
|
67
|
+
p zeratul.save # => true
|
68
|
+
p tassadar.save # => true
|
46
69
|
|
47
|
-
#
|
70
|
+
# We made error - mistakenly set Tassadar's attack as zero, let's fix it.
|
48
71
|
tassadar.stats.attack = 20
|
49
|
-
tassadar.save
|
72
|
+
p tassadar.save # => true
|
50
73
|
|
51
|
-
# Querying
|
52
|
-
Unit.first
|
53
|
-
Unit.all
|
74
|
+
# Querying, use standard MongoDB query.
|
75
|
+
p Unit.first(name: 'Zeratul') # => Zeratul
|
76
|
+
p Unit.all(name: 'Zeratul') # => [Zeratul]
|
54
77
|
Unit.all name: 'Zeratul' do |unit|
|
55
|
-
unit
|
78
|
+
p unit # => Zeratul
|
56
79
|
end
|
57
80
|
|
58
|
-
# Simple finders (bang versions also availiable).
|
59
|
-
Unit.by_name
|
60
|
-
Unit.first_by_name
|
61
|
-
Unit.all_by_name
|
62
|
-
|
63
|
-
# Scopes.
|
64
|
-
Unit.alive.count # => 1
|
65
|
-
Unit.alive.first # => zeratul
|
81
|
+
# Simple dynamic finders (bang versions also availiable).
|
82
|
+
p Unit.by_name('Zeratul') # => Zeratul
|
83
|
+
p Unit.first_by_name('Zeratul') # => Zeratul
|
84
|
+
p Unit.all_by_name('Zeratul') # => [Zeratul]
|
66
85
|
|
67
|
-
#
|
86
|
+
# In this example we covered basics of [Mongo Model][mongodb_model],
|
87
|
+
# please go to [contents][mongodb_model] for more samples.
|
88
|
+
#
|
89
|
+
# [mongodb_model]: index.html
|
90
|
+
```
|
68
91
|
|
69
|
-
#
|
92
|
+
# Installation
|
70
93
|
|
71
|
-
|
94
|
+
``` bash
|
95
|
+
gem install mongodb_model
|
72
96
|
```
|
73
97
|
|
74
|
-
|
98
|
+
# License
|
99
|
+
|
100
|
+
Copyright (c) Alexey Petrushin, http://petrush.in, released under the MIT license.
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongodb_model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2011-09-15 00:00:00.000000000Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: mongodb
|
16
|
-
requirement: &
|
16
|
+
requirement: &2844160 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2844160
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: file_model
|
27
|
-
requirement: &
|
27
|
+
requirement: &2843920 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *2843920
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: validatable2
|
38
|
-
requirement: &
|
38
|
+
requirement: &2843680 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *2843680
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: ruby_ext
|
49
|
-
requirement: &
|
49
|
+
requirement: &2843440 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,7 +54,7 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :runtime
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *2843440
|
58
58
|
description:
|
59
59
|
email:
|
60
60
|
executables: []
|
@@ -95,7 +95,7 @@ files:
|
|
95
95
|
- spec/scope_spec.rb
|
96
96
|
- spec/spec_helper.rb
|
97
97
|
- spec/validation_spec.rb
|
98
|
-
homepage: http://github.com/
|
98
|
+
homepage: http://alexeypetrushin.github.com/mongodb_model
|
99
99
|
licenses: []
|
100
100
|
post_install_message:
|
101
101
|
rdoc_options: []
|