mongomodel 0.5.2 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b6303f96496ed1cb9c8c52316e02c07a7354f06e
4
- data.tar.gz: 5ef0ee286e9a51dd9bb42390da710688eadde951
3
+ metadata.gz: 730ec72969141979066d7a38f450b9622485bb55
4
+ data.tar.gz: b72b388e3b6b6d041dd4f6431233d25c0aeddd8c
5
5
  SHA512:
6
- metadata.gz: d11971bf1bdef7946e7e54ba0c9912566e30008d64df721d1aff9aed17dc833d436a3156d337553d33e78711da0b8048bd3004c337bc0317efb05fdf5ba64326
7
- data.tar.gz: a158854ae696e4a0bf4102835e6a6def055bce660f1141716f190e5ab576a79c2ca4793c42dc21d046d9b4d21df0c1e1d4dbc743dc47f4173692dd79ec00ff49
6
+ metadata.gz: b954cd2ce4ed17b3dc9406572d8523092db0d249dfe9c1c61418b96b15702a91df7e740e4ed9af3581d41e5f37c8497447fceb794f7c01f661aa935007999f39
7
+ data.tar.gz: 0a92c9c4215cea8d312d9ecd13684c8bf9d54cfada4410e6587902506edc6093314672a39e448ce07659bf1991b82eb24e77879227f15eebce938e8f6f0dbc3c
@@ -21,7 +21,9 @@ module MongoModel
21
21
  end
22
22
 
23
23
  def ensure_class(value)
24
- raise AssociationTypeMismatch, "expected instance of #{klass} but got #{value.class}" unless value.is_a?(klass)
24
+ unless value.is_a?(klass) || value.class.name.constantize <= klass.name.constantize
25
+ raise AssociationTypeMismatch, "#{klass} expected, got #{value.class}"
26
+ end
25
27
  end
26
28
 
27
29
  protected
@@ -32,13 +32,13 @@ module MongoModel
32
32
  end
33
33
 
34
34
  scopes[name] = lambda do |*args|
35
- s = scope.is_a?(Proc) ? scope.call(*args) : scope
36
- scoped.merge(s)
35
+ scope.is_a?(Proc) ? scope.call(*args) : scope
37
36
  end
38
37
 
39
38
  singleton_class.class_eval do
40
39
  define_method(name) do |*args|
41
- scopes[name].call(*args)
40
+ s = scopes[name].call(*args)
41
+ scoped.merge(s)
42
42
  end
43
43
  end
44
44
  end
@@ -1,3 +1,3 @@
1
1
  module MongoModel
2
- VERSION = "0.5.2"
2
+ VERSION = "0.5.3"
3
3
  end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ module MongoModel
4
+ module Associations
5
+ module Base
6
+ describe Association do
7
+ define_class(:Chapter, MongoModel::Document)
8
+ define_class(:IllustratedChapter, :Chapter)
9
+ define_class(:NonChapter, MongoModel::Document)
10
+
11
+ let(:klass) { Chapter }
12
+ let(:definition) { double(:klass => klass) }
13
+ let(:instance) { double }
14
+
15
+ subject! { Association.new(definition, instance) }
16
+
17
+ describe "#ensure_class" do
18
+ it "accepts instances of the definition class" do
19
+ subject.ensure_class(Chapter.new)
20
+ end
21
+
22
+ it "accepts instances of a subclass of the definition class" do
23
+ subject.ensure_class(IllustratedChapter.new)
24
+ end
25
+
26
+ it "raises an error if passed an object that is not an instance of the definition class" do
27
+ lambda {
28
+ subject.ensure_class(NonChapter.new)
29
+ }.should raise_error(MongoModel::AssociationTypeMismatch)
30
+ end
31
+
32
+ it "accepts instances of the definition class that have been reloaded" do
33
+ # Ensure Chapter class name is cached on the original class
34
+ Chapter.name
35
+
36
+ Object.send(:remove_const, :Chapter)
37
+ Object.const_set(:Chapter, Class.new(MongoModel::Document))
38
+
39
+ subject.ensure_class(Chapter.new)
40
+ end
41
+
42
+ it "accepts stale instances of a class that has been reloaded" do
43
+ chapter = Chapter.new
44
+
45
+ Object.send(:remove_const, :Chapter)
46
+ Object.const_set(:Chapter, Class.new(MongoModel::Document))
47
+
48
+ definition = double(:klass => Chapter)
49
+ association = Association.new(definition, instance)
50
+
51
+ association.ensure_class(chapter)
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -87,7 +87,7 @@ module MongoModel
87
87
  let(:non_user) { NonUser.create! }
