mongomodel 0.4.6 → 0.4.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. data/Appraisals +12 -0
  2. data/bin/console +1 -2
  3. data/gemfiles/mongo_mapper.gemfile +12 -0
  4. data/gemfiles/mongoid.gemfile +12 -0
  5. data/lib/mongomodel.rb +3 -0
  6. data/lib/mongomodel/compatibility/mongo_mapper.rb +23 -0
  7. data/lib/mongomodel/compatibility/mongoid.rb +17 -0
  8. data/lib/mongomodel/concerns/properties.rb +5 -0
  9. data/lib/mongomodel/concerns/validations.rb +5 -3
  10. data/lib/mongomodel/support/core_extensions.rb +4 -4
  11. data/lib/mongomodel/support/mongo_options.rb +4 -2
  12. data/lib/mongomodel/support/mongo_order.rb +4 -0
  13. data/lib/mongomodel/support/scope.rb +1 -1
  14. data/lib/mongomodel/version.rb +1 -1
  15. data/spec/mongomodel/attributes/store_spec.rb +12 -12
  16. data/spec/mongomodel/concerns/associations/belongs_to_spec.rb +13 -13
  17. data/spec/mongomodel/concerns/associations/has_many_by_foreign_key_spec.rb +25 -25
  18. data/spec/mongomodel/concerns/associations/has_many_by_ids_spec.rb +25 -25
  19. data/spec/mongomodel/concerns/attribute_methods/before_type_cast_spec.rb +5 -5
  20. data/spec/mongomodel/concerns/attribute_methods/dirty_spec.rb +21 -21
  21. data/spec/mongomodel/concerns/attribute_methods/multi_parameter_assignment_spec.rb +3 -3
  22. data/spec/mongomodel/concerns/attribute_methods/protected_spec.rb +10 -10
  23. data/spec/mongomodel/concerns/attribute_methods/query_spec.rb +6 -6
  24. data/spec/mongomodel/concerns/attribute_methods/read_spec.rb +6 -6
  25. data/spec/mongomodel/concerns/attribute_methods/write_spec.rb +5 -5
  26. data/spec/mongomodel/concerns/attribute_methods_spec.rb +5 -5
  27. data/spec/mongomodel/concerns/attributes_spec.rb +13 -13
  28. data/spec/mongomodel/concerns/callbacks_spec.rb +11 -11
  29. data/spec/mongomodel/concerns/logging_spec.rb +2 -2
  30. data/spec/mongomodel/concerns/observing_spec.rb +3 -3
  31. data/spec/mongomodel/concerns/pretty_inspect_spec.rb +5 -5
  32. data/spec/mongomodel/concerns/properties_spec.rb +6 -6
  33. data/spec/mongomodel/concerns/serialization/json_serialization_spec.rb +6 -6
  34. data/spec/mongomodel/concerns/timestamps_spec.rb +10 -10
  35. data/spec/mongomodel/concerns/translation_spec.rb +1 -1
  36. data/spec/mongomodel/concerns/validations_spec.rb +12 -4
  37. data/spec/mongomodel/document/callbacks_spec.rb +10 -10
  38. data/spec/mongomodel/document/collection_modifiers_spec.rb +1 -1
  39. data/spec/mongomodel/document/dynamic_finders_spec.rb +5 -5
  40. data/spec/mongomodel/document/finders_spec.rb +9 -9
  41. data/spec/mongomodel/document/indexes_spec.rb +19 -19
  42. data/spec/mongomodel/document/optimistic_locking_spec.rb +8 -8
  43. data/spec/mongomodel/document/persistence_spec.rb +38 -38
  44. data/spec/mongomodel/document/scopes_spec.rb +8 -8
  45. data/spec/mongomodel/document/validations/uniqueness_spec.rb +9 -9
  46. data/spec/mongomodel/document/validations_spec.rb +16 -16
  47. data/spec/mongomodel/document_spec.rb +11 -11
  48. data/spec/mongomodel/embedded_document_spec.rb +13 -13
  49. data/spec/mongomodel/mongomodel_spec.rb +4 -4
  50. data/spec/mongomodel/support/collection_spec.rb +40 -40
  51. data/spec/mongomodel/support/map_spec.rb +41 -41
  52. data/spec/mongomodel/support/mongo_operator_spec.rb +11 -9
  53. data/spec/mongomodel/support/mongo_options_spec.rb +17 -17
  54. data/spec/mongomodel/support/mongo_order_spec.rb +22 -22
  55. data/spec/mongomodel/support/property_spec.rb +21 -12
  56. data/spec/mongomodel/support/scope_spec.rb +134 -134
  57. data/spec/spec_helper.rb +1 -0
  58. data/spec/specdoc.opts +0 -3
  59. metadata +7 -5
