Sha256: 794a6e1faf49466ead3e532cca7f286e19d2446aaadf411881d83af2bedb48aa

Contents?: true

Size: 1.06 KB

Versions: 19

Compression:

Stored size: 1.06 KB

Contents

# Mongoid serialization support for Timespan type.
module Mongoid
  module Fields
    class Timespan
      include Mongoid::Fields::Serializable
    
      def self.instantiate(name, options = {})
        super
      end

      # Deserialize a Timespan given the hash stored by Mongodb
      #
      # @param [Hash] Timespan as hash
      # @return [Timespan] deserialized Timespan
      def deserialize(hash)
        return if !hash
        ::Timespan.new :from => from(hash), :to => to(hash)
      end

      # Serialize a Timespan or a Hash (with Timespan units) or a Duration in some form to
      # a BSON serializable type.
      #
      # @param [Timespan, Hash, Integer, String] value
      # @return [Hash] Timespan in seconds
      def serialize(value)
        return if value.blank?
        timespan = case value
        when ::Timespan
          value
        else
          ::Timespan.new(value)
        end
        {:from => serialize_time(timespan.start_time), :to => serialize_time(timespan.end_time.to_i), :duration => timespan.duration.total }
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
timespan-0.6.0 lib/timespan/mongoid/mongoid_2x.rb
timespan-0.5.9 lib/timespan/mongoid/mongoid_2x.rb
timespan-0.5.7 lib/timespan/mongoid/mongoid_2x.rb
timespan-0.5.6.1 lib/timespan/mongoid/mongoid_2x.rb
timespan-0.5.6 lib/timespan/mongoid/mongoid_2x.rb
timespan-0.5.5 lib/timespan/mongoid/mongoid_2x.rb
timespan-0.5.4 lib/timespan/mongoid/mongoid_2x.rb
timespan-0.5.3 lib/timespan/mongoid/mongoid_2x.rb
timespan-0.5.2 lib/timespan/mongoid/mongoid_2x.rb
timespan-0.5.1 lib/timespan/mongoid/mongoid_2x.rb
timespan-0.5.0 lib/timespan/mongoid/mongoid_2x.rb
timespan-0.4.9 lib/timespan/mongoid/mongoid_2x.rb
timespan-0.4.6 lib/timespan/mongoid/mongoid_2x.rb
timespan-0.4.5 lib/timespan/mongoid/mongoid_2x.rb
timespan-0.4.4 lib/timespan/mongoid/mongoid_2x.rb
timespan-0.4.3 lib/timespan/mongoid/mongoid_2x.rb
timespan-0.4.2 lib/timespan/mongoid/mongoid_2x.rb
timespan-0.4.1 lib/timespan/mongoid/mongoid_2x.rb
timespan-0.4.0 lib/timespan/mongoid/mongoid_2x.rb