88
88
 
89
89
  it "raises a AssociationTypeMismatch exception" do
90
- lambda { subject.user = non_user }.should raise_error(AssociationTypeMismatch, "expected instance of User but got NonUser")
90
+ lambda { subject.user = non_user }.should raise_error(AssociationTypeMismatch, "User expected, got NonUser")
91
91
  end
92
92
  end
93
93
 
@@ -123,7 +123,7 @@ module MongoModel
123
123
  describe "adding a non-chapter" do
124
124
  def self.should_raise(message, &block)
125
125
  it "raises an AsssociationTypeMismatch error when #{message}" do
126
- lambda { instance_eval(&block) }.should raise_error(AssociationTypeMismatch, "expected instance of Chapter but got NonChapter")
126
+ lambda { instance_eval(&block) }.should raise_error(AssociationTypeMismatch, "Chapter expected, got NonChapter")
127
127
  end
128
128
  end
129
129
 
@@ -51,37 +51,40 @@ module MongoModel
51
51
  end
52
52
 
53
53
  describe "#scope" do
54
- it "creates a method returning the scope" do
55
- Post.class_eval do
56
- scope :published, where(:published => true)
57
- end
54
+ define_class(:Post, Document) do
55
+ scope :published, where(:published => true)
58
56
 
57
+ scope :latest, lambda { |num| order(:created_at.desc).limit(num) }
58
+
59
+ scope :recent, order(:created_at.desc).limit(5)
60
+ scope :recently_published, recent.where(:published => true)
61
+ end
62
+
63
+ define_class(:SpecialPost, :Post)
64
+
65
+ it "creates a method returning the scope" do
59
66
  scope = Post.published
60
67
  scope.should be_an_instance_of(MongoModel::Scope)
61
68
  scope.where_values.should == [{:published => true}]
62
69
  end
63
70
 
64
71
  it "creates parameterized method returning the scope when given a lambda" do
65
- Post.class_eval do
66
- scope :latest, lambda { |num| order(:created_at.desc).limit(num) }
67
- end
68
-
69
72
  scope = Post.latest(4)
70
73
  scope.order_values.should == [:created_at.desc]
71
74
  scope.limit_value.should == 4
72
75
  end
73
76
 
74
77
  it "allows existing scopes to be built upon" do
75
- Post.class_eval do
76
- scope :recent, order(:created_at.desc).limit(5)
77
- scope :recently_published, recent.where(:published => true)
78
- end
79
-
80
78
  scope = Post.recently_published
81
79
  scope.where_values.should == [{:published => true}]
82
80
  scope.order_values.should == [:created_at.desc]
83
81
  scope.limit_value.should == 5
84
82
  end
83
+
84
+ it "merges the scope with the current scope of the class it is called upon" do
85
+ scope = SpecialPost.published
86
+ scope.klass.should == SpecialPost
87
+ end
85
88
  end
86
89
 
87
90
  describe "named scopes" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongomodel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Pohlenz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-25 00:00:00.000000000 Z
11
+ date: 2014-04-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -221,6 +221,7 @@ files:
221
221
  - mongomodel.gemspec
222
222
  - spec/mongomodel/attributes/store_spec.rb
223
223
  - spec/mongomodel/concerns/activemodel_spec.rb
224
+ - spec/mongomodel/concerns/associations/base/association_spec.rb
224
225
  - spec/mongomodel/concerns/associations/belongs_to_spec.rb
225
226
  - spec/mongomodel/concerns/associations/has_many_by_foreign_key_spec.rb
226
227
  - spec/mongomodel/concerns/associations/has_many_by_ids_spec.rb