mongoid 0.5.5 → 0.5.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/lib/mongoid/attributes.rb +4 -2
- data/lib/mongoid/document.rb +2 -2
- data/lib/mongoid/extensions/boolean/conversions.rb +5 -3
- data/lib/mongoid/extensions/date/conversions.rb +5 -2
- data/lib/mongoid/extensions/float/conversions.rb +4 -1
- data/lib/mongoid/extensions/integer/conversions.rb +4 -1
- data/lib/mongoid/extensions/string/conversions.rb +4 -1
- data/lib/mongoid/extensions/time/conversions.rb +4 -1
- data/lib/mongoid/field.rb +9 -4
- data/mongoid.gemspec +1 -1
- data/spec/integration/mongoid/document_spec.rb +15 -0
- data/spec/spec_helper.rb +2 -1
- data/spec/unit/mongoid/attributes_spec.rb +0 -1
- data/spec/unit/mongoid/document_spec.rb +7 -8
- data/spec/unit/mongoid/extensions/array/conversions_spec.rb +2 -2
- data/spec/unit/mongoid/extensions/boolean/conversions_spec.rb +11 -3
- data/spec/unit/mongoid/extensions/date/conversions_spec.rb +26 -5
- data/spec/unit/mongoid/extensions/float/conversions_spec.rb +10 -2
- data/spec/unit/mongoid/extensions/integer/conversions_spec.rb +12 -3
- data/spec/unit/mongoid/extensions/object/conversions_spec.rb +1 -1
- data/spec/unit/mongoid/extensions/string/conversions_spec.rb +9 -2
- data/spec/unit/mongoid/extensions/time/conversions_spec.rb +12 -3
- data/spec/unit/mongoid/field_spec.rb +19 -5
- metadata +1 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.5.
|
1
|
+
0.5.6
|
data/lib/mongoid/attributes.rb
CHANGED
@@ -4,9 +4,11 @@ module Mongoid #:nodoc:
|
|
4
4
|
# field exists for them on the +Document+.
|
5
5
|
def process(fields, params)
|
6
6
|
attributes = HashWithIndifferentAccess.new(params)
|
7
|
-
|
8
|
-
|
7
|
+
fields.values.each do |field|
|
8
|
+
value = field.set(attributes[field.name])
|
9
|
+
attributes[field.name] = value if value
|
9
10
|
end
|
11
|
+
attributes
|
10
12
|
end
|
11
13
|
|
12
14
|
end
|
data/lib/mongoid/document.rb
CHANGED
@@ -209,7 +209,7 @@ module Mongoid #:nodoc:
|
|
209
209
|
|
210
210
|
# Read from the attributes hash.
|
211
211
|
def read_attribute(name)
|
212
|
-
fields[name].
|
212
|
+
fields[name].get(@attributes[name])
|
213
213
|
end
|
214
214
|
|
215
215
|
# Returns the id of the Document
|
@@ -225,7 +225,7 @@ module Mongoid #:nodoc:
|
|
225
225
|
|
226
226
|
# Write to the attributes hash.
|
227
227
|
def write_attribute(name, value)
|
228
|
-
@attributes[name] = fields[name].
|
228
|
+
@attributes[name] = fields[name].set(value)
|
229
229
|
notify
|
230
230
|
end
|
231
231
|
|
@@ -2,9 +2,11 @@ module Mongoid #:nodoc:
|
|
2
2
|
module Extensions #:nodoc:
|
3
3
|
module Boolean #:nodoc:
|
4
4
|
module Conversions #:nodoc:
|
5
|
-
def
|
6
|
-
|
7
|
-
|
5
|
+
def set(value)
|
6
|
+
value == "true"
|
7
|
+
end
|
8
|
+
def get(value)
|
9
|
+
value
|
8
10
|
end
|
9
11
|
end
|
10
12
|
end
|
data/lib/mongoid/field.rb
CHANGED
@@ -23,10 +23,15 @@ module Mongoid #:nodoc:
|
|
23
23
|
@type = options[:type] || String
|
24
24
|
end
|
25
25
|
|
26
|
-
#
|
27
|
-
# default
|
28
|
-
def
|
29
|
-
object ? type.
|
26
|
+
# Used for setting an object in the attributes hash. If nil is provided the
|
27
|
+
# default will get returned if it exists.
|
28
|
+
def set(object)
|
29
|
+
object ? type.set(object) : default
|
30
|
+
end
|
31
|
+
|
32
|
+
# Used for retrieving the object out of the attributes hash.
|
33
|
+
def get(object)
|
34
|
+
type.get(object)
|
30
35
|
end
|
31
36
|
|
32
37
|
end
|
data/mongoid.gemspec
CHANGED
@@ -149,4 +149,19 @@ describe Mongoid::Document do
|
|
149
149
|
|
150
150
|
end
|
151
151
|
|
152
|
+
context "typecasting" do
|
153
|
+
|
154
|
+
before do
|
155
|
+
@date = Date.new(1976, 7, 4)
|
156
|
+
@person = Person.new(:dob => @date)
|
157
|
+
@person.save
|
158
|
+
end
|
159
|
+
|
160
|
+
it "properly casts dates and times" do
|
161
|
+
person = Person.first
|
162
|
+
person.dob.should == @date
|
163
|
+
end
|
164
|
+
|
165
|
+
end
|
166
|
+
|
152
167
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -19,7 +19,8 @@ class Person < Mongoid::Document
|
|
19
19
|
include Mongoid::Timestamps
|
20
20
|
field :title
|
21
21
|
field :terms, :type => Boolean
|
22
|
-
field :age, :type => Integer
|
22
|
+
field :age, :type => Integer, :default => 100
|
23
|
+
field :dob, :type => Date
|
23
24
|
has_many :addresses
|
24
25
|
has_many :phone_numbers, :class_name => "Phone"
|
25
26
|
has_one :name
|
@@ -212,7 +212,7 @@ describe Mongoid::Document do
|
|
212
212
|
describe "#first" do
|
213
213
|
|
214
214
|
before do
|
215
|
-
@attributes =
|
215
|
+
@attributes = HashWithIndifferentAccess.new(:age => 100)
|
216
216
|
end
|
217
217
|
|
218
218
|
context "when a selector is provided" do
|
@@ -297,9 +297,10 @@ describe Mongoid::Document do
|
|
297
297
|
|
298
298
|
context "with no attributes" do
|
299
299
|
|
300
|
-
it "
|
300
|
+
it "sets default attributes" do
|
301
301
|
person = Person.new
|
302
|
-
person.attributes.empty?.should
|
302
|
+
person.attributes.empty?.should be_false
|
303
|
+
person.age.should == 100
|
303
304
|
end
|
304
305
|
|
305
306
|
end
|
@@ -446,12 +447,11 @@ describe Mongoid::Document do
|
|
446
447
|
context "when attribute does not exist" do
|
447
448
|
|
448
449
|
before do
|
449
|
-
Person.field :weight, :default => 100
|
450
450
|
@person = Person.new
|
451
451
|
end
|
452
452
|
|
453
453
|
it "returns the default value" do
|
454
|
-
@person.
|
454
|
+
@person.age.should == 100
|
455
455
|
end
|
456
456
|
|
457
457
|
end
|
@@ -481,13 +481,12 @@ describe Mongoid::Document do
|
|
481
481
|
context "when attribute does not exist" do
|
482
482
|
|
483
483
|
before do
|
484
|
-
Person.field :weight, :default => 100, :type => Integer
|
485
484
|
@person = Person.new
|
486
485
|
end
|
487
486
|
|
488
487
|
it "returns the default value" do
|
489
|
-
@person.
|
490
|
-
@person.
|
488
|
+
@person.age = nil
|
489
|
+
@person.age.should == 100
|
491
490
|
end
|
492
491
|
|
493
492
|
end
|
@@ -7,8 +7,8 @@ describe Mongoid::Extensions::Array::Conversions do
|
|
7
7
|
it "collects each of its attributes" do
|
8
8
|
array = [Person.new(:title => "Sir"), Person.new(:title => "Madam")]
|
9
9
|
array.mongoidize.should ==
|
10
|
-
[HashWithIndifferentAccess.new({ :title => "Sir" }),
|
11
|
-
HashWithIndifferentAccess.new({ :title => "Madam" })]
|
10
|
+
[HashWithIndifferentAccess.new({ :title => "Sir", :age => 100 }),
|
11
|
+
HashWithIndifferentAccess.new({ :title => "Madam", :age => 100 })]
|
12
12
|
end
|
13
13
|
|
14
14
|
end
|
@@ -2,12 +2,12 @@ require File.join(File.dirname(__FILE__), "/../../../../spec_helper.rb")
|
|
2
2
|
|
3
3
|
describe Mongoid::Extensions::Boolean::Conversions do
|
4
4
|
|
5
|
-
describe "#
|
5
|
+
describe "#set" do
|
6
6
|
|
7
7
|
context "when 'true'" do
|
8
8
|
|
9
9
|
it "returns true" do
|
10
|
-
Boolean.
|
10
|
+
Boolean.set("true").should be_true
|
11
11
|
end
|
12
12
|
|
13
13
|
end
|
@@ -15,11 +15,19 @@ describe Mongoid::Extensions::Boolean::Conversions do
|
|
15
15
|
context "when 'false'" do
|
16
16
|
|
17
17
|
it "returns false" do
|
18
|
-
Boolean.
|
18
|
+
Boolean.set("false").should be_false
|
19
19
|
end
|
20
20
|
|
21
21
|
end
|
22
22
|
|
23
23
|
end
|
24
24
|
|
25
|
+
describe "#get" do
|
26
|
+
|
27
|
+
it "returns the boolean" do
|
28
|
+
Boolean.get(false).should be_false
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
25
33
|
end
|
@@ -2,23 +2,44 @@ require File.join(File.dirname(__FILE__), "/../../../../spec_helper.rb")
|
|
2
2
|
|
3
3
|
describe Mongoid::Extensions::Date::Conversions do
|
4
4
|
|
5
|
-
|
5
|
+
before do
|
6
|
+
@time = Date.today.to_time.utc
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "#set" do
|
6
10
|
|
7
11
|
context "when string provided" do
|
8
12
|
|
9
|
-
it "
|
10
|
-
Date.
|
13
|
+
it "returns a time from the string" do
|
14
|
+
Date.set(@time.to_s).should == @time
|
11
15
|
end
|
12
16
|
|
13
17
|
end
|
14
18
|
|
15
19
|
context "when time provided" do
|
16
20
|
|
17
|
-
it "
|
18
|
-
Date.
|
21
|
+
it "returns the time" do
|
22
|
+
Date.set(@time).should == @time
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
context "when a date provided" do
|
28
|
+
|
29
|
+
it "returns a time from the date" do
|
30
|
+
Date.set(@time.to_date).should == @time
|
19
31
|
end
|
20
32
|
|
21
33
|
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "#get" do
|
38
|
+
|
39
|
+
it "converts the time back to a date" do
|
40
|
+
Date.get(@time).should == @time.to_date
|
41
|
+
end
|
42
|
+
|
22
43
|
end
|
23
44
|
|
24
45
|
end
|
@@ -2,10 +2,18 @@ require File.join(File.dirname(__FILE__), "/../../../../spec_helper.rb")
|
|
2
2
|
|
3
3
|
describe Mongoid::Extensions::Float::Conversions do
|
4
4
|
|
5
|
-
describe "#
|
5
|
+
describe "#set" do
|
6
6
|
|
7
7
|
it "converts the string to a float" do
|
8
|
-
Float.
|
8
|
+
Float.set("3.45").should == 3.45
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "#get" do
|
14
|
+
|
15
|
+
it "returns the float" do
|
16
|
+
Float.get(3.45).should == 3.45
|
9
17
|
end
|
10
18
|
|
11
19
|
end
|
@@ -2,12 +2,12 @@ require File.join(File.dirname(__FILE__), "/../../../../spec_helper.rb")
|
|
2
2
|
|
3
3
|
describe Mongoid::Extensions::Integer::Conversions do
|
4
4
|
|
5
|
-
describe "#
|
5
|
+
describe "#set" do
|
6
6
|
|
7
7
|
context "when string is a number" do
|
8
8
|
|
9
9
|
it "converts the string to an Integer" do
|
10
|
-
Integer.
|
10
|
+
Integer.set("32").should == 32
|
11
11
|
end
|
12
12
|
|
13
13
|
end
|
@@ -15,10 +15,19 @@ describe Mongoid::Extensions::Integer::Conversions do
|
|
15
15
|
context "when string is not a number" do
|
16
16
|
|
17
17
|
it "returns the string" do
|
18
|
-
Integer.
|
18
|
+
Integer.set("foo").should == "foo"
|
19
19
|
end
|
20
20
|
|
21
21
|
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "#get" do
|
26
|
+
|
27
|
+
it "returns the integer" do
|
28
|
+
Integer.get(44).should == 44
|
29
|
+
end
|
30
|
+
|
22
31
|
end
|
23
32
|
|
24
33
|
end
|
@@ -6,7 +6,7 @@ describe Mongoid::Extensions::Object::Conversions do
|
|
6
6
|
|
7
7
|
it "returns its attributes" do
|
8
8
|
Person.new(:title => "Sir").mongoidize.should ==
|
9
|
-
HashWithIndifferentAccess.new({ :title => "Sir" })
|
9
|
+
HashWithIndifferentAccess.new({ :title => "Sir", :age => 100 })
|
10
10
|
end
|
11
11
|
|
12
12
|
end
|
@@ -2,9 +2,16 @@ require File.join(File.dirname(__FILE__), "/../../../../spec_helper.rb")
|
|
2
2
|
|
3
3
|
describe Mongoid::Extensions::String::Conversions do
|
4
4
|
|
5
|
-
describe "#
|
5
|
+
describe "#set" do
|
6
6
|
it "returns the object to_s" do
|
7
|
-
String.
|
7
|
+
String.set(1).should == "1"
|
8
8
|
end
|
9
9
|
end
|
10
|
+
|
11
|
+
describe "#get" do
|
12
|
+
it "returns the string" do
|
13
|
+
String.get("test").should == "test"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
10
17
|
end
|
@@ -2,13 +2,22 @@ require File.join(File.dirname(__FILE__), "/../../../../spec_helper.rb")
|
|
2
2
|
|
3
3
|
describe Mongoid::Extensions::Time::Conversions do
|
4
4
|
|
5
|
-
|
5
|
+
before do
|
6
|
+
@time = Time.local(1976, 11, 19).utc
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "#set" do
|
6
10
|
context "when value is a string" do
|
7
11
|
it "converts to a time" do
|
8
|
-
|
9
|
-
Time.cast(time.to_s).should == time
|
12
|
+
Time.set(@time.to_s).should == @time
|
10
13
|
end
|
11
14
|
end
|
12
15
|
end
|
13
16
|
|
17
|
+
describe "#get" do
|
18
|
+
it "returns the time" do
|
19
|
+
Time.get(@time).should == @time
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
14
23
|
end
|
@@ -38,7 +38,7 @@ describe Mongoid::Field do
|
|
38
38
|
|
39
39
|
end
|
40
40
|
|
41
|
-
describe "#
|
41
|
+
describe "#set" do
|
42
42
|
|
43
43
|
before do
|
44
44
|
@type = mock
|
@@ -48,20 +48,34 @@ describe Mongoid::Field do
|
|
48
48
|
context "nil is provided" do
|
49
49
|
|
50
50
|
it "returns the default value" do
|
51
|
-
@field.
|
51
|
+
@field.set(nil).should == 10
|
52
52
|
end
|
53
53
|
|
54
54
|
end
|
55
55
|
|
56
56
|
context "value is provided" do
|
57
57
|
|
58
|
-
it "
|
59
|
-
@type.expects(:
|
60
|
-
@field.
|
58
|
+
it "sets the value" do
|
59
|
+
@type.expects(:set).with("30").returns(30)
|
60
|
+
@field.set("30").should == 30
|
61
61
|
end
|
62
62
|
|
63
63
|
end
|
64
64
|
|
65
65
|
end
|
66
66
|
|
67
|
+
describe "#get" do
|
68
|
+
|
69
|
+
before do
|
70
|
+
@type = mock
|
71
|
+
@field = Mongoid::Field.new(:score, :default => 10, :type => @type)
|
72
|
+
end
|
73
|
+
|
74
|
+
it "returns the value" do
|
75
|
+
@type.expects(:get).with(30).returns(30)
|
76
|
+
@field.get(30).should == 30
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
80
|
+
|
67
81
|
end
|