hashrocket-mongomapper 0.3.6 → 0.3.7

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.3.6
1
+ 0.3.7
@@ -320,7 +320,7 @@ module MongoMapper
320
320
  # collection.save returns mongoid
321
321
  def save_to_collection
322
322
  clear_custom_id_flag
323
- collection.save(attributes)
323
+ collection.save(mongodb_attributes)
324
324
  end
325
325
 
326
326
  def update_timestamps
@@ -199,6 +199,27 @@ module MongoMapper
199
199
  attrs.merge!(embedded_association_attributes)
200
200
  end
201
201
 
202
+ def mongodb_attributes
203
+ attrs = HashWithIndifferentAccess.new
204
+ self.class.keys.each_pair do |name, key|
205
+ value =
206
+ if key.native?
207
+ if key.type == Date and date = instance_variable_get("@#{key.name}")
208
+ key.normalize_date(date)
209
+ else
210
+ read_attribute(key.name)
211
+ end
212
+ else
213
+ if embedded_document = read_attribute(key.name)
214
+ embedded_document.mongodb_attributes
215
+ end
216
+ end
217
+
218
+ attrs[name] = value unless value.nil?
219
+ end
220
+ attrs.merge!(embedded_association_attributes)
221
+ end
222
+
202
223
  def [](name)
203
224
  read_attribute(name)
204
225
  end
@@ -1,7 +1,7 @@
1
1
  module MongoMapper
2
2
  class Key
3
3
  # DateTime and Date are currently not supported by mongo's bson so just use Time
4
- NativeTypes = [String, Float, Time, Integer, Boolean, Array, Hash]
4
+ NativeTypes = [String, Float, Time, Date, Integer, Boolean, Array, Hash]
5
5
 
6
6
  attr_accessor :name, :type, :options, :default_value
7
7
 
@@ -34,11 +34,20 @@ module MongoMapper
34
34
  value || []
35
35
  elsif type == Hash
36
36
  HashWithIndifferentAccess.new(value || {})
37
+ elsif type == Date && value
38
+ value.send(:to_date)
37
39
  else
38
40
  value
39
41
  end
40
42
  end
41
43
 
44
+ def normalize_date(value)
45
+ date = Date.parse(value.to_s)
46
+ Time.utc(date.year, date.month, date.day)
47
+ rescue
48
+ nil
49
+ end
50
+
42
51
  private
43
52
  def typecast(value)
44
53
  return value if type.nil?
@@ -50,6 +59,7 @@ module MongoMapper
50
59
  elsif type == Float then value.to_f
51
60
  elsif type == Array then value.to_a
52
61
  elsif type == Time then Time.parse(value.to_s).utc
62
+ elsif type == Date then normalize_date(value)
53
63
  elsif type == Boolean then Boolean.mm_typecast(value)
54
64
  elsif type == Integer
55
65
  # ganked from datamapper
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongomapper}
8
- s.version = "0.3.6"
8
+ s.version = "0.3.7"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["John Nunemaker"]
@@ -84,7 +84,7 @@ Gem::Specification.new do |s|
84
84
  s.rdoc_options = ["--charset=UTF-8"]
85
85
  s.require_paths = ["lib"]
86
86
  s.rubyforge_project = %q{mongomapper}
87
- s.rubygems_version = %q{1.3.4}
87
+ s.rubygems_version = %q{1.3.5}
88
88
  s.summary = %q{Awesome gem for modeling your domain and storing it in mongo}
89
89
  s.test_files = [
90
90
  "test/functional/associations/test_belongs_to_polymorphic_proxy.rb",
@@ -10,6 +10,7 @@ class DocumentTest < Test::Unit::TestCase
10
10
  key :first_name, String
11
11
  key :last_name, String
12
12
  key :age, Integer
13
+ key :date, Date
13
14
  end
14
15
 
15
16
  @document.collection.clear
@@ -773,6 +774,17 @@ class DocumentTest < Test::Unit::TestCase
773
774
  from_db = RealPerson.find(person.id)
774
775
  from_db.name.should == "David"
775
776
  end
777
+
778
+ context "Saving documents with Date key set" do
779
+ setup do
780
+ @doc = @document.new(:first_name => 'John', :age => '27', :date => "12/01/2009")
781
+ end
782
+
783
+ should "save the Date value as a Time object" do
784
+ @doc.save
785
+ @doc.date.should == Date.new(2009, 12, 1)
786
+ end
787
+ end
776
788
  end
777
789
 
778
790
  context "Saving an existing document" do
@@ -14,7 +14,7 @@ class KeyTest < Test::Unit::TestCase
14
14
 
15
15
  context "The Key Class" do
16
16
  should "have the native types defined" do
17
- Key::NativeTypes.should == [String, Float, Time, Integer, Boolean, Array, Hash]
17
+ Key::NativeTypes.should == [String, Float, Time, Date, Integer, Boolean, Array, Hash]
18
18
  end
19
19
  end
20
20
 
@@ -141,6 +141,36 @@ class KeyTest < Test::Unit::TestCase
141
141
  key = Key.new(:foo, Time)
142
142
  key.set('2000-01-01 01:01:01.123456').zone.should == "UTC"
143
143
  end
144
+
145
+ context "Dates" do
146
+ should "correctly typecast String to Date" do
147
+ key = Key.new(:foo, Date)
148
+ value = key.set('12/01/1974')
149
+ value.day.should == 1
150
+ value.month.should == 12
151
+ value.year.should == 1974
152
+ end
153
+
154
+ should "correctly typecast bogus String to nil" do
155
+ key = Key.new(:foo, Date)
156
+ value = key.set('jdsafop874')
157
+ value.should == nil
158
+ end
159
+
160
+ should "correctly typecast empty String to nil" do
161
+ key = Key.new(:foo, Date)
162
+ value = key.set('')
163
+ value.should == nil
164
+ end
165
+
166
+ should "correctly preserve Date" do
167
+ key = Key.new(:foo, Date)
168
+ value = key.set(Date.parse('12/01/1974'))
169
+ value.day.should == 1
170
+ value.month.should == 12
171
+ value.year.should == 1974
172
+ end
173
+ end
144
174
 
145
175
  should "correctly typecast Boolean" do
146
176
  key = Key.new(:foo, Boolean)
@@ -206,6 +236,22 @@ class KeyTest < Test::Unit::TestCase
206
236
  end
207
237
  end
208
238
 
239
+ context "for a Date" do
240
+ should "correctly typecast Dates" do
241
+ key = Key.new(:foo, Date)
242
+ value = key.get(Time.utc(1974, 12, 1))
243
+ value.class.should == Date
244
+ value.day.should == 1
245
+ value.month.should == 12
246
+ value.year.should == 1974
247
+ end
248
+
249
+ should "correctly return nil" do
250
+ key = Key.new(:foo, Date)
251
+ key.get(nil).should == nil
252
+ end
253
+ end
254
+
209
255
  context "for an array" do
210
256
  should "return array" do
211
257
  key = Key.new(:foo, Array)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hashrocket-mongomapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.6
4
+ version: 0.3.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Nunemaker