mongoid 0.7.3 → 0.7.4
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/mongoid.rb +1 -0
- data/lib/mongoid/associations/has_many.rb +1 -1
- data/lib/mongoid/attributes.rb +0 -8
- data/lib/mongoid/document.rb +14 -2
- data/lib/mongoid/extensions/date/conversions.rb +1 -1
- data/mongoid.gemspec +6 -4
- data/perf/benchmark.rb +66 -0
- data/spec/spec_helper.rb +6 -0
- data/spec/unit/mongoid/document_spec.rb +22 -2
- data/spec/unit/mongoid/extensions/date/conversions_spec.rb +8 -0
- metadata +3 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.7.
|
1
|
+
0.7.4
|
data/lib/mongoid.rb
CHANGED
data/lib/mongoid/attributes.rb
CHANGED
@@ -4,18 +4,10 @@ module Mongoid #:nodoc:
|
|
4
4
|
# field exists for them on the +Document+.
|
5
5
|
def process(params = nil)
|
6
6
|
@attributes = (params || {}).with_indifferent_access
|
7
|
-
process_fields
|
8
7
|
process_attributes
|
9
8
|
end
|
10
9
|
|
11
10
|
protected
|
12
|
-
def process_fields
|
13
|
-
fields.values.each do |field|
|
14
|
-
value = field.set(@attributes[field.name])
|
15
|
-
@attributes[field.name] = value if value
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
11
|
def process_attributes
|
20
12
|
@attributes.each_pair do |key, value|
|
21
13
|
send("#{key}=", value)
|
data/lib/mongoid/document.rb
CHANGED
@@ -50,6 +50,11 @@ module Mongoid #:nodoc:
|
|
50
50
|
Criteria.translate(*args).count(self)
|
51
51
|
end
|
52
52
|
|
53
|
+
# Returns a hash of all the default values
|
54
|
+
def defaults
|
55
|
+
@defaults
|
56
|
+
end
|
57
|
+
|
53
58
|
# return true if the +Document+ is embedded in another +Documnet+.
|
54
59
|
def embedded?
|
55
60
|
@embedded == true
|
@@ -69,7 +74,9 @@ module Mongoid #:nodoc:
|
|
69
74
|
# <tt>field :score, :default => 0</tt>
|
70
75
|
def field(name, options = {})
|
71
76
|
@fields ||= {}.with_indifferent_access
|
77
|
+
@defaults ||= {}.with_indifferent_access
|
72
78
|
@fields[name.to_s] = Field.new(name.to_s, options)
|
79
|
+
@defaults[name.to_s] = options[:default] if options[:default]
|
73
80
|
define_method(name) { read_attribute(name) }
|
74
81
|
define_method("#{name}=") { |value| write_attribute(name, value) }
|
75
82
|
end
|
@@ -188,7 +195,7 @@ module Mongoid #:nodoc:
|
|
188
195
|
|
189
196
|
# Performs equality checking on the attributes.
|
190
197
|
def ==(other)
|
191
|
-
|
198
|
+
return false unless other.is_a?(Document)
|
192
199
|
@attributes.except(:modified_at).except(:created_at) ==
|
193
200
|
other.attributes.except(:modified_at).except(:created_at)
|
194
201
|
end
|
@@ -198,6 +205,11 @@ module Mongoid #:nodoc:
|
|
198
205
|
self.class.collection
|
199
206
|
end
|
200
207
|
|
208
|
+
# Returns the class defaults
|
209
|
+
def defaults
|
210
|
+
self.class.defaults
|
211
|
+
end
|
212
|
+
|
201
213
|
# Return true if the +Document+ is embedded in another +Document+.
|
202
214
|
def embedded?
|
203
215
|
self.class.embedded?
|
@@ -225,7 +237,7 @@ module Mongoid #:nodoc:
|
|
225
237
|
# Instantiate a new Document, setting the Document's attributes if given.
|
226
238
|
# If no attributes are provided, they will be initialized with an empty Hash.
|
227
239
|
def initialize(attrs = {})
|
228
|
-
process(attrs)
|
240
|
+
process(defaults.merge(attrs))
|
229
241
|
@new_record = true if id.nil?
|
230
242
|
generate_key
|
231
243
|
end
|
data/mongoid.gemspec
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
# Generated by jeweler
|
2
|
-
# DO NOT EDIT THIS FILE
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{mongoid}
|
8
|
-
s.version = "0.7.
|
8
|
+
s.version = "0.7.4"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Durran Jordan"]
|
12
|
-
s.date = %q{2009-11-
|
12
|
+
s.date = %q{2009-11-14}
|
13
13
|
s.email = %q{durran@gmail.com}
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"README.textile"
|
@@ -58,6 +58,7 @@ Gem::Specification.new do |s|
|
|
58
58
|
"lib/mongoid/timestamps.rb",
|
59
59
|
"lib/mongoid/versioning.rb",
|
60
60
|
"mongoid.gemspec",
|
61
|
+
"perf/benchmark.rb",
|
61
62
|
"spec/integration/mongoid/document_spec.rb",
|
62
63
|
"spec/spec.opts",
|
63
64
|
"spec/spec_helper.rb",
|
@@ -163,3 +164,4 @@ Gem::Specification.new do |s|
|
|
163
164
|
s.add_dependency(%q<mongo_ext>, ["= 0.16"])
|
164
165
|
end
|
165
166
|
end
|
167
|
+
|
data/perf/benchmark.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "ruby-prof"
|
3
|
+
require "mongoid"
|
4
|
+
|
5
|
+
class Person < Mongoid::Document
|
6
|
+
include Mongoid::Timestamps
|
7
|
+
include Mongoid::Versioning
|
8
|
+
field :birth_date, :type => Date
|
9
|
+
has_one :name
|
10
|
+
has_many :addresses
|
11
|
+
has_many :phones
|
12
|
+
end
|
13
|
+
|
14
|
+
class Name < Mongoid::Document
|
15
|
+
include Mongoid::Timestamps
|
16
|
+
field :given
|
17
|
+
field :family
|
18
|
+
field :middle
|
19
|
+
belongs_to :person
|
20
|
+
end
|
21
|
+
|
22
|
+
class Address < Mongoid::Document
|
23
|
+
include Mongoid::Timestamps
|
24
|
+
field :street
|
25
|
+
field :city
|
26
|
+
field :state
|
27
|
+
field :post_code
|
28
|
+
field :type
|
29
|
+
belongs_to :person
|
30
|
+
end
|
31
|
+
|
32
|
+
class Phone < Mongoid::Document
|
33
|
+
include Mongoid::Timestamps
|
34
|
+
field :country_code, :type => Integer
|
35
|
+
field :number
|
36
|
+
field :type
|
37
|
+
belongs_to :person
|
38
|
+
end
|
39
|
+
|
40
|
+
Mongoid.connect_to("mongoid_performance")
|
41
|
+
|
42
|
+
Mongoid.database.collection("people").drop
|
43
|
+
|
44
|
+
RubyProf.start
|
45
|
+
|
46
|
+
1000.times do |n|
|
47
|
+
person = Person.new(:birth_date => Date.new(1970, 1, 1))
|
48
|
+
name = Name.new(:given => "James", :family => "Kirk", :middle => "Tiberius")
|
49
|
+
address = Address.new(
|
50
|
+
:street => "1 Starfleet Command Way",
|
51
|
+
:city => "San Francisco",
|
52
|
+
:state => "CA",
|
53
|
+
:post_code => "94133",
|
54
|
+
:type => "Work"
|
55
|
+
)
|
56
|
+
phone = Phone.new(:country_code => 1, :number => "415-555-1212", :type => "Mobile")
|
57
|
+
person.name = name
|
58
|
+
person.addresses << address
|
59
|
+
person.phones << phone
|
60
|
+
person.save
|
61
|
+
puts "Saved person #{n}"
|
62
|
+
end
|
63
|
+
|
64
|
+
result = RubyProf.stop
|
65
|
+
printer = RubyProf::FlatPrinter.new(result)
|
66
|
+
printer.print(STDOUT, 0)
|
data/spec/spec_helper.rb
CHANGED
@@ -27,6 +27,7 @@ class Person < Mongoid::Document
|
|
27
27
|
field :dob, :type => Date
|
28
28
|
field :mixed_drink, :type => MixedDrink
|
29
29
|
field :employer_id
|
30
|
+
field :lunch_time, :type => Time
|
30
31
|
has_many :addresses
|
31
32
|
has_many :phone_numbers, :class_name => "Phone"
|
32
33
|
has_one :name
|
@@ -117,3 +118,8 @@ class Post < Mongoid::Document
|
|
117
118
|
include Mongoid::Versioning
|
118
119
|
field :title
|
119
120
|
end
|
121
|
+
|
122
|
+
class Game < Mongoid::Document
|
123
|
+
field :high_score, :default => 500
|
124
|
+
field :score, :default => 0
|
125
|
+
end
|
@@ -48,8 +48,8 @@ describe Mongoid::Document do
|
|
48
48
|
|
49
49
|
context "when other object is not a Document" do
|
50
50
|
|
51
|
-
it "
|
52
|
-
|
51
|
+
it "returns false" do
|
52
|
+
Person.new.==("Test").should be_false
|
53
53
|
end
|
54
54
|
|
55
55
|
end
|
@@ -134,6 +134,26 @@ describe Mongoid::Document do
|
|
134
134
|
|
135
135
|
end
|
136
136
|
|
137
|
+
describe ".defaults" do
|
138
|
+
|
139
|
+
it "returns a hash of all the default values" do
|
140
|
+
Game.defaults.should == { "high_score" => 500, "score" => 0 }
|
141
|
+
end
|
142
|
+
|
143
|
+
end
|
144
|
+
|
145
|
+
describe "#defaults" do
|
146
|
+
|
147
|
+
before do
|
148
|
+
@game = Game.new
|
149
|
+
end
|
150
|
+
|
151
|
+
it "returns the class defaults" do
|
152
|
+
@game.defaults.should == { "high_score" => 500, "score" => 0 }
|
153
|
+
end
|
154
|
+
|
155
|
+
end
|
156
|
+
|
137
157
|
describe ".embedded?" do
|
138
158
|
|
139
159
|
context "when the document is embedded" do
|
@@ -14,6 +14,14 @@ describe Mongoid::Extensions::Date::Conversions do
|
|
14
14
|
Date.set(@time.utc.to_s).should == @time
|
15
15
|
end
|
16
16
|
|
17
|
+
context "when string is empty" do
|
18
|
+
|
19
|
+
it "returns nil" do
|
20
|
+
Date.set("").should == nil
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
17
25
|
end
|
18
26
|
|
19
27
|
context "when time provided" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Durran Jordan
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-11-
|
12
|
+
date: 2009-11-14 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -114,6 +114,7 @@ files:
|
|
114
114
|
- lib/mongoid/timestamps.rb
|
115
115
|
- lib/mongoid/versioning.rb
|
116
116
|
- mongoid.gemspec
|
117
|
+
- perf/benchmark.rb
|
117
118
|
- spec/integration/mongoid/document_spec.rb
|
118
119
|
- spec/spec.opts
|
119
120
|
- spec/spec_helper.rb
|