mongodb_model 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/{mongodb_model → mongo/model}/assignment.rb +0 -0
- data/lib/{mongodb_model → mongo/model}/attribute_convertors.rb +0 -0
- data/lib/{mongodb_model → mongo/model}/callbacks.rb +0 -10
- data/lib/{mongodb_model → mongo/model}/crud.rb +3 -3
- data/lib/{mongodb_model → mongo/model}/db.rb +0 -0
- data/lib/mongo/model/file_model.rb +27 -0
- data/lib/{mongodb_model → mongo/model}/misc.rb +4 -0
- data/lib/{mongodb_model → mongo/model}/model.rb +2 -0
- data/lib/{mongodb_model → mongo/model}/query.rb +0 -0
- data/lib/{mongodb_model → mongo/model}/scope.rb +0 -0
- data/lib/{mongodb_model → mongo/model}/spec.rb +1 -1
- data/lib/{mongodb_model → mongo/model}/support/types.rb +0 -0
- data/lib/mongo/model/validation/uniqueness_validator.rb +30 -0
- data/lib/mongo/model/validation.rb +29 -0
- data/lib/{mongodb_model.rb → mongo/model.rb} +17 -4
- data/lib/{mongodb_model → mongo_model}/gems.rb +3 -2
- data/readme.md +1 -1
- data/spec/callbacks_spec.rb +0 -11
- data/spec/crud_spec.rb +1 -1
- data/spec/file_model_spec.rb +21 -0
- data/spec/spec_helper.rb +2 -2
- data/spec/validation_spec.rb +94 -17
- metadata +20 -62
- data/lib/mongodb_model/validation.rb +0 -5
- data/spec/validatable2_spec.rb +0 -40
File without changes
|
File without changes
|
@@ -1,16 +1,6 @@
|
|
1
1
|
module Mongo::Model::Callbacks
|
2
2
|
inherit RubyExt::Callbacks
|
3
3
|
|
4
|
-
def _run_callbacks type, method_name
|
5
|
-
if type == :before
|
6
|
-
run_before_callbacks method_name, method: method_name
|
7
|
-
elsif type == :after
|
8
|
-
run_after_callbacks method_name, method: method_name
|
9
|
-
else
|
10
|
-
raise "invalid callback type (#{type})!"
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
4
|
module ClassMethods
|
15
5
|
[:validate, :update, :save, :destroy].each do |method_name|
|
16
6
|
define_method "before_#{method_name}" do |*args, &block|
|
@@ -20,17 +20,17 @@ module Mongo::Model::Crud
|
|
20
20
|
end
|
21
21
|
|
22
22
|
module ClassMethods
|
23
|
-
def build attributes, opts = {}
|
23
|
+
def build attributes = {}, opts = {}
|
24
24
|
self.new.set attributes, opts
|
25
25
|
end
|
26
26
|
|
27
|
-
def create attributes, opts = {}
|
27
|
+
def create attributes = {}, opts = {}
|
28
28
|
o = build attributes, opts
|
29
29
|
o.save
|
30
30
|
o
|
31
31
|
end
|
32
32
|
|
33
|
-
def create! attributes, opts = {}
|
33
|
+
def create! attributes = {}, opts = {}
|
34
34
|
o = create attributes
|
35
35
|
raise(Mongo::Error, "can't create #{attributes.inspect}!") if o.new_record?
|
36
36
|
o
|
File without changes
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Mongo::Model::FileModel
|
2
|
+
inherit ::FileModel::Helper
|
3
|
+
|
4
|
+
def attribute_get name
|
5
|
+
instance_variable_get :"@#{name}"
|
6
|
+
end
|
7
|
+
|
8
|
+
def attribute_set name, value
|
9
|
+
instance_variable_set :"@#{name}", value
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
def mount_file attr_name, file_model_class
|
14
|
+
super
|
15
|
+
|
16
|
+
before_validate do |model|
|
17
|
+
file_model = model.send(attr_name)
|
18
|
+
file_model.validate
|
19
|
+
model.errors[attr_name] = file_model.errors unless file_model.errors.empty?
|
20
|
+
end
|
21
|
+
|
22
|
+
after_save{|model| model.send(attr_name).save}
|
23
|
+
|
24
|
+
after_destroy{|model| model.send(attr_name).destroy}
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1,30 @@
|
|
1
|
+
class Mongo::Model::UniquenessValidator < Validatable::ValidationBase
|
2
|
+
attr_accessor :scope, :case_sensitive
|
3
|
+
|
4
|
+
def initialize(klass, attribute, options={})
|
5
|
+
super
|
6
|
+
self.case_sensitive = false if case_sensitive == nil
|
7
|
+
end
|
8
|
+
|
9
|
+
def valid?(instance)
|
10
|
+
conditions = {}
|
11
|
+
|
12
|
+
conditions[scope] = instance.send scope if scope
|
13
|
+
|
14
|
+
value = instance.send attribute
|
15
|
+
if case_sensitive
|
16
|
+
conditions[attribute] = value
|
17
|
+
else
|
18
|
+
conditions[attribute] = /^#{Regexp.escape(value.to_s)}$/i
|
19
|
+
end
|
20
|
+
|
21
|
+
# Make sure we're not including the current document in the query
|
22
|
+
conditions[:_id] = {_ne: instance._id} if instance._id
|
23
|
+
|
24
|
+
!klass.exists?(conditions)
|
25
|
+
end
|
26
|
+
|
27
|
+
def message(instance)
|
28
|
+
super || "#{attribute} must be unique!"
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Mongo::Model::Validation
|
2
|
+
def errors
|
3
|
+
@_errors ||= Validatable::Errors.new
|
4
|
+
end
|
5
|
+
|
6
|
+
def run_validations
|
7
|
+
self.class.validations.each{|v| v.validate self}
|
8
|
+
true
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
include ::Validatable::Macros
|
13
|
+
|
14
|
+
inheritable_accessor :validations, []
|
15
|
+
|
16
|
+
def validates_uniqueness_of *args
|
17
|
+
add_validations(args, Mongo::Model::UniquenessValidator)
|
18
|
+
end
|
19
|
+
|
20
|
+
protected
|
21
|
+
def add_validations(args, klass)
|
22
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
23
|
+
args.each do |attribute|
|
24
|
+
new_validation = klass.new self, attribute, options
|
25
|
+
validations << new_validation
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -1,9 +1,10 @@
|
|
1
|
-
require '
|
1
|
+
require 'mongo_model/gems'
|
2
2
|
|
3
3
|
require 'validatable'
|
4
|
+
require 'file_model'
|
4
5
|
require 'i18n'
|
5
6
|
require 'ruby_ext'
|
6
|
-
require '
|
7
|
+
require 'mongo/object'
|
7
8
|
|
8
9
|
module Mongo::Model; end
|
9
10
|
|
@@ -14,17 +15,29 @@ module Mongo::Model; end
|
|
14
15
|
assignment
|
15
16
|
callbacks
|
16
17
|
validation
|
18
|
+
validation/uniqueness_validator
|
17
19
|
crud
|
18
20
|
query
|
19
21
|
scope
|
20
22
|
attribute_convertors
|
23
|
+
file_model
|
21
24
|
misc
|
22
25
|
model
|
23
|
-
).each{|f| require "
|
26
|
+
).each{|f| require "mongo/model/#{f}"}
|
24
27
|
|
25
28
|
module Mongo
|
26
29
|
module Model
|
27
|
-
inherit
|
30
|
+
inherit \
|
31
|
+
Db,
|
32
|
+
Assignment,
|
33
|
+
Callbacks,
|
34
|
+
Validation,
|
35
|
+
Crud,
|
36
|
+
Query,
|
37
|
+
Scope,
|
38
|
+
AttributeConvertors,
|
39
|
+
Mongo::Model::FileModel,
|
40
|
+
Misc
|
28
41
|
end
|
29
42
|
end
|
30
43
|
|
data/readme.md
CHANGED
@@ -13,7 +13,7 @@ Other ODM usually try to cover simple but non-standard API of MongoDB behind com
|
|
13
13
|
|
14
14
|
``` ruby
|
15
15
|
# Connecting to MongoDB.
|
16
|
-
require '
|
16
|
+
require 'mongo/model'
|
17
17
|
Mongo.defaults.merge! symbolize: true, multi: true, safe: true
|
18
18
|
connection = Mongo::Connection.new
|
19
19
|
db = connection.db 'default_test'
|
data/spec/callbacks_spec.rb
CHANGED
@@ -5,17 +5,6 @@ describe 'Model callbacks' do
|
|
5
5
|
|
6
6
|
after(:all){remove_constants :TheModel, :Player}
|
7
7
|
|
8
|
-
it "callback integration" do
|
9
|
-
class TheModel
|
10
|
-
inherit Mongo::Model
|
11
|
-
end
|
12
|
-
|
13
|
-
model = TheModel.new
|
14
|
-
|
15
|
-
model.should_receive(:run_before_callbacks).with(:save, {method: :save})
|
16
|
-
model._run_callbacks :before, :save
|
17
|
-
end
|
18
|
-
|
19
8
|
it "integration smoke test" do
|
20
9
|
class Player
|
21
10
|
inherit Mongo::Model
|
data/spec/crud_spec.rb
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'file_model/spec/shared_crud'
|
3
|
+
|
4
|
+
describe 'File Model' do
|
5
|
+
with_mongo_model
|
6
|
+
it_should_behave_like "file model crud"
|
7
|
+
|
8
|
+
before :all do
|
9
|
+
class ImageFile; end
|
10
|
+
|
11
|
+
class Unit
|
12
|
+
inherit Mongo::Model
|
13
|
+
collection :units
|
14
|
+
|
15
|
+
mount_file :image, ImageFile
|
16
|
+
end
|
17
|
+
end
|
18
|
+
after(:all){remove_constants :Unit}
|
19
|
+
|
20
|
+
before{@model_class = Unit}
|
21
|
+
end
|
data/spec/spec_helper.rb
CHANGED
data/spec/validation_spec.rb
CHANGED
@@ -3,35 +3,112 @@ require 'spec_helper'
|
|
3
3
|
describe "Validations" do
|
4
4
|
with_mongo_model
|
5
5
|
|
6
|
-
before do
|
7
|
-
class
|
6
|
+
before :all do
|
7
|
+
class BaseUnit
|
8
8
|
inherit Mongo::Model
|
9
9
|
collection :units
|
10
10
|
|
11
|
-
attr_accessor :errors
|
12
|
-
|
13
11
|
attr_accessor :name
|
14
12
|
end
|
15
13
|
end
|
16
14
|
after{remove_constants :Unit}
|
15
|
+
after(:all){remove_constants :BaseUnit}
|
16
|
+
|
17
|
+
describe 'basics' do
|
18
|
+
it "before validation callback" do
|
19
|
+
class Unit < BaseUnit
|
20
|
+
before_validate :check_name
|
21
|
+
def check_name
|
22
|
+
errors[:name] = 'invalid name'
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
unit = Unit.new
|
27
|
+
unit.save.should be_false
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should not save model with errors" do
|
31
|
+
unit = BaseUnit.build name: 'Zeratul'
|
32
|
+
unit.save.should be_true
|
33
|
+
|
34
|
+
unit.errors.clear
|
35
|
+
unit.save.should be_true
|
36
|
+
|
37
|
+
unit.errors[:name] = 'hairy error'
|
38
|
+
unit.save.should be_false
|
39
|
+
|
40
|
+
unit.errors.clear
|
41
|
+
unit.save.should be_true
|
42
|
+
end
|
43
|
+
|
44
|
+
# it "should check :errors only and ignore valid? method" do
|
45
|
+
# unit = BaseUnit.build name: 'Zeratul'
|
46
|
+
# unit.should_not_receive(:valid?)
|
47
|
+
# unit.save.should be_true
|
48
|
+
# end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe "validatable2" do
|
52
|
+
it "smoke test" do
|
53
|
+
class Unit < BaseUnit
|
54
|
+
validates_presence_of :name
|
55
|
+
end
|
17
56
|
|
18
|
-
|
19
|
-
|
20
|
-
|
57
|
+
unit = Unit.new
|
58
|
+
unit.should_not be_valid
|
59
|
+
unit.errors.size.should == 1
|
60
|
+
unit.errors.first.first.should == :name
|
61
|
+
unit.save.should be_false
|
21
62
|
|
22
|
-
|
23
|
-
|
63
|
+
unit.errors.clear
|
64
|
+
unit.name = 'Zeratul'
|
65
|
+
unit.should be_valid
|
66
|
+
unit.errors.should be_empty
|
67
|
+
unit.save.should be_true
|
68
|
+
end
|
24
69
|
|
25
|
-
|
26
|
-
|
70
|
+
it "should validate before save" do
|
71
|
+
unit = BaseUnit.new
|
72
|
+
unit.should_receive(:run_validations)
|
73
|
+
unit.save
|
74
|
+
end
|
27
75
|
|
28
|
-
|
29
|
-
|
76
|
+
it "should not save errors as instance variables" do
|
77
|
+
unit = BaseUnit.new
|
78
|
+
unit.valid?
|
79
|
+
unit.instance_variables.select{|iv_name| iv_name !~ /^@_/}.should be_empty
|
80
|
+
end
|
30
81
|
end
|
31
82
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
83
|
+
describe "special" do
|
84
|
+
it 'validates_uniqueness_of' do
|
85
|
+
class Unit < BaseUnit
|
86
|
+
validates_uniqueness_of :name
|
87
|
+
end
|
88
|
+
|
89
|
+
unit = Unit.build name: 'Zeratul'
|
90
|
+
unit.save.should be_true
|
91
|
+
|
92
|
+
unit = Unit.build name: 'Zeratul'
|
93
|
+
unit.save.should be_false
|
94
|
+
unit.errors[:name].first.should =~ /unique/
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'validates_uniqueness_of with scope' do
|
98
|
+
class Unit < BaseUnit
|
99
|
+
attr_accessor :account_id
|
100
|
+
|
101
|
+
validates_uniqueness_of :name, scope: :account_id
|
102
|
+
end
|
103
|
+
|
104
|
+
unit = Unit.build name: 'Zeratul', account_id: '10'
|
105
|
+
unit.save.should be_true
|
106
|
+
|
107
|
+
unit = Unit.build name: 'Zeratul', account_id: '10'
|
108
|
+
unit.save.should be_false
|
109
|
+
|
110
|
+
unit = Unit.build name: 'Zeratul', account_id: '20'
|
111
|
+
unit.save.should be_true
|
112
|
+
end
|
36
113
|
end
|
37
114
|
end
|
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.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,52 +9,8 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-08-
|
13
|
-
dependencies:
|
14
|
-
- !ruby/object:Gem::Dependency
|
15
|
-
name: i18n
|
16
|
-
requirement: &2785140 !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
|
-
requirements:
|
19
|
-
- - ! '>='
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
version: '0.5'
|
22
|
-
type: :runtime
|
23
|
-
prerelease: false
|
24
|
-
version_requirements: *2785140
|
25
|
-
- !ruby/object:Gem::Dependency
|
26
|
-
name: validatable2
|
27
|
-
requirement: &2784840 !ruby/object:Gem::Requirement
|
28
|
-
none: false
|
29
|
-
requirements:
|
30
|
-
- - ! '>='
|
31
|
-
- !ruby/object:Gem::Version
|
32
|
-
version: '0'
|
33
|
-
type: :runtime
|
34
|
-
prerelease: false
|
35
|
-
version_requirements: *2784840
|
36
|
-
- !ruby/object:Gem::Dependency
|
37
|
-
name: mongodb
|
38
|
-
requirement: &2784550 !ruby/object:Gem::Requirement
|
39
|
-
none: false
|
40
|
-
requirements:
|
41
|
-
- - ! '>='
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
version: '0'
|
44
|
-
type: :runtime
|
45
|
-
prerelease: false
|
46
|
-
version_requirements: *2784550
|
47
|
-
- !ruby/object:Gem::Dependency
|
48
|
-
name: ruby_ext
|
49
|
-
requirement: &2784260 !ruby/object:Gem::Requirement
|
50
|
-
none: false
|
51
|
-
requirements:
|
52
|
-
- - ! '>='
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '0'
|
55
|
-
type: :runtime
|
56
|
-
prerelease: false
|
57
|
-
version_requirements: *2784260
|
12
|
+
date: 2011-08-30 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
58
14
|
description:
|
59
15
|
email:
|
60
16
|
executables: []
|
@@ -63,30 +19,32 @@ extra_rdoc_files: []
|
|
63
19
|
files:
|
64
20
|
- Rakefile
|
65
21
|
- readme.md
|
66
|
-
- lib/
|
67
|
-
- lib/
|
68
|
-
- lib/
|
69
|
-
- lib/
|
70
|
-
- lib/
|
71
|
-
- lib/
|
72
|
-
- lib/
|
73
|
-
- lib/
|
74
|
-
- lib/
|
75
|
-
- lib/
|
76
|
-
- lib/
|
77
|
-
- lib/
|
78
|
-
- lib/
|
79
|
-
- lib/
|
22
|
+
- lib/mongo/model/assignment.rb
|
23
|
+
- lib/mongo/model/attribute_convertors.rb
|
24
|
+
- lib/mongo/model/callbacks.rb
|
25
|
+
- lib/mongo/model/crud.rb
|
26
|
+
- lib/mongo/model/db.rb
|
27
|
+
- lib/mongo/model/file_model.rb
|
28
|
+
- lib/mongo/model/misc.rb
|
29
|
+
- lib/mongo/model/model.rb
|
30
|
+
- lib/mongo/model/query.rb
|
31
|
+
- lib/mongo/model/scope.rb
|
32
|
+
- lib/mongo/model/spec.rb
|
33
|
+
- lib/mongo/model/support/types.rb
|
34
|
+
- lib/mongo/model/validation/uniqueness_validator.rb
|
35
|
+
- lib/mongo/model/validation.rb
|
36
|
+
- lib/mongo/model.rb
|
37
|
+
- lib/mongo_model/gems.rb
|
80
38
|
- spec/assignment_spec.rb
|
81
39
|
- spec/attribute_convertors_spec.rb
|
82
40
|
- spec/callbacks_spec.rb
|
83
41
|
- spec/crud_spec.rb
|
84
42
|
- spec/db_spec.rb
|
43
|
+
- spec/file_model_spec.rb
|
85
44
|
- spec/misc_spec.rb
|
86
45
|
- spec/query_spec.rb
|
87
46
|
- spec/scope_spec.rb
|
88
47
|
- spec/spec_helper.rb
|
89
|
-
- spec/validatable2_spec.rb
|
90
48
|
- spec/validation_spec.rb
|
91
49
|
homepage: http://github.com/alexeypetrushin/mongodb_model
|
92
50
|
licenses: []
|
data/spec/validatable2_spec.rb
DELETED
@@ -1,40 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'validatable'
|
3
|
-
|
4
|
-
describe "Integration with validatable2" do
|
5
|
-
with_mongo_model
|
6
|
-
|
7
|
-
before do
|
8
|
-
class Unit
|
9
|
-
inherit Mongo::Model
|
10
|
-
collection :units
|
11
|
-
|
12
|
-
include Validatable
|
13
|
-
|
14
|
-
attr_accessor :name, :status
|
15
|
-
|
16
|
-
validates_presence_of :name
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
after{remove_constants :Unit}
|
21
|
-
|
22
|
-
it "ActiveModel integration smoke test" do
|
23
|
-
unit = Unit.new
|
24
|
-
unit.should_not be_valid
|
25
|
-
unit.errors.size.should == 1
|
26
|
-
unit.errors.first.first.should == :name
|
27
|
-
unit.save.should be_false
|
28
|
-
|
29
|
-
unit.name = 'Zeratul'
|
30
|
-
unit.should be_valid
|
31
|
-
unit.errors.should be_empty
|
32
|
-
unit.save.should be_true
|
33
|
-
end
|
34
|
-
|
35
|
-
it "should not save errors as instance variables" do
|
36
|
-
unit = Unit.new
|
37
|
-
unit.valid?
|
38
|
-
unit.instance_variables.select{|iv_name| iv_name !~ /^@_/}.should be_empty
|
39
|
-
end
|
40
|
-
end
|