Class: Mongoid::Fields::Duration
- Inherits:
-
Object
- Object
- Mongoid::Fields::Duration
- Includes:
- Serializable
- Defined in:
- lib/duration/mongoid.rb
Instance Method Summary collapse
-
#deserialize(seconds) ⇒ Duration
Deserialize a Duration given the amount of seconds stored by Mongodb.
-
#serialize(value) ⇒ Integer
Serialize a Duration or a Hash (with duration units) or a amount of seconds to a BSON serializable type.
Instance Method Details
#deserialize(seconds) ⇒ Duration
Deserialize a Duration given the amount of seconds stored by Mongodb
14 15 16 17 |
# File 'lib/duration/mongoid.rb', line 14 def deserialize(seconds) return if !seconds ::Duration.new(seconds) end |
#serialize(value) ⇒ Integer
Serialize a Duration or a Hash (with duration units) or a amount of seconds to a BSON serializable type.
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/duration/mongoid.rb', line 24 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)} return if value.blank? ::Duration.new(value).to_i elsif value.respond_to?(:to_i) value.to_i end end |