@@ -11,19 +11,19 @@ module MongoModel
11
11
 
12
12
  describe "#query_attribute" do
13
13
  context "string attribute" do
14
- it "should return true if the attribute is not blank" do
14
+ it "returns true if the attribute is not blank" do
15
15
  subject.foo = 'set foo'
16
16
  subject.query_attribute(:foo).should be_true
17
17
  subject.foo?.should be_true
18
18
  end
19
19
 
20
- it "should return false if the attribute is nil" do
20
+ it "returns false if the attribute is nil" do
21
21
  subject.foo = nil
22
22
  subject.query_attribute(:foo).should be_false
23
23
  subject.foo?.should be_false
24
24
  end
25
25
 
26
- it "should return false if the attribute is blank" do
26
+ it "returns false if the attribute is blank" do
27
27
  subject.foo = ''
28
28
  subject.query_attribute(:foo).should be_false
29
29
  subject.foo?.should be_false
@@ -31,19 +31,19 @@ module MongoModel
31
31
  end
32
32
 
33
33
  context "boolean attribute" do
34
- it "should return true if the attribute is true" do
34
+ it "returns true if the attribute is true" do
35
35
  subject.boolean = true
36
36
  subject.query_attribute(:boolean).should be_true
37
37
  subject.boolean?.should be_true
38
38
  end
39
39
 
40
- it "should return false if the attribute is nil" do
40
+ it "returns false if the attribute is nil" do
41
41
  subject.boolean = nil
42
42
  subject.query_attribute(:boolean).should be_false
43
43
  subject.boolean?.should be_false
44
44
  end
45
45
 
46
- it "should return false if the attribute is false" do
46
+ it "returns false if the attribute is false" do
47
47
  subject.boolean = false
48
48
  subject.query_attribute(:boolean).should be_false
49
49
  subject.boolean?.should be_false
@@ -10,28 +10,28 @@ module MongoModel
10
10
 
11
11
  describe "#read_attribute" do
12
12
  context "valid property" do
13
- it "should return the attribute value" do
13
+ it "returns the attribute value" do
14
14
  subject.read_attribute(:foo).should == 'value of foo'
15
15
  end
16
16
 
17
- it "should define a reader method" do
17
+ it "defines a reader method" do
18
18
  subject.foo.should == 'value of foo'
19
19
  end
20
20
  end
21
21
 
22
22
  context "no property" do
23
- it "should return the attribute value" do
23
+ it "returns the attribute value" do
24
24
  subject.read_attribute(:bar).should == 'value of bar'
25
25
  end
26
26
 
27
- it "should not define a reader method" do
27
+ it "does not define a reader method" do
28
28
  lambda { subject.bar }.should raise_error(NoMethodError)
29
29
  end
30
30
  end
31
31
  end
32
32
 
33
33
  describe "#[]" do
34
- it "should read the given attribute" do
34
+ it "reads the given attribute" do
35
35
  subject.should_receive(:read_attribute).with(:foo).and_return('value of foo')
36
36
  subject[:foo].should == 'value of foo'
37
37
  end
@@ -44,7 +44,7 @@ module MongoModel
44
44
  subject { TestDocument.new }
45
45
 
46
46
  describe "#id" do
47
- it "should return id from attributes" do
47
+ it "returns id from attributes" do
48
48
  subject.id.should == subject.attributes[:id]
49
49
  end
50
50
  end
