mongo_odm 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. data/Gemfile +11 -0
  2. data/LICENSE +20 -0
  3. data/README.rdoc +251 -0
  4. data/Rakefile +70 -0
  5. data/lib/mongo_odm.rb +48 -0
  6. data/lib/mongo_odm/collection.rb +42 -0
  7. data/lib/mongo_odm/core_ext/conversions.rb +262 -0
  8. data/lib/mongo_odm/cursor.rb +11 -0
  9. data/lib/mongo_odm/document.rb +34 -0
  10. data/lib/mongo_odm/document/associations.rb +39 -0
  11. data/lib/mongo_odm/document/associations/has_many.rb +19 -0
  12. data/lib/mongo_odm/document/associations/has_one.rb +19 -0
  13. data/lib/mongo_odm/document/attribute_methods.rb +103 -0
  14. data/lib/mongo_odm/document/attribute_methods/dirty.rb +51 -0
  15. data/lib/mongo_odm/document/attribute_methods/localization.rb +67 -0
  16. data/lib/mongo_odm/document/attribute_methods/query.rb +35 -0
  17. data/lib/mongo_odm/document/attribute_methods/read.rb +39 -0
  18. data/lib/mongo_odm/document/attribute_methods/write.rb +37 -0
  19. data/lib/mongo_odm/document/callbacks.rb +29 -0
  20. data/lib/mongo_odm/document/fields.rb +66 -0
  21. data/lib/mongo_odm/document/inspect.rb +28 -0
  22. data/lib/mongo_odm/document/persistence.rb +96 -0
  23. data/lib/mongo_odm/document/validations.rb +46 -0
  24. data/lib/mongo_odm/errors.rb +18 -0
  25. data/lib/mongo_odm/version.rb +7 -0
  26. data/spec/models/00-blank_slate.rb +4 -0
  27. data/spec/models/01-shape.rb +8 -0
  28. data/spec/models/02-circle.rb +7 -0
  29. data/spec/models/03-big_red_circle.rb +8 -0
  30. data/spec/mongo_odm/core_ext/conversions_spec.rb +423 -0
  31. data/spec/mongo_odm/document/fields_spec.rb +187 -0
  32. data/spec/mongo_odm/document/inspect_spec.rb +19 -0
  33. data/spec/mongo_odm/document_spec.rb +12 -0
  34. data/spec/mongo_odm/mongo_odm_spec.rb +84 -0
  35. data/spec/spec.opts +4 -0
  36. data/spec/spec_helper.rb +12 -0
  37. metadata +185 -0
