mongo_db 0.1.10 → 0.1.11
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/mongo_db/integration/locales/activemodel/ru.yml +27 -0
- data/lib/mongo_db/integration/locales/mongo_mapper/en.yml +4 -0
- data/lib/mongo_db/integration/locales/mongo_mapper/ru.yml +4 -0
- data/lib/mongo_db/integration/locales.rb +4 -0
- data/lib/mongo_db/model/assignment.rb +21 -10
- data/lib/mongo_db/model/attribute_convertors.rb +54 -0
- data/lib/mongo_db/model/crud.rb +30 -1
- data/lib/mongo_db/model/misc.rb +19 -0
- data/lib/mongo_db/model/model.rb +2 -1
- data/lib/mongo_db/model.rb +2 -1
- data/readme.md +10 -13
- data/spec/integration/am_conversion_spec.rb +1 -0
- data/spec/integration/am_validation_spec.rb +22 -28
- data/spec/model/assignment_spec.rb +11 -10
- data/spec/model/attribute_convertors_spec.rb +73 -0
- data/spec/model/crud_spec.rb +24 -6
- data/spec/model/misc_spec.rb +28 -2
- data/spec/model/query_spec.rb +2 -2
- data/spec/model/scope_spec.rb +3 -3
- data/spec/model/validation_spec.rb +2 -2
- metadata +7 -1
@@ -0,0 +1,27 @@
|
|
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: "должно быть четно"
|
@@ -14,22 +14,33 @@ module Mongo::Model::Assignment
|
|
14
14
|
protected
|
15
15
|
attr_reader :attributes
|
16
16
|
|
17
|
-
def method_missing attribute_name,
|
17
|
+
def method_missing attribute_name, *args
|
18
18
|
attribute_name.must_be.a Symbol
|
19
|
-
|
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
|
+
|
20
29
|
attributes[attribute_name] = [type, mass_assignment]
|
21
30
|
end
|
22
31
|
end
|
23
32
|
|
24
33
|
def set attributes, options = {}
|
25
|
-
if rules = self.class.
|
34
|
+
if rules = self.class._assign
|
26
35
|
force = options[:force]
|
27
36
|
attributes.each do |n, v|
|
28
37
|
n = n.to_sym
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
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
|
33
44
|
end
|
34
45
|
end
|
35
46
|
else
|
@@ -43,12 +54,12 @@ module Mongo::Model::Assignment
|
|
43
54
|
end
|
44
55
|
|
45
56
|
module ClassMethods
|
46
|
-
inheritable_accessor :
|
57
|
+
inheritable_accessor :_assign, nil
|
47
58
|
|
48
|
-
def
|
59
|
+
def assign &block
|
49
60
|
dsl = ::Mongo::Model::Assignment::Dsl.new
|
50
61
|
dsl.instance_eval &block
|
51
|
-
self.
|
62
|
+
self._assign = (_assign || {}).merge dsl.to_h
|
52
63
|
end
|
53
64
|
end
|
54
65
|
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
module Mongo::Model::AttributeConvertors
|
5
|
+
CONVERTORS = {
|
6
|
+
line: {
|
7
|
+
from_string: -> s {(s || "").split(',').collect{|s| s.strip}},
|
8
|
+
to_string: -> v {v.join(', ')}
|
9
|
+
},
|
10
|
+
column: {
|
11
|
+
from_string: -> s {(s || "").split("\n").collect{|s| s.strip}},
|
12
|
+
to_string: -> v {v.join("\n")}
|
13
|
+
},
|
14
|
+
yaml: {
|
15
|
+
from_string: -> s {YAML.load s rescue {}},
|
16
|
+
to_string: -> v {v.to_yaml.strip}
|
17
|
+
},
|
18
|
+
json: {
|
19
|
+
from_string: -> s {JSON.parse s rescue {}},
|
20
|
+
to_string: -> v {v.to_json.strip}
|
21
|
+
}
|
22
|
+
}
|
23
|
+
|
24
|
+
module ClassMethods
|
25
|
+
def available_as_string name, converter_name
|
26
|
+
converter = CONVERTORS[converter_name]
|
27
|
+
raise "unknown converter name :#{converter_name} for :#{name} field!" unless converter
|
28
|
+
|
29
|
+
from_string, to_string = converter[:from_string], converter[:to_string]
|
30
|
+
name_as_string = "#{name}_as_string".to_sym
|
31
|
+
define_method name_as_string do
|
32
|
+
_cache[name_as_string] ||= to_string.call(send(name))
|
33
|
+
end
|
34
|
+
|
35
|
+
define_method "#{name_as_string}=" do |value|
|
36
|
+
_cache.delete name_as_string
|
37
|
+
self.send "#{name}=", from_string.call(value)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def available_as_yaml name
|
42
|
+
raise "delimiter not specified for :#{name} field!" unless delimiter
|
43
|
+
method = "#{name}_as_string"
|
44
|
+
define_method method do
|
45
|
+
self.send(name).join(delimiter)
|
46
|
+
end
|
47
|
+
define_method "#{method}=" do |value|
|
48
|
+
value = (value || "").split(delimiter.strip).collect{|s| s.strip}
|
49
|
+
self.send "#{name}=", value
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
data/lib/mongo_db/model/crud.rb
CHANGED
@@ -16,7 +16,36 @@ module Mongo::Model::Crud
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def destroy! *args
|
19
|
-
destroy(*args)|| raise(Mongo::Error, "can't destroy #{self.inspect}!")
|
19
|
+
destroy(*args) || raise(Mongo::Error, "can't destroy #{self.inspect}!")
|
20
|
+
end
|
21
|
+
|
22
|
+
module ClassMethods
|
23
|
+
def build attributes, opts = {}
|
24
|
+
self.new.set attributes, opts
|
25
|
+
end
|
26
|
+
|
27
|
+
def create attributes, opts = {}
|
28
|
+
o = build attributes, opts
|
29
|
+
o.save
|
30
|
+
o
|
31
|
+
end
|
32
|
+
|
33
|
+
def create! attributes, opts = {}
|
34
|
+
o = create attributes
|
35
|
+
raise(Mongo::Error, "can't create #{attributes.inspect}!") if o.new_record?
|
36
|
+
o
|
37
|
+
end
|
38
|
+
|
39
|
+
def destroy_all selector = {}, opts = {}
|
40
|
+
success = true
|
41
|
+
collection = opts[:collection] || self.collection
|
42
|
+
each(selector){|o| success = false unless o.destroy}
|
43
|
+
success
|
44
|
+
end
|
45
|
+
|
46
|
+
def destroy_all! selector = {}, opts = {}
|
47
|
+
destroy_all(selector, opts) || raise(Mongo::Error, "can't destroy #{selector.inspect}!")
|
48
|
+
end
|
20
49
|
end
|
21
50
|
|
22
51
|
protected
|
data/lib/mongo_db/model/misc.rb
CHANGED
@@ -5,6 +5,25 @@ module Mongo::Model::Misc
|
|
5
5
|
self.updated_at = now
|
6
6
|
end
|
7
7
|
|
8
|
+
|
9
|
+
def _cache
|
10
|
+
@_cache ||= {}
|
11
|
+
end
|
12
|
+
def _clear_cache
|
13
|
+
@_cache = {}
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
def dom_id
|
18
|
+
# new_record? ? "new_#{self.class.name.underscore}" : to_param
|
19
|
+
to_param
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_param
|
23
|
+
(_id || '').to_s
|
24
|
+
end
|
25
|
+
|
26
|
+
|
8
27
|
module ClassMethods
|
9
28
|
def timestamps!
|
10
29
|
attr_accessor :created_at, :updated_at
|
data/lib/mongo_db/model/model.rb
CHANGED
data/lib/mongo_db/model.rb
CHANGED
@@ -19,12 +19,13 @@ module Mongo::Model; end
|
|
19
19
|
crud
|
20
20
|
query
|
21
21
|
scope
|
22
|
+
attribute_convertors
|
22
23
|
misc
|
23
24
|
model
|
24
25
|
).each{|f| require "mongo_db/model/#{f}"}
|
25
26
|
|
26
27
|
module Mongo
|
27
28
|
module Model
|
28
|
-
inherit Db, Assignment, Callbacks, Validation, Crud, Query, Scope, Misc
|
29
|
+
inherit Db, Assignment, Callbacks, Validation, Crud, Query, Scope, AttributeConvertors, Misc
|
29
30
|
end
|
30
31
|
end
|
data/readme.md
CHANGED
@@ -2,7 +2,7 @@ Object Model & Ruby driver enhancements for MongoDB.
|
|
2
2
|
|
3
3
|
1. Driver enchancements & Migrations.
|
4
4
|
2. Persistence for any Ruby object.
|
5
|
-
3. Object Model (callbacks, validations, mass-assignment, finders, ...)
|
5
|
+
3. Object Model (callbacks, validations, mass-assignment, finders, ...).
|
6
6
|
|
7
7
|
Lower layers are independent from upper, use only what You need.
|
8
8
|
|
@@ -124,11 +124,8 @@ db.units.all name: {_gt: 'Z'} # => [zeratul]
|
|
124
124
|
|
125
125
|
Source: examples/object.rb
|
126
126
|
|
127
|
-
# Object Model
|
127
|
+
# Object Model
|
128
128
|
|
129
|
-
Model designed after the excellent "Domain-Driven Design" book by Eric Evans.
|
130
|
-
|
131
|
-
- Very small, see [code stats][:code_stats].
|
132
129
|
- The same API for pure driver and Models.
|
133
130
|
- Minimum extra abstractions, trying to keep things as close to the MongoDB semantic as possible.
|
134
131
|
- Schema-less, dynamic (with ability to specify types for mass-assignment).
|
@@ -136,9 +133,9 @@ Model designed after the excellent "Domain-Driven Design" book by Eric Evans.
|
|
136
133
|
- Full support for embedded objects (validations, callbacks, ...).
|
137
134
|
- Scope, default_scope
|
138
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].
|
139
137
|
|
140
|
-
|
141
|
-
This ODM exposes simplicity of MongoDB and leverages it's differences.
|
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**.
|
142
139
|
|
143
140
|
``` ruby
|
144
141
|
# Connecting to MongoDB.
|
@@ -153,11 +150,11 @@ Mongo::Model.db = db
|
|
153
150
|
class Unit
|
154
151
|
inherit Mongo::Model
|
155
152
|
collection :units
|
156
|
-
|
153
|
+
|
157
154
|
attr_accessor :name, :status, :stats
|
158
|
-
|
155
|
+
|
159
156
|
scope :alive, status: 'alive'
|
160
|
-
|
157
|
+
|
161
158
|
class Stats
|
162
159
|
inherit Mongo::Model
|
163
160
|
attr_accessor :attack, :life, :shield
|
@@ -165,8 +162,8 @@ class Unit
|
|
165
162
|
end
|
166
163
|
|
167
164
|
# Create.
|
168
|
-
zeratul = Unit.
|
169
|
-
tassadar = Unit.
|
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))
|
170
167
|
|
171
168
|
zeratul.save
|
172
169
|
tassadar.save
|
@@ -264,4 +261,4 @@ Copyright (c) Alexey Petrushin, http://petrush.in, released under the MIT licens
|
|
264
261
|
|
265
262
|
[mongo_mapper_ext]: https://github.com/alexeypetrushin/mongo_mapper_ext
|
266
263
|
[mongoid_misc]: https://github.com/alexeypetrushin/mongoid_misc
|
267
|
-
[code_stats]: https://
|
264
|
+
[code_stats]: https://github.com/alexeypetrushin/mongo_db/raw/master/docs/code_stats.png
|
@@ -0,0 +1 @@
|
|
1
|
+
# to_json, to_xml
|
@@ -1,40 +1,34 @@
|
|
1
1
|
require 'model/spec_helper'
|
2
|
+
require 'active_model'
|
2
3
|
|
3
|
-
|
4
|
-
|
4
|
+
describe "Validations" do
|
5
|
+
with_mongo_model
|
5
6
|
|
6
|
-
|
7
|
-
|
7
|
+
before do
|
8
|
+
class Unit
|
9
|
+
inherit Mongo::Model
|
10
|
+
collection :units
|
8
11
|
|
9
|
-
|
10
|
-
class Unit
|
11
|
-
inherit Mongo::Model
|
12
|
-
collection :units
|
12
|
+
include ActiveModel::Validations
|
13
13
|
|
14
|
-
|
14
|
+
attr_accessor :name, :status
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
validates_presence_of :name
|
19
|
-
end
|
16
|
+
validates_presence_of :name
|
20
17
|
end
|
18
|
+
end
|
21
19
|
|
22
|
-
|
20
|
+
after{remove_constants :Unit}
|
23
21
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
22
|
+
it "ActiveModel integration smoke test" do
|
23
|
+
unit = Unit.new
|
24
|
+
unit.should be_invalid
|
25
|
+
unit.errors.size.should == 1
|
26
|
+
unit.errors.first.first.should == :name
|
27
|
+
unit.save.should be_false
|
30
28
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
end
|
29
|
+
unit.name = 'Zeratul'
|
30
|
+
unit.should be_valid
|
31
|
+
unit.errors.should be_empty
|
32
|
+
unit.save.should be_true
|
36
33
|
end
|
37
|
-
|
38
|
-
rescue LoadError => e
|
39
|
-
warn 'No ActiveModel, integration with ActiveModel::Validations spec will be skipped.'
|
40
34
|
end
|
@@ -9,31 +9,32 @@ describe 'Model callbacks' do
|
|
9
9
|
class User
|
10
10
|
inherit Mongo::Model
|
11
11
|
|
12
|
-
attr_accessor :name, :
|
12
|
+
attr_accessor :name, :has_mail, :age, :banned
|
13
13
|
end
|
14
14
|
|
15
15
|
u = User.new
|
16
|
-
u.set name: 'Alex',
|
17
|
-
[u.name, u.
|
16
|
+
u.set name: 'Alex', has_mail: '1', age: '31', banned: '0'
|
17
|
+
[u.name, u.has_mail, u.age, u.banned].should == ['Alex', '1', '31', '0']
|
18
18
|
end
|
19
19
|
|
20
20
|
it "should update only specified attributes" do
|
21
21
|
class User
|
22
22
|
inherit Mongo::Model
|
23
23
|
|
24
|
-
attr_accessor :name, :
|
24
|
+
attr_accessor :name, :has_mail, :age, :position, :banned
|
25
25
|
|
26
|
-
|
26
|
+
assign do
|
27
27
|
name String, true
|
28
|
-
|
28
|
+
has_mail Boolean, true
|
29
29
|
age Integer, true
|
30
|
+
position true
|
30
31
|
banned Boolean
|
31
32
|
end
|
32
33
|
end
|
33
34
|
|
34
35
|
u = User.new
|
35
|
-
u.set name: 'Alex',
|
36
|
-
[u.name, u.
|
36
|
+
u.set name: 'Alex', has_mail: '1', age: '31', position: [11, 34] ,banned: '0'
|
37
|
+
[u.name, u.has_mail, u.age, u.position, u.banned].should == ['Alex', true, 31, [11, 34], nil]
|
37
38
|
|
38
39
|
# should allow to forcefully cast and update any attribute
|
39
40
|
u.set! banned: '0'
|
@@ -46,7 +47,7 @@ describe 'Model callbacks' do
|
|
46
47
|
|
47
48
|
attr_accessor :age
|
48
49
|
|
49
|
-
|
50
|
+
assign do
|
50
51
|
age Integer, true
|
51
52
|
end
|
52
53
|
end
|
@@ -54,7 +55,7 @@ describe 'Model callbacks' do
|
|
54
55
|
class Writer < User
|
55
56
|
attr_accessor :posts
|
56
57
|
|
57
|
-
|
58
|
+
assign do
|
58
59
|
posts Integer, true
|
59
60
|
end
|
60
61
|
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'model/spec_helper'
|
2
|
+
|
3
|
+
describe "Attribute Convertors" do
|
4
|
+
with_mongo_model
|
5
|
+
|
6
|
+
after(:all){remove_constants :TheSample}
|
7
|
+
|
8
|
+
convertors = Mongo::Model::AttributeConvertors::CONVERTORS
|
9
|
+
|
10
|
+
it ":line convertor" do
|
11
|
+
v = ['a', 'b']
|
12
|
+
str_v = 'a, b'
|
13
|
+
convertors[:line][:from_string].call(str_v).should == v
|
14
|
+
convertors[:line][:to_string].call(v).should == str_v
|
15
|
+
end
|
16
|
+
|
17
|
+
it ":yaml convertor" do
|
18
|
+
v = {'a' => 'b'}
|
19
|
+
str_v = v.to_yaml.strip
|
20
|
+
|
21
|
+
convertors[:yaml][:from_string].call(str_v).should == v
|
22
|
+
convertors[:yaml][:to_string].call(v).should == str_v
|
23
|
+
end
|
24
|
+
|
25
|
+
it ":json convertor" do
|
26
|
+
v = {'a' => 'b'}
|
27
|
+
str_v = v.to_json.strip
|
28
|
+
convertors[:json][:from_string].call(str_v).should == v
|
29
|
+
convertors[:json][:to_string].call(v).should == str_v
|
30
|
+
end
|
31
|
+
|
32
|
+
it ":field should generate helper methods if :as_string option provided" do
|
33
|
+
class ::TheSample
|
34
|
+
inherit Mongo::Model
|
35
|
+
|
36
|
+
attr_accessor :tags, :protected_tags
|
37
|
+
available_as_string :tags, :line
|
38
|
+
available_as_string :protected_tags, :line
|
39
|
+
|
40
|
+
def initialize
|
41
|
+
@tags, @protected_tags = [], []
|
42
|
+
end
|
43
|
+
|
44
|
+
assign do
|
45
|
+
tags_as_string true
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
o = TheSample.new
|
50
|
+
|
51
|
+
# get
|
52
|
+
o.tags_as_string.should == ''
|
53
|
+
o.tags = %w(Java Ruby)
|
54
|
+
o._clear_cache
|
55
|
+
o.tags_as_string.should == 'Java, Ruby'
|
56
|
+
|
57
|
+
# set
|
58
|
+
o.tags_as_string = ''
|
59
|
+
o.tags.should == []
|
60
|
+
o.tags_as_string = 'Java, Ruby'
|
61
|
+
o.tags.should == %w(Java Ruby)
|
62
|
+
|
63
|
+
# mass assignment
|
64
|
+
o.tags = []
|
65
|
+
o.set tags_as_string: 'Java, Ruby'
|
66
|
+
o.tags.should == %w(Java Ruby)
|
67
|
+
|
68
|
+
# # protection
|
69
|
+
o.protected_tags = []
|
70
|
+
o.set protected_tags_as_string: 'Java, Ruby'
|
71
|
+
o.protected_tags.should == []
|
72
|
+
end
|
73
|
+
end
|
data/spec/model/crud_spec.rb
CHANGED
@@ -17,7 +17,7 @@ describe "Model CRUD" do
|
|
17
17
|
after(:all){remove_constants :Unit}
|
18
18
|
|
19
19
|
before do
|
20
|
-
@zeratul = Unit.
|
20
|
+
@zeratul = Unit.build name: 'Zeratul', info: 'Dark Templar'
|
21
21
|
end
|
22
22
|
|
23
23
|
it_should_behave_like "object CRUD"
|
@@ -72,9 +72,27 @@ describe "Model CRUD" do
|
|
72
72
|
db.heroes.count.should == 0
|
73
73
|
end
|
74
74
|
|
75
|
-
it '
|
75
|
+
it 'build' do
|
76
|
+
u = Unit.build name: 'Zeratul'
|
77
|
+
u.name.should == 'Zeratul'
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'create' do
|
81
|
+
u = Unit.create(name: 'Zeratul')
|
82
|
+
u.new_record?.should be_false
|
83
|
+
|
84
|
+
u = Unit.create!(name: 'Zeratul')
|
85
|
+
u.new_record?.should be_false
|
86
|
+
end
|
76
87
|
|
77
|
-
it 'destroy_all'
|
88
|
+
it 'destroy_all' do
|
89
|
+
Unit.create(name: 'Zeratul')
|
90
|
+
Unit.count.should == 1
|
91
|
+
Unit.destroy_all
|
92
|
+
Unit.count.should == 0
|
93
|
+
|
94
|
+
Unit.destroy_all!
|
95
|
+
end
|
78
96
|
end
|
79
97
|
|
80
98
|
describe 'embedded' do
|
@@ -100,8 +118,8 @@ describe "Model CRUD" do
|
|
100
118
|
@mission_class = Player::Mission
|
101
119
|
@player = Player.new
|
102
120
|
@player.missions = [
|
103
|
-
Player::Mission.
|
104
|
-
Player::Mission.
|
121
|
+
Player::Mission.build(name: 'Wasteland', stats: {buildings: 5, units: 10}),
|
122
|
+
Player::Mission.build(name: 'Backwater Station', stats: {buildings: 8, units: 25}),
|
105
123
|
]
|
106
124
|
end
|
107
125
|
|
@@ -119,7 +137,7 @@ describe "Model CRUD" do
|
|
119
137
|
|
120
138
|
# update
|
121
139
|
@player.missions.first.stats[:units] = 9
|
122
|
-
@player.missions << Player::Mission.
|
140
|
+
@player.missions << Player::Mission.build(name: 'Desperate Alliance', stats: {buildings: 11, units: 40})
|
123
141
|
@player.save.should be_true
|
124
142
|
Player.count.should == 1
|
125
143
|
Player.first.should == @player
|
data/spec/model/misc_spec.rb
CHANGED
@@ -3,7 +3,13 @@ require 'model/spec_helper'
|
|
3
3
|
describe 'Model Miscellaneous' do
|
4
4
|
with_mongo_model
|
5
5
|
|
6
|
-
|
6
|
+
before do
|
7
|
+
class User
|
8
|
+
inherit Mongo::Model
|
9
|
+
collection :users
|
10
|
+
end
|
11
|
+
end
|
12
|
+
after{remove_constants :Unit3, :User}
|
7
13
|
|
8
14
|
it "timestamps" do
|
9
15
|
class Unit3
|
@@ -15,7 +21,7 @@ describe 'Model Miscellaneous' do
|
|
15
21
|
timestamps!
|
16
22
|
end
|
17
23
|
|
18
|
-
unit = Unit3.
|
24
|
+
unit = Unit3.build name: 'Zeratul'
|
19
25
|
unit.save!
|
20
26
|
|
21
27
|
unit = Unit3.first
|
@@ -28,5 +34,25 @@ describe 'Model Miscellaneous' do
|
|
28
34
|
unit.updated_at.should > updated_at
|
29
35
|
end
|
30
36
|
|
37
|
+
it 'cache' do
|
38
|
+
class Unit3
|
39
|
+
inherit Mongo::Model
|
40
|
+
end
|
41
|
+
u = Unit3.new
|
42
|
+
u._cache.should == {}
|
43
|
+
end
|
44
|
+
|
45
|
+
it "to_param" do
|
46
|
+
u = User.new
|
47
|
+
u.to_param.should == ''
|
48
|
+
u.save!
|
49
|
+
u.to_param.should_not be_empty
|
50
|
+
end
|
31
51
|
|
52
|
+
it "dom_id" do
|
53
|
+
u = User.new
|
54
|
+
u.dom_id.should == ''
|
55
|
+
u.save!
|
56
|
+
u.dom_id.should_not be_empty
|
57
|
+
end
|
32
58
|
end
|
data/spec/model/query_spec.rb
CHANGED
@@ -14,7 +14,7 @@ describe "Model Query" do
|
|
14
14
|
end
|
15
15
|
after(:all){remove_constants :Unit}
|
16
16
|
|
17
|
-
before{@zeratul = Unit.
|
17
|
+
before{@zeratul = Unit.build name: 'Zeratul'}
|
18
18
|
|
19
19
|
it 'exist?' do
|
20
20
|
Unit.should_not exist(name: 'Zeratul')
|
@@ -41,7 +41,7 @@ describe "Model Query" do
|
|
41
41
|
|
42
42
|
it 'dynamic finders integration' do
|
43
43
|
Unit.first_by_name('Zeratul').should be_nil
|
44
|
-
Unit.
|
44
|
+
Unit.build(name: 'Zeratul').save!
|
45
45
|
Unit.first_by_name('Zeratul').name.should == 'Zeratul'
|
46
46
|
end
|
47
47
|
end
|
data/spec/model/scope_spec.rb
CHANGED
@@ -16,9 +16,9 @@ describe "Scope" do
|
|
16
16
|
|
17
17
|
describe 'current scope' do
|
18
18
|
it "should affect finders" do
|
19
|
-
Unit.
|
20
|
-
Unit.
|
21
|
-
Unit.
|
19
|
+
Unit.build(name: 'Zeratul', status: 'alive').save!
|
20
|
+
Unit.build(name: 'Jim', status: 'alive').save!
|
21
|
+
Unit.build(name: 'Tassadar', status: 'dead').save!
|
22
22
|
|
23
23
|
Unit.count.should == 3
|
24
24
|
Unit.all.size.should == 3
|
@@ -16,7 +16,7 @@ describe "Validations" do
|
|
16
16
|
after{remove_constants :Unit}
|
17
17
|
|
18
18
|
it "should not save model with errors" do
|
19
|
-
unit = Unit.
|
19
|
+
unit = Unit.build name: 'Zeratul'
|
20
20
|
unit.save.should be_true
|
21
21
|
|
22
22
|
unit.errors = []
|
@@ -30,7 +30,7 @@ describe "Validations" do
|
|
30
30
|
end
|
31
31
|
|
32
32
|
it "should check :errors only and ignore valid? method" do
|
33
|
-
unit = Unit.
|
33
|
+
unit = Unit.build name: 'Zeratul'
|
34
34
|
unit.should_not_receive(:valid?)
|
35
35
|
unit.save.should be_true
|
36
36
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongo_db
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.11
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -36,11 +36,16 @@ files:
|
|
36
36
|
- lib/mongo_db/driver/spec.rb
|
37
37
|
- lib/mongo_db/driver.rb
|
38
38
|
- lib/mongo_db/gems.rb
|
39
|
+
- lib/mongo_db/integration/locales/activemodel/ru.yml
|
40
|
+
- lib/mongo_db/integration/locales/mongo_mapper/en.yml
|
41
|
+
- lib/mongo_db/integration/locales/mongo_mapper/ru.yml
|
42
|
+
- lib/mongo_db/integration/locales.rb
|
39
43
|
- lib/mongo_db/migration/definition.rb
|
40
44
|
- lib/mongo_db/migration/migration.rb
|
41
45
|
- lib/mongo_db/migration/tasks.rb
|
42
46
|
- lib/mongo_db/migration.rb
|
43
47
|
- lib/mongo_db/model/assignment.rb
|
48
|
+
- lib/mongo_db/model/attribute_convertors.rb
|
44
49
|
- lib/mongo_db/model/callbacks.rb
|
45
50
|
- lib/mongo_db/model/crud.rb
|
46
51
|
- lib/mongo_db/model/db.rb
|
@@ -66,6 +71,7 @@ files:
|
|
66
71
|
- spec/integration/am_validation_spec.rb
|
67
72
|
- spec/migration/migration_spec.rb
|
68
73
|
- spec/model/assignment_spec.rb
|
74
|
+
- spec/model/attribute_convertors_spec.rb
|
69
75
|
- spec/model/callbacks_spec.rb
|
70
76
|
- spec/model/crud_spec.rb
|
71
77
|
- spec/model/db_spec.rb
|