@@ -10,31 +10,31 @@ module MongoModel
10
10
 
11
11
  describe "#write_attribute" do
12
12
  context "with property" do
13
- it "should set the attribute hash" do
13
+ it "sets the attribute hash" do
14
14
  subject.write_attribute(:foo, 'set foo')
15
15
  subject.attributes[:foo].should == 'set foo'
16
16
  end
17
17
 
18
- it "should define a writer method" do
18
+ it "defines a writer method" do
19
19
  subject.foo = 'set foo'
20
20
  subject.attributes[:foo].should == 'set foo'
21
21
  end
22
22
  end
23
23
 
24
24
  context "no property" do
25
- it "should set the attribute hash" do
25
+ it "sets the attribute hash" do
26
26
  subject.write_attribute(:bar, 'set bar')
27
27
  subject.attributes[:bar].should == 'set bar'
28
28
  end
29
29
 
30
- it "should not define a writer method" do
30
+ it "does not define a writer method" do
31
31
  lambda { subject.bar = 'set bar' }.should raise_error(NoMethodError)
32
32
  end
33
33
  end
34
34
  end
35
35
 
36
36
  describe "#[]=" do
37
- it "should write the given attribute" do
37
+ it "writes the given attribute" do
38
38
  subject.should_receive(:write_attribute).with(:foo, 'value of foo')
39
39
  subject[:foo] = 'value of foo'
40
40
  end
@@ -11,7 +11,7 @@ module MongoModel
11
11
 
12
12
  subject { TestDocument.new }
13
13
 
14
- it "should not overwrite an existing method" do
14
+ it "does not overwrite an existing method" do
15
15
  subject.foo(1).should == "from method"
16
16
  end
17
17
 
@@ -19,7 +19,7 @@ module MongoModel
19
19
  define_class(:SubDocument, :TestDocument)
20
20
  subject { SubDocument.new }
21
21
 
22
- it "should not overwrite methods defined on the superclass" do
22
+ it "does not overwrite methods defined on the superclass" do
23
23
  subject.foo(1).should == "from method"
24
24
  end
25
25
  end
@@ -29,7 +29,7 @@ module MongoModel
29
29
  def foo(a); "from method"; end
30
30
  end
31
31
 
32
- it "should not overwrite methods defined on the included module" do
32
+ it "does not overwrite methods defined on the included module" do
33
33
  TestDocument.send(:include, TestModule)
34
34
  subject.foo(1).should == "from method"
35
35
  end
@@ -47,7 +47,7 @@ module MongoModel
47
47
  end
48
48
 
49
49
  context "the base class" do
50
- it "should not overwrite methods defined within the concern" do
50
+ it "does not overwrite methods defined within the concern" do
51
51
  TestDocument.send(:include, TestConcern)
52
52
  subject.bar(1).should == "from concern"
53
53
  end
@@ -57,7 +57,7 @@ module MongoModel
57
57
  define_class(:SubDocument, :TestDocument)
58
58
  subject { SubDocument.new }
59
59
 
60
- it "should not overwrite methods defined within the concern" do
60
+ it "does not overwrite methods defined within the concern" do
61
61
  SubDocument.send(:include, TestConcern)
62
62
  subject.bar(1).should == "from concern"
63
63
  end
@@ -19,12 +19,12 @@ module MongoModel
19
19
  specs_for(Document, EmbeddedDocument) do
20
20
  define_class(:TestDocument, described_class)
21
21
 
22
- it "should have an attributes store on the instance" do
22
+ it "has an attributes store on the instance" do
23
23
  doc = TestDocument.new
24
24
  doc.attributes.should be_an_instance_of(MongoModel::Attributes::Store)
25
25
  end
26
26
 
27
- it "should convert to mongo representation" do
27
+ it "converts to mongo representation" do
28
28
  doc = TestDocument.new
29
29
  doc.to_mongo.should == doc.attributes.to_mongo
30
30
  end
@@ -51,22 +51,22 @@ module MongoModel
51
51
  let(:reloaded) { TestDocument.find(subject.id) }
52
52
  end
53
53
 
