mongodb_model 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -35,7 +35,7 @@ module Mongo::Model::Assignment
35
35
  end
36
36
 
37
37
  def initialize attributes = nil
38
- super
38
+ super()
39
39
  set attributes if attributes
40
40
  end
41
41
 
@@ -4,11 +4,11 @@ require 'yaml'
4
4
  module Mongo::Model::AttributeConvertors
5
5
  CONVERTORS = {
6
6
  line: {
7
- from_string: -> s {(s || "").split(',').collect{|s| s.strip}},
7
+ from_string: -> s {(s || "").split(',').collect{|s| s.strip}.sort},
8
8
  to_string: -> v {v.join(', ')}
9
9
  },
10
10
  column: {
11
- from_string: -> s {(s || "").split("\n").collect{|s| s.strip}},
11
+ from_string: -> s {(s || "").split("\n").collect{|s| s.strip}.sort},
12
12
  to_string: -> v {v.join("\n")}
13
13
  },
14
14
  yaml: {
@@ -12,7 +12,9 @@ module Mongo::Model::Callbacks
12
12
  args.each{|executor| set_callback method_name, :before, executor, opt}
13
13
  end
14
14
  end
15
+ end
15
16
 
17
+ [:validate, :create, :update, :save, :destroy, :build].each do |method_name|
16
18
  define_method "after_#{method_name}" do |*args, &block|
17
19
  opt = args.extract_options!
18
20
  if block
@@ -1,3 +1,4 @@
1
+ require 'file_model'
1
2
  module Mongo::Model::FileModel
2
3
  inherit ::FileModel::Helper
3
4
 
@@ -7,8 +7,10 @@ module Mongo::Model
7
7
  def new_record?; !_id end
8
8
 
9
9
  inherited do
10
- alias_method :eql?, :model_eql?
11
- alias_method :==, :model_eq?
10
+ unless is?(Array) or is?(Hash)
11
+ alias_method :eql?, :model_eql?
12
+ alias_method :==, :model_eq?
13
+ end
12
14
  end
13
15
 
14
16
  def model_eql? o
@@ -3,7 +3,7 @@ module Mongo::Model::Validation
3
3
  @_errors ||= Validatable::Errors.new
4
4
  end
5
5
 
6
- def run_validations
6
+ def run_model_validations
7
7
  self.class.validations.each do |v|
8
8
  if v.respond_to?(:validate)
9
9
  v.validate self
data/lib/mongo/model.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  require 'mongodb_model/gems'
2
2
 
3
3
  require 'validatable'
4
- require 'file_model'
5
4
  require 'ruby_ext'
6
5
  require 'mongo/object'
7
6
 
@@ -20,14 +19,15 @@ module Mongo::Model; end
20
19
  query
21
20
  query_mixin
22
21
  scope
23
- attribute_convertors
24
- file_model
22
+ attribute_convertorshttp://alexeypetrushin.github.com/mongodb_model
25
23
  misc
26
24
  model
27
25
  ).each{|f| require "mongo/model/#{f}"}
28
26
 
29
27
  module Mongo
30
28
  module Model
29
+ autoload :FileModel, 'mongo/model/file_model'
30
+ http://alexeypetrushin.github.com/mongodb_model
31
31
  inherit \
32
32
  Db,
33
33
  Conversion,
@@ -38,7 +38,6 @@ module Mongo
38
38
  QueryMixin,
39
39
  Scope,
40
40
  AttributeConvertors,
41
- Mongo::Model::FileModel,
42
41
  Misc
43
42
  end
44
43
  end
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  describe 'Model callbacks' do
4
4
  with_mongo_model
5
5
 
6
- after{remove_constants :TheModel, :Player}
6
+ after{remove_constants :Post, :Player}
7
7
 
8
8
  it "integration smoke test" do
9
9
  class Player
@@ -33,4 +33,28 @@ describe 'Model callbacks' do
33
33
 
34
34
  db.units.save(player).should be_true
35
35
  end
36
+
37
+ it "should have :build callback" do
38
+ class Post
39
+ inherit Mongo::Model
40
+ collection :posts
41
+
42
+ class Tags < Array
43
+ end
44
+
45
+ def tags
46
+ @tags ||= Tags.new
47
+ end
48
+ attr_writer :tags
49
+
50
+ after_build do |post|
51
+ post.tags = Tags.new.replace post.tags
52
+ end
53
+ end
54
+
55
+ post = Post.new
56
+ post.save!
57
+
58
+ Post.first.tags.class.should == Post::Tags
59
+ end
36
60
  end
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  describe 'Model equality' do
4
4
  with_mongo_model
5
5
 
6
- after{remove_constants :Player}
6
+ after{remove_constants :Player, :Tags}
7
7
 
8
8
  it "integration smoke test" do
9
9
  class Player
@@ -29,4 +29,13 @@ describe 'Model equality' do
29
29
  player1.mission.name = 'Into the Flames'
30
30
  player1.should_not == player2
31
31
  end
32
+
33
+ it "should correct compare Array/Hash models (from error)" do
34
+ class Tags < Array
35
+ inherit Mongo::Model
36
+ end
37
+
38
+ tags = Tags.new.replace ['a', 'b']
39
+ tags.should_not == Tags.new
40
+ end
32
41
  end
@@ -10,6 +10,7 @@ describe 'File Model' do
10
10
 
11
11
  class Unit
12
12
  inherit Mongo::Model
13
+ inherit Mongo::Model::FileModel
13
14
  collection :units
14
15
 
15
16
  attr_accessor :name
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.2.2
4
+ version: 0.2.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-09-17 00:00:00.000000000Z
12
+ date: 2011-09-19 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongodb
16
- requirement: &2840810 !ruby/object:Gem::Requirement
16
+ requirement: &2781440 !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: *2840810
24
+ version_requirements: *2781440
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: file_model
27
- requirement: &2840570 !ruby/object:Gem::Requirement
27
+ requirement: &2781200 !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: *2840570
35
+ version_requirements: *2781200
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: validatable2
38
- requirement: &2840330 !ruby/object:Gem::Requirement
38
+ requirement: &2780960 !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: *2840330
46
+ version_requirements: *2780960
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: ruby_ext
49
- requirement: &2840090 !ruby/object:Gem::Requirement
49
+ requirement: &2780720 !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: *2840090
57
+ version_requirements: *2780720
58
58
  description:
59
59
  email:
60
60
  executables: []