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.
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
@@ -23,8 +23,12 @@ module Mongo::Model::Misc
23
23
  (_id || '').to_s
24
24
  end
25
25
 
26
+ delegate :t, to: I18n
27
+
26
28
 
27
29
  module ClassMethods
30
+ delegate :t, to: I18n
31
+
28
32
  def timestamps!
29
33
  attr_accessor :created_at, :updated_at
30
34
  before_save :update_timestamps
@@ -1,4 +1,6 @@
1
1
  module Mongo::Model
2
+ include Mongo::Object
3
+
2
4
  attr_accessor :_id, :_class
3
5
 
4
6
  def _id?; !!_id end
File without changes
File without changes
@@ -1,4 +1,4 @@
1
- require 'mongodb/object/spec'
1
+ require 'mongo/object/spec'
2
2
 
3
3
  rspec do
4
4
  class << self
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 'mongodb_model/gems'
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 'mongodb/object'
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 "mongodb_model/#{f}"}
26
+ ).each{|f| require "mongo/model/#{f}"}
24
27
 
25
28
  module Mongo
26
29
  module Model
27
- inherit Db, Assignment, Callbacks, Validation, Crud, Query, Scope, AttributeConvertors, Misc
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
 
@@ -1,7 +1,8 @@
1
- gem 'i18n', '>= 0.5'
1
+ gem 'i18n', '~> 0.5'
2
2
 
3
3
  if respond_to? :fake_gem
4
- fake_gem 'validatable2'
5
4
  fake_gem 'mongodb'
5
+ fake_gem 'file_model'
6
+ fake_gem 'validatable2'
6
7
  fake_gem 'ruby_ext'
7
8
  end
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 'mongodb/model'
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'
@@ -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
@@ -1,5 +1,5 @@
1
1
  require 'spec_helper'
2
- require 'mongodb/object/spec/crud_shared'
2
+ require 'mongo/object/spec/crud_shared'
3
3
 
4
4
  describe "Model CRUD" do
5
5
  with_mongo_model
@@ -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
@@ -1,7 +1,7 @@
1
- require 'mongodb_model'
1
+ require 'mongo/model'
2
2
 
3
3
  require 'rspec_ext'
4
- require 'mongodb_model/spec'
4
+ require 'mongo/model/spec'
5
5
 
6
6
  #
7
7
  # Handy spec helpers
@@ -3,35 +3,112 @@ require 'spec_helper'
3
3
  describe "Validations" do
4
4
  with_mongo_model
5
5
 
6
- before do
7
- class Unit
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
- it "should not save model with errors" do
19
- unit = Unit.build name: 'Zeratul'
20
- unit.save.should be_true
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
- unit.errors = []
23
- unit.save.should be_true
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
- unit.errors = ['hairy error']
26
- unit.save.should be_false
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
- unit.errors = {name: 'hairy error'}
29
- unit.save.should be_false
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
- it "should check :errors only and ignore valid? method" do
33
- unit = Unit.build name: 'Zeratul'
34
- unit.should_not_receive(:valid?)
35
- unit.save.should be_true
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.2
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-28 00:00:00.000000000Z
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/mongodb_model/assignment.rb
67
- - lib/mongodb_model/attribute_convertors.rb
68
- - lib/mongodb_model/callbacks.rb
69
- - lib/mongodb_model/crud.rb
70
- - lib/mongodb_model/db.rb
71
- - lib/mongodb_model/gems.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
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: []
@@ -1,5 +0,0 @@
1
- module Mongo::Model::Validation
2
- def _valid?
3
- !(respond_to?(:errors) and errors and !errors.empty?)
4
- end
5
- end
@@ -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