54
- it "should read the correct value from attributes" do
54
+ it "reads the correct value from attributes" do
55
55
  subject.test_property.should == value
56
56
  end
57
57
 
58
- it "should read the correct value after reloading" do
58
+ it "reads the correct value after reloading" do
59
59
  reloaded.test_property.should == subject.test_property
60
60
  end
61
61
  end
62
62
  end
63
63
 
64
- it "should have an attributes store" do
64
+ it "has an attributes store" do
65
65
  doc = TestDocument.new
66
66
  doc.attributes.should be_an_instance_of(MongoModel::Attributes::Store)
67
67
  end
68
68
 
69
- it "should duplicate attributes when duplicating object" do
69
+ it "duplicates attributes when duplicating object" do
70
70
  original = TestDocument.new
71
71
  duplicate = original.dup
72
72
 
@@ -79,18 +79,18 @@ module MongoModel
79
79
  property :age, Integer, :default => 21
80
80
  end
81
81
 
82
- it "should be initializable with attributes hash" do
82
+ it "is initializable with attributes hash" do
83
83
  doc = Person.new(:name => 'Fred', :age => 42)
84
84
  doc.name.should == 'Fred'
85
85
  doc.age.should == 42
86
86
  end
87
87
 
88
- it "should use default attributes when initializing with partial attributes hash" do
88
+ it "uses default attributes when initializing with partial attributes hash" do
89
89
  doc = Person.new(:name => 'Maurice')
90
90
  doc.age.should == 21
91
91
  end
92
92
 
93
- it "should load from mongo representation" do
93
+ it "loads from mongo representation" do
94
94
  doc = Person.from_mongo({ 'name' => 'James', 'age' => 15 })
95
95
  doc.name.should == 'James'
96
96
  doc.age.should == 15
@@ -108,12 +108,12 @@ module MongoModel
108
108
 
109
109
  subject { TestDocument.new }
110
110
 
111
- it "should call custom property methods" do
111
+ it "calls custom property methods" do
112
112
  subject.attributes = { :test_property => 'property value' }
113
113
  subject.test_property.should == 'set from method'
114
114
  end
115
115
 
116
- it "should use write_attribute if no such property" do
116
+ it "uses write_attribute if no such property" do
117
117
  subject.attributes = { :non_property => 'property value' }
118
118
  subject.read_attribute(:non_property).should == 'property value'
119
119
  end
@@ -122,7 +122,7 @@ module MongoModel
122
122
  describe "#new" do
123
123
  define_class(:TestDocument, described_class)
124
124
 
125
- it "should yield the instance to a block if provided" do
125
+ it "yields the instance to a block if provided" do
126
126
  block_called = false
127
127
 
128
128
  TestDocument.new do |doc|
@@ -145,7 +145,7 @@ module MongoModel
145
145
 
146
146
  it { should be_frozen }
147
147
 
148
- it "should not allow changes to the attributes hash" do
148
+ it "does not allow changes to the attributes hash" do
149
149
  lambda { subject.attributes[:test_property] = 'Change' }.should raise_error
150
150
  end
151
151
  end
@@ -12,7 +12,7 @@ module MongoModel
12
12
  property :things, Collection[ChildThingDocument]
13
13
  end
14
14
 
15
- it "should call callbacks on all embedded documents when adding a new one" do
15
+ it "calls callbacks on all embedded documents when adding a new one" do
16
16
  parent = ParentDocument.create!
17
17
  parent.things = Collection[ChildThingDocument].new
18
18
  parent.things << ChildThingDocument.new(:name => "Thing One")
@@ -35,46 +35,46 @@ module MongoModel
35
35
  let(:child) { ChildDocument.new }
36
36
  let(:parent) { ParentDocument.create!(:child => child) }
37
37
 
38
- it "should run each type of callback when initializing" do
38
+ it "runs each type of callback when initializing" do
39
39
  instance = ChildDocument.new
40
40
  instance.should run_callbacks(:after_initialize)
41
41
  end
42
42
 
