Sha256: c7c1044e915a9ebf49cf37b552f0f81319a5acf860e4d6be7a7c3ca058a17ad5

Contents?: true

Size: 974 Bytes

Versions: 1

Compression:

Stored size: 974 Bytes

Contents

require "timespan"
require "mongoid/fields"

# Mongoid serialization support for Timespan type.
module Mongoid
  module Fields
    class Timespan
      include Mongoid::Fields::Serializable
    
      # Deserialize a Timespan given the hash stored by Mongodb
      #
      # @param [Hash] Timespan as hash
      # @return [Timespan] deserialized Timespan
      def deserialize(timespan_hash)
        return if !timespan_hash
        ::Timespan.new(:from => timespan_hash[:from], :to => timespan_hash[:to])
      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 = ::Timespan.new(value)
        {:from => time_span.start_time, :to => time_span.end_time, :Timespan => time_span.Timespan}
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
timespan-0.2.0 lib/timespan/mongoid.rb