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 CHANGED
@@ -1 +1 @@
1
- 0.5.5
1
+ 0.5.6
@@ -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
- attributes.each_pair do |key, value|
8
- attributes[key] = fields[key].value(value) if fields[key]
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
@@ -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].value(@attributes[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].value(value)
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 cast(value)
6
- return true if value == "true"
7
- false
5
+ def set(value)
6
+ value == "true"
7
+ end
8
+ def get(value)
9
+ value
8
10
  end
9
11
  end
10
12
  end
@@ -2,8 +2,11 @@ module Mongoid #:nodoc:
2
2
  module Extensions #:nodoc:
3
3
  module Date #:nodoc:
4
4
  module Conversions #:nodoc:
5
- def cast(value)
6
- parse(value.to_s)
5
+ def set(value)
6
+ value.to_time.utc
7
+ end
8
+ def get(value)
9
+ value.to_date
7
10
  end
8
11
  end
9
12
  end
@@ -2,9 +2,12 @@ module Mongoid #:nodoc:
2
2
  module Extensions #:nodoc:
3
3
  module Float #:nodoc:
4
4
  module Conversions #:nodoc:
5
- def cast(value)
5
+ def set(value)
6
6
  value.to_f
7
7
  end
8
+ def get(value)
9
+ value
10
+ end
8
11
  end
9
12
  end
10
13
  end
@@ -2,9 +2,12 @@ module Mongoid #:nodoc:
2
2
  module Extensions #:nodoc:
3
3
  module Integer #:nodoc:
4
4
  module Conversions #:nodoc:
5
- def cast(value)
5
+ def set(value)
6
6
  value =~ /\d/ ? value.to_i : value
7
7
  end
8
+ def get(value)
9
+ value
10
+ end
8
11
  end
9
12
  end
10
13
  end
@@ -2,9 +2,12 @@ module Mongoid #:nodoc:
2
2
  module Extensions #:nodoc:
3
3
  module String #:nodoc:
4
4
  module Conversions #:nodoc:
5
- def cast(value)
5
+ def set(value)
6
6
  value.to_s
7
7
  end
8
+ def get(value)
9
+ value
10
+ end
8
11
  end
9
12
  end
10
13
  end
@@ -2,9 +2,12 @@ module Mongoid #:nodoc:
2
2
  module Extensions #:nodoc:
3
3
  module Time #:nodoc:
4
4
  module Conversions #:nodoc:
5
- def cast(value)
5
+ def set(value)
6
6
  value.to_time
7
7
  end
8
+ def get(value)
9
+ value
10
+ end
8
11
  end
9
12
  end
10
13
  end
@@ -23,10 +23,15 @@ module Mongoid #:nodoc:
23
23
  @type = options[:type] || String
24
24
  end
25
25
 
26
- # Gets the value for the supplied object. If the object is nil, then the
27
- # default value will be returned.
28
- def value(object)
29
- object ? type.cast(object) : default
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
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid}
8
- s.version = "0.5.5"
8
+ s.version = "0.5.6"
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"]
@@ -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
@@ -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
@@ -20,7 +20,6 @@ describe Mongoid::Attributes do
20
20
  { :_id => "4", :street => "Second Street" }
21
21
  ]
22
22
  }
23
- @fields = Person.fields.values
24
23
  end
25
24
 
26
25
  it "returns a properly cast HashWithIndifferentAccess" do
@@ -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 "does not set any attributes" do
300
+ it "sets default attributes" do
301
301
  person = Person.new
302
- person.attributes.empty?.should be_true
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.weight.should == 100
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.weight = nil
490
- @person.weight.should == 100
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 "#cast" do
5
+ describe "#set" do
6
6
 
7
7
  context "when 'true'" do
8
8
 
9
9
  it "returns true" do
10
- Boolean.cast("true").should be_true
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.cast("false").should be_false
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
- describe "#cast" do
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 "parses the string" do
10
- Date.cast("1976/11/19").should == Date.new(1976, 11, 19)
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 "parses the time" do
18
- Date.cast(30.seconds.ago).should == Date.today
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 "#cast" do
5
+ describe "#set" do
6
6
 
7
7
  it "converts the string to a float" do
8
- Float.cast("3.45").should == 3.45
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 "#cast" do
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.cast("32").should == 32
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.cast("foo").should == "foo"
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 "#cast" do
5
+ describe "#set" do
6
6
  it "returns the object to_s" do
7
- String.cast(1).should == "1"
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
- describe "#cast" do
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
- time = Time.local(1976, 11, 19).utc
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 "#value" do
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.value(nil).should == 10
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 "casts the value" do
59
- @type.expects(:cast).with("30").returns(30)
60
- @field.value("30").should == 30
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
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.5.5
4
+ version: 0.5.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Durran Jordan