@@ -0,0 +1,187 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ describe MongoODM::Document::Fields do
5
+
6
+ describe ".field" do
7
+
8
+ context "with a name and no type" do
9
+
10
+ before do
11
+ BlankSlate.field(:testing_no_type)
12
+ end
13
+
14
+ after do
15
+ BlankSlate.instance_variable_set(:@fields, nil)
16
+ end
17
+
18
+ it "adds a String field to the class" do
19
+ BlankSlate.fields[:testing_no_type].should be_kind_of(MongoODM::Document::Fields::Field)
20
+ BlankSlate.fields[:testing_no_type].type.should == String
21
+ end
22
+
23
+ end
24
+
25
+ context "with a name and a type" do
26
+
27
+ before do
28
+ BlankSlate.field(:testing_float, Float)
29
+ end
30
+
31
+ it "adds the typed field to the class" do
32
+ BlankSlate.fields[:testing_float].should be_kind_of(MongoODM::Document::Fields::Field)
33
+ BlankSlate.fields[:testing_float].type.should == Float
34
+ end
35
+
36
+ end
37
+
38
+ end
39
+
40
+ describe ".fields" do
41
+
42
+ context "on classes without defined fields" do
43
+
44
+ before do
45
+ @klass = BlankSlate
46
+ end
47
+
48
+ it "returns an empty hash" do
49
+ @klass.fields.should == {}
50
+ end
51
+
52
+ end
53
+
54
+ context "on parent classes" do
55
+
56
+ before do
57
+ @klass = Shape
58
+ end
59
+
60
+ it "returns a hash with the defined fields" do
61
+ expected_fields = [:x, :y, :color]
62
+ expected_fields.each do |field_name|
63
+ @klass.fields[field_name].should be_kind_of(MongoODM::Document::Fields::Field)
64
+ end
65
+ end
66
+
67
+ it "allows indifferent access to the hash" do
68
+ @klass.fields[:x].should == @klass.fields["x"]
69
+ end
70
+
71
+ end
72
+
73
+ context "on subclasses" do
74
+
75
+ before do
76
+ @klass = BigRedCircle
77
+ end
78
+
79
+ it "returns all the fields defined on the class and its subclasses" do
80
+ expected_fields = [:x, :y, :radius, :color]
81
+ expected_fields.each do |field_name|
82
+ @klass.fields[field_name].should be_kind_of(MongoODM::Document::Fields::Field)
83
+ end
84
+ end
85
+
86
+ it "overrides field descriptions on any parent class" do
87
+ @klass.fields[:color].default.should == "red"
88
+ @klass.fields[:radius].default.should == 20.0
89
+ end
90
+
91
+ end
92
+
93
+ end
94
+
95
+ end
96
+
97
+ describe MongoODM::Document::Fields::Field do
98
+
99
+ describe ".new" do
100
+
101
+ context "with a name :testing_float and a type class Float" do
102
+
103
+ before do
104
+ @field = MongoODM::Document::Fields::Field.new(:testing_float, Float)
105
+ end
106
+
107
+ it "creates a new Field instance with name :testing_float and type Float" do
108
+ @field.name.should == :testing_float
109
+ @field.type.should == Float
110
+ end
111
+
112
+ end
113
+
114
+ context "with a name :testing_float, a type class Float and a default option 1.0" do
115
+
116
+ before do
117
+ @field = MongoODM::Document::Fields::Field.new(:testing_float, Float, :default => 1.0)
118
+ end
119
+
120
+ it "creates a new Field instance with name :testing_float, type Float and default 1.0" do
121
+ @field.name.should == :testing_float
122
+ @field.type.should == Float
123
+ @field.default.should == 1.0
124
+ end
125
+
126
+ end
127
+
128
+ end
129
+
130
+ describe "#default" do
131
+
132
+ context "when the field described a default value" do
133
+
134
+ before do
135
+ @field = MongoODM::Document::Fields::Field.new(:color, String, :default => "red")
136
+ end
137
+
138
+ it "returns the value" do
139
+ @field.default.should == "red"
140
+ end
141
+
142
+ end
143
+
144
+ context "when the field described a block that returns the default value" do
145
+
146
+ before do
147
+ @field = MongoODM::Document::Fields::Field.new(:color, String, :default => lambda { "b" + "lue"} )
148
+ end
149
+
150
+ it "calls the block and returns the result" do
151
+ @field.default.should == "blue"
152
+ end
153
+
154
+ end
155
+
156
+ end
157
+
158
+ describe "#type_cast" do
159
+
160
+ context "when the field's `type' class responds to type_cast" do
161
+
162
+ before do
163
+ @field = MongoODM::Document::Fields::Field.new(:number, Integer)
164
+ end
165
+
166
+ it "returns the casted value" do
167
+ @field.type_cast("32.3").should == 32
168
+ end
169
+
170
+ end
171
+
172
+ context "when the field's `type' class do not respond to type_cast" do
173
+
174
+ before do
175
+ class InvalidClass; end
176
+ @field = MongoODM::Document::Fields::Field.new(:invalid, InvalidClass)
177
+ end
178
+
179
+ it "returns the raw value" do
180
+ lambda { @field.type_cast("whatever") }.should raise_error(MongoODM::Errors::TypeCastMissing)
181
+ end
182
+
183
+ end
184
+
185
+ end
186
+
187
+ end
@@ -0,0 +1,19 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ describe MongoODM::Document::Inspect do
5
+
6
+ describe ".inspect" do
7
+ it "returns a string with the name of the class that includes the MongoODM::Document module and a list of its properties with types" do
8
+ Shape.inspect.should == "Shape(x: Float, y: Float, color: String)"
9
+ end
10
+ end
11
+
12
+ describe "#inspect" do
13
+ it "returns a string with the name of the class that includes the MongoODM::Document module and a list of its attributes with values" do
14
+ circle = Circle.new(:x => 10, :y => 12.5, :radius => 1.2)
15
+ circle.inspect.should == "#<Circle x: 10.0, y: 12.5, color: nil, radius: 1.2>"
16
+ end
17
+ end
18
+
19
+ end
@@ -0,0 +1,12 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ describe MongoODM::Document do
5
+ # TODO: See ActiveModel::Lint::Tests for a minimal set of tests
6
+ # test_valid?
7
+ # test_new_record?
8
+ # test_destroyed?
9
+ # test_model_naming
10
+ # test_errors_aref
11
+ # test_errors_full_messages
12
+ end
@@ -0,0 +1,84 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ describe MongoODM do
5
+
6
+ describe "included modules" do
7
+
8
+ end
9
+
10
+ describe "#connection" do
11
+
12
+ context "when connection does not exist" do
13
+
14
+ before do
15
+ MongoODM.connection = nil
16
+ end
17
+
18
+ it "creates a new Mongo::Connection instance" do
19
+ MongoODM.connection.should be_kind_of(Mongo::Connection)
20
+ end
21
+
22
+ end
23
+
24
+ context "when connection already exists" do
25
+
26
+ before do
27
+ @connection = MongoODM.connection
28
+ end
29
+
30
+ it "returns the current Mongo::Connection instance" do
31
+ MongoODM.connection.should == @connection
32
+ end
33
+
34
+ it "returns a different Mongo::Connection instance per thread" do
35
+ thread = Thread.new { @connection.should_not == MongoODM.connection }
36
+ thread.join
37
+ end
38
+
39
+ it "returns a different Mongo::Connection when configuration changes" do
40
+ MongoODM.config = {:port => 9000}
41
+ @connection.should_not == MongoODM.connection
42
+ end
43
+
44
+ end
45
+
46
+ end
47
+
48
+ describe "#connection=" do
49
+ it "accepts a Mongo::Connection instance to set the connection" do
50
+ connection = Mongo::Connection.new('localhost', 27017)
51
+ MongoODM.connection = connection
52
+ connection.should == MongoODM.connection
53
+ end
54
+ end
55
+
56
+ describe "#config" do
57
+
58
+ context "when no configuration was passed" do
59
+
60
+ it "returns an empty hash" do
61
+ MongoODM.config.should == {}
62
+ end
63
+
64
+ end
65
+
66
+ context "when a custom configuration was passed" do
67
+
68
+ before do
69
+ MongoODM.config = {:host => "localhost", :port => 9000}
70
+ end
71
+
72
+ after do
73
+ MongoODM.config = {}
74
+ end
75
+
76
+ it "should return the configuration as a hash" do
77
+ MongoODM.config.should == {:host => "localhost", :port => 9000}
78
+ end
79
+
80
+ end
81
+
82
+ end
83
+
84
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,4 @@
1
+ --colour
2
+ --format nested
3
+ --loadby mtime
4
+ --reverse
@@ -0,0 +1,12 @@
1
+ # encoding: utf-8
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
4
+
5
+ require "rubygems"
6
+ require "spec"
7
+ require "spec/mocks"
8
+ require "mongo_odm"
9
+
10
+ # Load all example models
11
+ MODELS_PATH = File.join(File.dirname(__FILE__), "models")
12
+ Dir[ File.join(MODELS_PATH, "*.rb") ].sort.each { |file| require file }
metadata ADDED
@@ -0,0 +1,185 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongo_odm
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 8
9
+ version: 0.1.8
10
+ platform: ruby
11
+ authors:
12
+ - Carlos Paramio
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-06-10 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activesupport
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - "="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 3
30
+ - 0
31
+ - 0
32
+ - beta3
33
+ version: 3.0.0.beta3
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: activemodel
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - "="
43
+ - !ruby/object:Gem::Version
44
+ segments:
45
+ - 3
46
+ - 0
47
+ - 0
48
+ - beta3
49
+ version: 3.0.0.beta3
50
+ type: :runtime
51
+ version_requirements: *id002
52
+ - !ruby/object:Gem::Dependency
53
+ name: mongo
54
+ prerelease: false
55
+ requirement: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - "="
59
+ - !ruby/object:Gem::Version
60
+ segments:
61
+ - 1
62
+ - 0
63
+ - 1
64
+ version: 1.0.1
65
+ type: :runtime
66
+ version_requirements: *id003
67
+ - !ruby/object:Gem::Dependency
68
+ name: bson
69
+ prerelease: false
70
+ requirement: &id004 !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - "="
74
+ - !ruby/object:Gem::Version
75
+ segments:
76
+ - 1
77
+ - 0
78
+ - 1
79
+ version: 1.0.1
80
+ type: :runtime
81
+ version_requirements: *id004
82
+ - !ruby/object:Gem::Dependency
83
+ name: bson_ext
84
+ prerelease: false
85
+ requirement: &id005 !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - "="
89
+ - !ruby/object:Gem::Version
90
+ segments:
91
+ - 1
92
+ - 0
93
+ - 1
94
+ version: 1.0.1
95
+ type: :runtime
96
+ version_requirements: *id005
97
+ description: mongo_odm is a flexible persistence module for any Ruby class to MongoDB.
98
+ email: carlos@evolve.st
99
+ executables: []
100
+
101
+ extensions: []
102
+
103
+ extra_rdoc_files:
104
+ - LICENSE
105
+ - README.rdoc
106
+ files:
107
+ - Gemfile
108
+ - LICENSE
109
+ - README.rdoc
110
+ - Rakefile
111
+ - lib/mongo_odm.rb
112
+ - lib/mongo_odm/collection.rb
113
+ - lib/mongo_odm/core_ext/conversions.rb
114
+ - lib/mongo_odm/cursor.rb
115
+ - lib/mongo_odm/document.rb
116
+ - lib/mongo_odm/document/associations.rb
117
+ - lib/mongo_odm/document/associations/has_many.rb
118
+ - lib/mongo_odm/document/associations/has_one.rb
119
+ - lib/mongo_odm/document/attribute_methods.rb
120
+ - lib/mongo_odm/document/attribute_methods/dirty.rb
121
+ - lib/mongo_odm/document/attribute_methods/localization.rb
122
+ - lib/mongo_odm/document/attribute_methods/query.rb
123
+ - lib/mongo_odm/document/attribute_methods/read.rb
124
+ - lib/mongo_odm/document/attribute_methods/write.rb
125
+ - lib/mongo_odm/document/callbacks.rb
126
+ - lib/mongo_odm/document/fields.rb
127
+ - lib/mongo_odm/document/inspect.rb
128
+ - lib/mongo_odm/document/persistence.rb
129
+ - lib/mongo_odm/document/validations.rb
130
+ - lib/mongo_odm/errors.rb
131
+ - lib/mongo_odm/version.rb
132
+ - spec/models/00-blank_slate.rb
133
+ - spec/models/01-shape.rb
134
+ - spec/models/02-circle.rb
135
+ - spec/models/03-big_red_circle.rb
136
+ - spec/mongo_odm/core_ext/conversions_spec.rb
137
+ - spec/mongo_odm/document/fields_spec.rb
138
+ - spec/mongo_odm/document/inspect_spec.rb
139
+ - spec/mongo_odm/document_spec.rb
140
+ - spec/mongo_odm/mongo_odm_spec.rb
141
+ - spec/spec.opts
142
+ - spec/spec_helper.rb
143
+ has_rdoc: true
144
+ homepage: http://github.com/carlosparamio/mongo_odm
145
+ licenses: []
146
+
147
+ post_install_message:
148
+ rdoc_options:
149
+ - --charset=UTF-8
150
+ require_paths:
151
+ - lib
152
+ required_ruby_version: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ segments:
158
+ - 0
159
+ version: "0"
160
+ required_rubygems_version: !ruby/object:Gem::Requirement
161
+ none: false
162
+ requirements:
163
+ - - ">="
164
+ - !ruby/object:Gem::Version
165
+ segments:
166
+ - 0
167
+ version: "0"
168
+ requirements: []
169
+
170
+ rubyforge_project:
171
+ rubygems_version: 1.3.7
172
+ signing_key:
173
+ specification_version: 3
174
+ summary: mongo_odm is a flexible persistence module for any Ruby class to MongoDB.
175
+ test_files:
176
+ - spec/models/00-blank_slate.rb
177
+ - spec/models/01-shape.rb
178
+ - spec/models/02-circle.rb
179
+ - spec/models/03-big_red_circle.rb
180
+ - spec/mongo_odm/core_ext/conversions_spec.rb
181
+ - spec/mongo_odm/document/fields_spec.rb
182
+ - spec/mongo_odm/document/inspect_spec.rb
183
+ - spec/mongo_odm/document_spec.rb
184
+ - spec/mongo_odm/mongo_odm_spec.rb
185
+ - spec/spec_helper.rb