Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Updated Mongoid serialiser for Mongoid >= 3.x

  • Loading branch information...
commit 7daef35f3603558b216704c3333a4a12f5e62579 1 parent 39dc650
@xiy xiy authored
Showing with 39 additions and 19 deletions.
  1. +39 −19 lib/duration/mongoid.rb
View
58 lib/duration/mongoid.rb
@@ -5,30 +5,50 @@
module Mongoid
module Fields
class Duration
- include Mongoid::Fields::Serializable
- # Deserialize a Duration given the amount of seconds stored by Mongodb
- #
- # @param [Integer, nil] duration in seconds
- # @return [Duration] deserialized Duration
- def deserialize(seconds)
- return if !seconds
+ # Instantiates a new Duration object
+ def initialize(seconds)
::Duration.new(seconds)
end
- # Serialize a Duration or a Hash (with duration units) or a amount of seconds to
- # a BSON serializable type.
- #
- # @param [Duration, Hash, Integer] value
- # @return [Integer] duration in seconds
- def serialize(value)
- return if value.blank?
- if value.is_a?(Hash)
- value.delete_if{|k, v| v.blank? || !::Duration::UNITS.include?(k.to_sym)}
+ # Converts the Duration object into a MongoDB friendly value.
+ def mongoize
+ self.to_i
+ end
+
+ class << self
+ # Deserialize a Duration given the amount of seconds stored by Mongodb
+ #
+ # @param [Integer, nil] duration in seconds
+ # @return [Duration] deserialized Duration
+ def demongoize(seconds)
+ return if !seconds
+ ::Duration.new(seconds)
+ end
+
+ # Serialize a Duration or a Hash (with duration units) or a amount of seconds to
+ # a BSON serializable type.
+ #
+ # @param [Duration, Hash, Integer] value
+ # @return [Integer] duration in seconds
+ def mongoize(value)
return if value.blank?
- ::Duration.new(value).to_i
- elsif value.respond_to?(:to_i)
- value.to_i
+ if value.is_a?(Hash)
+ value.delete_if{|k, v| v.blank? || !::Duration::UNITS.include?(k.to_sym)}
+ return if value.blank?
+ ::Duration.new(value).to_i
+ elsif value.respond_to?(:to_i)
+ value.to_i
+ end
+ end
+
+ # Converts the object that was supplied to a criteria and converts it
+ # into a database friendly form.
+ def evolve(object)
+ case object
+ when ::Duration then object.mongoize
+ else object
+ end
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.