43
- it "should run each type of callback on find" do
43
+ it "runs each type of callback on find" do
44
44
  instance = ParentDocument.find(parent.id).child
45
45
  instance.should run_callbacks(:after_initialize, :after_find)
46
46
  end
47
47
 
48
- it "should run each type of callback when validating a new document" do
48
+ it "runs each type of callback when validating a new document" do
49
49
  instance = ChildDocument.new
50
50
  instance.valid?
51
51
  instance.should run_callbacks(:after_initialize, :before_validation, :after_validation)
52
52
  end
53
53
 
54
- it "should run each type of callback when validating an existing document" do
54
+ it "runs each type of callback when validating an existing document" do
55
55
  instance = ParentDocument.find(parent.id).child
56
56
  instance.valid?
57
57
  instance.should run_callbacks(:after_initialize, :after_find, :before_validation, :after_validation)
58
58
  end
59
59
 
60
- it "should run each type of callback when creating a document" do
60
+ it "runs each type of callback when creating a document" do
61
61
  instance = ParentDocument.create!(:child => ChildDocument.new)
62
62
  instance.child.should run_callbacks(:after_initialize, :before_validation, :after_validation, :before_save, :before_create, :after_create, :after_save)
63
63
  end
64
64
 
65
- it "should run each type of callback when saving an existing document" do
65
+ it "runs each type of callback when saving an existing document" do
66
66
  instance = ParentDocument.find(parent.id)
67
67
  instance.save
68
68
  instance.child.should run_callbacks(:after_initialize, :after_find, :before_validation, :after_validation, :before_save, :before_update, :after_update, :after_save)
69
69
  end
70
70
 
71
- it "should run each type of callback when destroying a document" do
71
+ it "runs each type of callback when destroying a document" do
72
72
  instance = ParentDocument.find(parent.id)
73
73
  instance.destroy
74
74
  instance.child.should run_callbacks(:after_initialize, :after_find, :before_destroy, :after_destroy)
75
75
  end
76
76
 
77
- it "should not run destroy callbacks when deleting a document" do
77
+ it "does not run destroy callbacks when deleting a document" do
78
78
  instance = ParentDocument.find(parent.id)
79
79
  instance.delete
80
80
  instance.child.should run_callbacks(:after_initialize, :after_find)
@@ -95,13 +95,13 @@ module MongoModel
95
95
  let(:parent) { ParentDocument.new(:child => child) }
96
96
 
97
97
  describe "#save" do
98
- it "should return false" do
98
+ it "returns false" do
99
99
  parent.save.should be_false
100
100
  end
101
101
  end
102
102
 
103
103
  describe "#save!" do
104
- it "should raise a MongoModel::DocumentNotSaved exception" do
104
+ it "raises a MongoModel::DocumentNotSaved exception" do
105
105
  lambda { parent.save! }.should raise_error(MongoModel::DocumentNotSaved)
106
106
  end
107
107
  end
@@ -8,11 +8,11 @@ module MongoModel
8
8
  let(:logger) { mock('logger').as_null_object }
9
9
  before(:each) { MongoModel.logger = logger }
10
10
 
11
- it "should have a logger reader on the class" do
11
+ it "has a logger reader on the class" do
12
12
  TestDocument.logger.should == logger
13
13
  end
14
14
 
15
- it "should have a logger reader on the instance" do
15
+ it "has a logger reader on the instance" do
16
16
  TestDocument.new.logger.should == logger
17
17
  end
18
18
  end
@@ -16,11 +16,11 @@ module MongoModel
16
16
 
17
17
  subject { TestDocument.new }
18
18
 
19
- it "should have an #instance method to access the observer singleton" do
19
+ it "has an #instance method to access the observer singleton" do
20
20
  TestObserver.instance.should eq(TestObserver.instance)
21
21
  end
22
22
 
23
- it "should invoke the TestObserver singleton's after_save method after saving" do
23
+ it "invokes the TestObserver singleton's after_save method after saving" do
24
24
  callback = stub
25
25
  callback.should_receive(:call).with(subject)
26
26
 
@@ -29,4 +29,4 @@ module MongoModel
29
29
  end
