mongodb 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/lib/mongodb/object/object_helper.rb +2 -2
- data/readme.md +56 -129
- data/spec/object/crud_spec.rb +1 -1
- metadata +8 -36
- data/lib/mongodb/integration/locales.rb +0 -4
- data/lib/mongodb/integration/locales/activemodel/ru.yml +0 -27
- data/lib/mongodb/model.rb +0 -26
- data/lib/mongodb/model/assignment.rb +0 -65
- data/lib/mongodb/model/attribute_convertors.rb +0 -54
- data/lib/mongodb/model/callbacks.rb +0 -36
- data/lib/mongodb/model/crud.rb +0 -57
- data/lib/mongodb/model/db.rb +0 -53
- data/lib/mongodb/model/misc.rb +0 -33
- data/lib/mongodb/model/model.rb +0 -11
- data/lib/mongodb/model/query.rb +0 -36
- data/lib/mongodb/model/scope.rb +0 -99
- data/lib/mongodb/model/spec.rb +0 -12
- data/lib/mongodb/model/support/types.rb +0 -110
- data/lib/mongodb/model/validation.rb +0 -5
- data/spec/integration/am_conversion_spec.rb +0 -1
- data/spec/integration/am_validation_spec.rb +0 -34
- data/spec/integration/validatable2_spec.rb +0 -40
- data/spec/model/assignment_spec.rb +0 -80
- data/spec/model/attribute_convertors_spec.rb +0 -73
- data/spec/model/callbacks_spec.rb +0 -47
- data/spec/model/crud_spec.rb +0 -151
- data/spec/model/db_spec.rb +0 -63
- data/spec/model/misc_spec.rb +0 -58
- data/spec/model/query_spec.rb +0 -47
- data/spec/model/scope_spec.rb +0 -149
- data/spec/model/spec_helper.rb +0 -4
- data/spec/model/validation_spec.rb +0 -37
data/Rakefile
CHANGED
@@ -34,7 +34,7 @@ module Mongo::ObjectHelper
|
|
34
34
|
::Mongo::ObjectSerializer.new(arg).remove opts, self
|
35
35
|
end
|
36
36
|
end
|
37
|
-
|
37
|
+
|
38
38
|
def save! doc, opts = {}
|
39
39
|
save(doc, opts) || raise(Mongo::Error, "can't save #{doc.inspect}!")
|
40
40
|
end
|
@@ -49,7 +49,7 @@ module Mongo::ObjectHelper
|
|
49
49
|
doc = super selector, opts, &block
|
50
50
|
object ? ::Mongo::ObjectSerializer.build(doc) : doc
|
51
51
|
end
|
52
|
-
|
52
|
+
|
53
53
|
def each selector = {}, opts = {}, &block
|
54
54
|
opts = opts.clone
|
55
55
|
object = (opts.delete(:object) == false) ? false : true
|
data/readme.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
|
1
|
+
Ruby object persistence & driver enhancements for MongoDB.
|
2
2
|
|
3
|
-
1. Driver enchancements
|
4
|
-
2.
|
5
|
-
3.
|
3
|
+
1. Driver enchancements.
|
4
|
+
2. Migrations.
|
5
|
+
3. Persistence for any Ruby object.
|
6
6
|
|
7
7
|
Lower layers are independent from upper, use only what You need.
|
8
8
|
|
@@ -62,6 +62,58 @@ Source: examples/driver.rb
|
|
62
62
|
More docs - there's no need for more docs, the whole point of this extension is to be small, intuitive, 100% compatible with the official driver, and require no extra knowledge.
|
63
63
|
So, please use standard Ruby driver documentation.
|
64
64
|
|
65
|
+
# Migrations
|
66
|
+
|
67
|
+
Define migration steps, specify desired version and apply it (usually all this should be done via Rake task).
|
68
|
+
|
69
|
+
``` ruby
|
70
|
+
require 'mongodb/migration'
|
71
|
+
|
72
|
+
# Connection & db.
|
73
|
+
connection = Mongo::Connection.new
|
74
|
+
db = connection.db 'default_test'
|
75
|
+
db.units.drop
|
76
|
+
|
77
|
+
# Initialize migration (usually all this should be done inside of :migrate
|
78
|
+
# rake task).
|
79
|
+
migration = Mongo::Migration.new db
|
80
|
+
|
81
|
+
# Define migrations.
|
82
|
+
# Usually they are defined as files in some folder and You loading it by
|
83
|
+
# using something like this:
|
84
|
+
# Dir['<runtime_dir>/db/migrations/*.rb'].each{|fname| load fname}
|
85
|
+
migration.add 1 do |m|
|
86
|
+
m.up{|db| db.units.save name: 'Zeratul'}
|
87
|
+
m.down{|db| db.units.remove name: 'Zeratul'}
|
88
|
+
end
|
89
|
+
|
90
|
+
# Let's add another one.
|
91
|
+
migration.add 2 do |m|
|
92
|
+
m.up{|db| db.units.save name: 'Tassadar'}
|
93
|
+
m.down{|db| db.units.remove name: 'Tassadar'}
|
94
|
+
end
|
95
|
+
|
96
|
+
# Specify what version of database You need and apply migration.
|
97
|
+
migration.update 2
|
98
|
+
|
99
|
+
migration.current_version # => 2
|
100
|
+
db.units.count # => 2
|
101
|
+
|
102
|
+
# You can rollback it the same way.
|
103
|
+
migration.update 0
|
104
|
+
|
105
|
+
migration.current_version # => 0
|
106
|
+
db.units.count # => 0
|
107
|
+
|
108
|
+
# To update to the highest version just call it without the version specified
|
109
|
+
migration.update
|
110
|
+
|
111
|
+
migration.current_version # => 2
|
112
|
+
db.units.count # => 2
|
113
|
+
```
|
114
|
+
|
115
|
+
Source: examples/migration.rb
|
116
|
+
|
65
117
|
# Persistence for any Ruby object
|
66
118
|
|
67
119
|
Save any Ruby object to MongoDB, as if it's a document. Objects can be any type, simple or composite with other objects / arrays / hashes inside.
|
@@ -124,131 +176,6 @@ db.units.all name: {_gt: 'Z'} # => [zeratul]
|
|
124
176
|
|
125
177
|
Source: examples/object.rb
|
126
178
|
|
127
|
-
# Object Model
|
128
|
-
|
129
|
-
- The same API for pure driver and Models.
|
130
|
-
- Minimum extra abstractions, trying to keep things as close to the MongoDB semantic as possible.
|
131
|
-
- Schema-less, dynamic (with ability to specify types for mass-assignment).
|
132
|
-
- Models can be saved to any collection.
|
133
|
-
- Full support for embedded objects (validations, callbacks, ...).
|
134
|
-
- Scope, default_scope
|
135
|
-
- Doesn't try to mimic ActiveRecord, MongoDB is differrent and this tool designed to get most of it.
|
136
|
-
- Very small, see [code stats][code_stats].
|
137
|
-
|
138
|
-
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 it's differences**.
|
139
|
-
|
140
|
-
``` ruby
|
141
|
-
# Connecting to MongoDB.
|
142
|
-
require 'mongodb/model'
|
143
|
-
Mongo.defaults.merge! symbolize: true, multi: true, safe: true
|
144
|
-
connection = Mongo::Connection.new
|
145
|
-
db = connection.db 'default_test'
|
146
|
-
db.units.drop
|
147
|
-
Mongo::Model.db = db
|
148
|
-
|
149
|
-
# Let's define the game unit.
|
150
|
-
class Unit
|
151
|
-
inherit Mongo::Model
|
152
|
-
collection :units
|
153
|
-
|
154
|
-
attr_accessor :name, :status, :stats
|
155
|
-
|
156
|
-
scope :alive, status: 'alive'
|
157
|
-
|
158
|
-
class Stats
|
159
|
-
inherit Mongo::Model
|
160
|
-
attr_accessor :attack, :life, :shield
|
161
|
-
end
|
162
|
-
end
|
163
|
-
|
164
|
-
# Create.
|
165
|
-
zeratul = Unit.build(name: 'Zeratul', status: 'alive', stats: Unit::Stats.build(attack: 85, life: 300, shield: 100))
|
166
|
-
tassadar = Unit.build(name: 'Tassadar', status: 'dead', stats: Unit::Stats.build(attack: 0, life: 80, shield: 300))
|
167
|
-
|
168
|
-
zeratul.save
|
169
|
-
tassadar.save
|
170
|
-
|
171
|
-
# Udate (we made error - mistakenly set Tassadar's attack as zero, let's fix it).
|
172
|
-
tassadar.stats.attack = 20
|
173
|
-
tassadar.save
|
174
|
-
|
175
|
-
# Querying first & all, there's also :each, the same as :all.
|
176
|
-
Unit.first name: 'Zeratul' # => zeratul
|
177
|
-
Unit.all name: 'Zeratul' # => [zeratul]
|
178
|
-
Unit.all name: 'Zeratul' do |unit|
|
179
|
-
unit # => zeratul
|
180
|
-
end
|
181
|
-
|
182
|
-
# Simple finders (bang versions also availiable).
|
183
|
-
Unit.by_name 'Zeratul' # => zeratul
|
184
|
-
Unit.first_by_name 'Zeratul' # => zeratul
|
185
|
-
Unit.all_by_name 'Zeratul' # => [zeratul]
|
186
|
-
|
187
|
-
# Scopes.
|
188
|
-
Unit.alive.count # => 1
|
189
|
-
Unit.alive.first # => zeratul
|
190
|
-
|
191
|
-
# Callbacks & callbacks on embedded models.
|
192
|
-
|
193
|
-
# Validations.
|
194
|
-
|
195
|
-
# Save model to any collection.
|
196
|
-
```
|
197
|
-
|
198
|
-
Source: examples/model.rb
|
199
|
-
|
200
|
-
# Migrations
|
201
|
-
|
202
|
-
Define migration steps, specify desired version and apply it (usually all this should be done via Rake task).
|
203
|
-
|
204
|
-
``` ruby
|
205
|
-
require 'mongodb/migration'
|
206
|
-
|
207
|
-
# Connection & db.
|
208
|
-
connection = Mongo::Connection.new
|
209
|
-
db = connection.db 'default_test'
|
210
|
-
db.units.drop
|
211
|
-
|
212
|
-
# Initialize migration (usually all this should be done inside of :migrate
|
213
|
-
# rake task).
|
214
|
-
migration = Mongo::Migration.new db
|
215
|
-
|
216
|
-
# Define migrations.
|
217
|
-
# Usually they are defined as files in some folder and You loading it by
|
218
|
-
# using something like this:
|
219
|
-
# Dir['<runtime_dir>/db/migrations/*.rb'].each{|fname| load fname}
|
220
|
-
migration.add 1 do |m|
|
221
|
-
m.up{|db| db.units.save name: 'Zeratul'}
|
222
|
-
m.down{|db| db.units.remove name: 'Zeratul'}
|
223
|
-
end
|
224
|
-
|
225
|
-
# Let's add another one.
|
226
|
-
migration.add 2 do |m|
|
227
|
-
m.up{|db| db.units.save name: 'Tassadar'}
|
228
|
-
m.down{|db| db.units.remove name: 'Tassadar'}
|
229
|
-
end
|
230
|
-
|
231
|
-
# Specify what version of database You need and apply migration.
|
232
|
-
migration.update 2
|
233
|
-
|
234
|
-
migration.current_version # => 2
|
235
|
-
db.units.count # => 2
|
236
|
-
|
237
|
-
# You can rollback it the same way.
|
238
|
-
migration.update 0
|
239
|
-
|
240
|
-
migration.current_version # => 0
|
241
|
-
db.units.count # => 0
|
242
|
-
|
243
|
-
# To update to the highest version just call it without the version specified
|
244
|
-
migration.update
|
245
|
-
|
246
|
-
migration.current_version # => 2
|
247
|
-
db.units.count # => 2
|
248
|
-
```
|
249
|
-
|
250
|
-
Source: examples/migration.rb
|
251
|
-
|
252
179
|
# Installation
|
253
180
|
|
254
181
|
``` bash
|
data/spec/object/crud_spec.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongodb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2011-08-28 00:00:00.000000000Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: mongo
|
16
|
-
requirement: &
|
16
|
+
requirement: &2820100 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '1.3'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2820100
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: i18n
|
27
|
-
requirement: &
|
27
|
+
requirement: &2784980 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0.5'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *2784980
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: ruby_ext
|
38
|
-
requirement: &
|
38
|
+
requirement: &2784710 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *2784710
|
47
47
|
description:
|
48
48
|
email:
|
49
49
|
executables: []
|
@@ -58,25 +58,10 @@ files:
|
|
58
58
|
- lib/mongodb/driver/spec.rb
|
59
59
|
- lib/mongodb/driver.rb
|
60
60
|
- lib/mongodb/gems.rb
|
61
|
-
- lib/mongodb/integration/locales/activemodel/ru.yml
|
62
|
-
- lib/mongodb/integration/locales.rb
|
63
61
|
- lib/mongodb/migration/definition.rb
|
64
62
|
- lib/mongodb/migration/migration.rb
|
65
63
|
- lib/mongodb/migration/tasks.rb
|
66
64
|
- lib/mongodb/migration.rb
|
67
|
-
- lib/mongodb/model/assignment.rb
|
68
|
-
- lib/mongodb/model/attribute_convertors.rb
|
69
|
-
- lib/mongodb/model/callbacks.rb
|
70
|
-
- lib/mongodb/model/crud.rb
|
71
|
-
- lib/mongodb/model/db.rb
|
72
|
-
- lib/mongodb/model/misc.rb
|
73
|
-
- lib/mongodb/model/model.rb
|
74
|
-
- lib/mongodb/model/query.rb
|
75
|
-
- lib/mongodb/model/scope.rb
|
76
|
-
- lib/mongodb/model/spec.rb
|
77
|
-
- lib/mongodb/model/support/types.rb
|
78
|
-
- lib/mongodb/model/validation.rb
|
79
|
-
- lib/mongodb/model.rb
|
80
65
|
- lib/mongodb/object/object_helper.rb
|
81
66
|
- lib/mongodb/object/object_serializer.rb
|
82
67
|
- lib/mongodb/object.rb
|
@@ -87,20 +72,7 @@ files:
|
|
87
72
|
- spec/driver/fixes_spec.rb
|
88
73
|
- spec/driver/hash_helper_spec.rb
|
89
74
|
- spec/driver/spec_helper.rb
|
90
|
-
- spec/integration/am_conversion_spec.rb
|
91
|
-
- spec/integration/am_validation_spec.rb
|
92
|
-
- spec/integration/validatable2_spec.rb
|
93
75
|
- spec/migration/migration_spec.rb
|
94
|
-
- spec/model/assignment_spec.rb
|
95
|
-
- spec/model/attribute_convertors_spec.rb
|
96
|
-
- spec/model/callbacks_spec.rb
|
97
|
-
- spec/model/crud_spec.rb
|
98
|
-
- spec/model/db_spec.rb
|
99
|
-
- spec/model/misc_spec.rb
|
100
|
-
- spec/model/query_spec.rb
|
101
|
-
- spec/model/scope_spec.rb
|
102
|
-
- spec/model/spec_helper.rb
|
103
|
-
- spec/model/validation_spec.rb
|
104
76
|
- spec/object/callbacks_spec.rb
|
105
77
|
- spec/object/crud_shared.rb
|
106
78
|
- spec/object/crud_spec.rb
|
@@ -129,5 +101,5 @@ rubyforge_project:
|
|
129
101
|
rubygems_version: 1.8.6
|
130
102
|
signing_key:
|
131
103
|
specification_version: 3
|
132
|
-
summary:
|
104
|
+
summary: Save any Ruby object to MongoDB
|
133
105
|
test_files: []
|
@@ -1,27 +0,0 @@
|
|
1
|
-
ru:
|
2
|
-
errors:
|
3
|
-
# The default format to use in full error messages.
|
4
|
-
format: "%{attribute} %{message}"
|
5
|
-
|
6
|
-
# The values :model, :attribute and :value are always available for interpolation
|
7
|
-
# The value :count is available when applicable. Can be used for pluralization.
|
8
|
-
messages:
|
9
|
-
inclusion: "недопустимое значение"
|
10
|
-
exclusion: "недопустимо"
|
11
|
-
invalid: "неверно"
|
12
|
-
confirmation: "не совпадает с подтверждением"
|
13
|
-
accepted: "долно быть принято"
|
14
|
-
empty: "не может быть пустым"
|
15
|
-
blank: "не может быть пустым"
|
16
|
-
too_long: "слишком длинно (максимально допустимо %{count})"
|
17
|
-
too_short: "слищком коротко (минимально допустимо %{count})"
|
18
|
-
wrong_length: "неверная длинна (должно быть %{count})"
|
19
|
-
not_a_number: "не номер"
|
20
|
-
not_an_integer: "долно быть целочисленно"
|
21
|
-
greater_than: "должно быть больше чем %{count}"
|
22
|
-
greater_than_or_equal_to: "должно быть больше чем или равно %{count}"
|
23
|
-
equal_to: "должно быть равно %{count}"
|
24
|
-
less_than: "должно быть меньше чем %{count}"
|
25
|
-
less_than_or_equal_to: "должно быть меньше чем или равно %{count}"
|
26
|
-
odd: "должно быть не четно"
|
27
|
-
even: "должно быть четно"
|
data/lib/mongodb/model.rb
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
require 'mongodb/object'
|
2
|
-
require 'ruby_ext'
|
3
|
-
require 'i18n'
|
4
|
-
|
5
|
-
module Mongo::Model; end
|
6
|
-
|
7
|
-
%w(
|
8
|
-
support/types
|
9
|
-
|
10
|
-
db
|
11
|
-
assignment
|
12
|
-
callbacks
|
13
|
-
validation
|
14
|
-
crud
|
15
|
-
query
|
16
|
-
scope
|
17
|
-
attribute_convertors
|
18
|
-
misc
|
19
|
-
model
|
20
|
-
).each{|f| require "mongodb/model/#{f}"}
|
21
|
-
|
22
|
-
module Mongo
|
23
|
-
module Model
|
24
|
-
inherit Db, Assignment, Callbacks, Validation, Crud, Query, Scope, AttributeConvertors, Misc
|
25
|
-
end
|
26
|
-
end
|
@@ -1,65 +0,0 @@
|
|
1
|
-
module Mongo::Model::Assignment
|
2
|
-
class Dsl < BasicObject
|
3
|
-
def initialize
|
4
|
-
@attributes = {}
|
5
|
-
end
|
6
|
-
|
7
|
-
def self.const_missing name
|
8
|
-
# BasicObject doesn't have access to any constants like String, Symbol, ...
|
9
|
-
::Object.const_get name
|
10
|
-
end
|
11
|
-
|
12
|
-
def to_h; attributes end
|
13
|
-
|
14
|
-
protected
|
15
|
-
attr_reader :attributes
|
16
|
-
|
17
|
-
def method_missing attribute_name, *args
|
18
|
-
attribute_name.must_be.a Symbol
|
19
|
-
|
20
|
-
args.size.must_be.in 1..2
|
21
|
-
if args.first.is_a? Class
|
22
|
-
type, mass_assignment = args
|
23
|
-
mass_assignment ||= false
|
24
|
-
type.must.respond_to :cast
|
25
|
-
else
|
26
|
-
type, mass_assignment = nil, args.first
|
27
|
-
end
|
28
|
-
|
29
|
-
attributes[attribute_name] = [type, mass_assignment]
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
def set attributes, options = {}
|
34
|
-
if rules = self.class._assign
|
35
|
-
force = options[:force]
|
36
|
-
attributes.each do |n, v|
|
37
|
-
n = n.to_sym
|
38
|
-
if rule = rules[n]
|
39
|
-
type, mass_assignment = rule
|
40
|
-
if mass_assignment or force
|
41
|
-
v = type.cast(v) if type
|
42
|
-
send "#{n}=", v
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
else
|
47
|
-
attributes.each{|n, v| send "#{n}=", v}
|
48
|
-
end
|
49
|
-
self
|
50
|
-
end
|
51
|
-
|
52
|
-
def set! attributes, options = {}
|
53
|
-
set attributes, options.merge(force: true)
|
54
|
-
end
|
55
|
-
|
56
|
-
module ClassMethods
|
57
|
-
inheritable_accessor :_assign, nil
|
58
|
-
|
59
|
-
def assign &block
|
60
|
-
dsl = ::Mongo::Model::Assignment::Dsl.new
|
61
|
-
dsl.instance_eval &block
|
62
|
-
self._assign = (_assign || {}).merge dsl.to_h
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|