Permalink
Browse files

added to_date_object method for use in finders

  • Loading branch information...
1 parent 8e3c4c6 commit 6289e37c42dd7ff49b968d07360b4fd0e8b41cd6 @kevinvangelder kevinvangelder committed Oct 10, 2012
Showing with 7 additions and 3 deletions.
  1. +7 −3 lib/parse_resource/base.rb
View
@@ -88,6 +88,10 @@ def to_pointer
{"__type" => "Pointer", "className" => klass_name, "objectId" => self.id}
end
+ def self.to_date_object(date)
+ {"__type" => "Date", "iso" => date.iso8601} if date && (date.is_a?(Date) || date.is_a?(DateTime) || date.is_a?(Time))
+ end
+
# Creates setter methods for model fields
def create_setters!(k,v)
unless self.respond_to? "#{k}="
@@ -487,9 +491,9 @@ def get_attribute(k)
def set_attribute(k, v)
if v.is_a?(Date) || v.is_a?(Time) || v.is_a?(DateTime)
- v = {"__type" => "Date", "iso" => v.iso8601}
- else
- v = v.to_pointer if v.respond_to?(:to_pointer)
+ v = self.to_date_object(v)
+ elsif v.respond_to?(:to_pointer)
+ v = v.to_pointer
end
@unsaved_attributes[k.to_s] = v unless v == @attributes[k.to_s] # || @unsaved_attributes[k.to_s]
@attributes[k.to_s] = v

2 comments on commit 6289e37

Owner

adelevie replied Nov 9, 2012

Can you post some sample usage? I'm having trouble getting this to work. Thanks for adding, btw.

Contributor

kevinvangelder replied Nov 9, 2012

Post.where("date" => {"$gt" => Post.to_date_object(30.days.ago.midnight)}).first()

Not the most elegant solution but it's operable for now.

Please sign in to comment.