30
30
  end
31
31
  end
32
- end
32
+ end
@@ -4,7 +4,7 @@ module MongoModel
4
4
  specs_for(Document, EmbeddedDocument) do
5
5
  describe "#inspect" do
6
6
  context "on base class" do
7
- it "should return class name" do
7
+ it "returns class name" do
8
8
  described_class.inspect.should == described_class.name
9
9
  end
10
10
  end
@@ -13,7 +13,7 @@ module MongoModel
13
13
  context "without properties" do
14
14
  define_class(:TestDocument, described_class)
15
15
 
16
- it "should return class name" do
16
+ it "returns class name" do
17
17
  TestDocument.inspect.should == 'TestDocument()'
18
18
  end
19
19
  end
@@ -24,7 +24,7 @@ module MongoModel
24
24
  property :age, Integer
25
25
  end
26
26
 
27
- it "should return class name and property definitions" do
27
+ it "returns class name and property definitions" do
28
28
  TestDocument.inspect.should == 'TestDocument(name: String, age: Integer)'
29
29
  end
30
30
  end
@@ -42,7 +42,7 @@ module MongoModel
42
42
 
43
43
  subject { TestDocument.new(:id => 'abc-123', :name => 'Doc name', :age => 54) }
44
44
 
45
- it "should return class name and property values" do
45
+ it "returns class name and property values" do
46
46
  subject.inspect.should == '#<TestDocument id: abc-123, name: "Doc name", age: 54>'
47
47
  end
48
48
  end
@@ -59,7 +59,7 @@ module MongoModel
59
59
 
60
60
  subject { TestDocument.new(:name => 'Doc name', :age => 54) }
61
61
 
62
- it "should return class name and property values" do
62
+ it "returns class name and property values" do
63
63
  subject.inspect.should == '#<TestDocument name: "Doc name", age: 54>'
64
64
  end
65
65
  end
@@ -11,14 +11,14 @@ module MongoModel
11
11
  property :skill, String
12
12
  end
13
13
 
14
- it "should have a populated hash of properties" do
14
+ it "has a populated hash of properties" do
15
15
  Person.properties.should include({
16
16
  :name => Properties::Property.new(:name, String),
17
17
  :age => Properties::Property.new(:age, Integer, :default => 21)
18
18
  })
19
19
  end
20
20
 
21
- it "should extend properties in subclasses" do
21
+ it "extends properties in subclasses" do
22
22
  SkilledPerson.properties.should include({
23
23
  :name => Properties::Property.new(:name, String),
24
24
  :age => Properties::Property.new(:age, Integer, :default => 21),
@@ -26,7 +26,7 @@ module MongoModel
26
26
  })
27
27
  end
28
28
 
29
- it "should have a set of internal property names" do
29
+ it "has a set of internal property names" do
30
30
  Person.internal_properties.should include(Person.properties[:type])
31
31
  Person.internal_properties.should include(Person.properties[:id]) if described_class == Document
32
32
  end
@@ -40,14 +40,14 @@ module MongoModel
40
40
  let(:with_manager) { Factory.create!(:manager => person) }
41
41
  let(:without_manager) { Factory.create! }
42
42
 
43
- it "should load correctly when property is set" do
43
+ it "loads correctly when property is set" do
44
44
  factory = Factory.find(with_manager.id)
45
45
  factory.manager.should be_an_instance_of(SkilledPerson)
46
46
  factory.manager.name.should == "Joe"
47
47
  factory.manager.skill.should == "Management"
48
48
  end
49
49
 
50
- it "should load correctly when property is nil" do
50
+ it "loads correctly when property is nil" do
51
51
  factory = Factory.find(without_manager.id)
52
52
  factory.manager.should be_nil
53
53
  end
@@ -60,7 +60,7 @@ module MongoModel
60
60
 
61
61
  subject { ParentClass.new }
62
62
 
63
- it "should include methods from the module" do
63
+ it "includes methods from the module" do
64
64
  subject.should respond_to(:custom_accessor)
65
65
  subject.custom_accessor.should == "Custom accessor method"
66
66
  end