Sha256: 17094d153ec4cf95105a6a926979b36d5f4b29622f4d4c22af7f8923817b5334

Contents?: true

Size: 1.15 KB

Versions: 2

Compression:

Stored size: 1.15 KB

Contents

class Timespan
  module Mongoize
    extend ActiveSupport::Concern
    # See http://mongoid.org/en/mongoid/docs/upgrading.html        

    # 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 mongoize
      {:from => serialize_time(start_time), :to => serialize_time(end_time), :duration => duration.total }
    end

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

      # TODO
      # def evolve(object)
      #   { "$gte" => object.first, "$lte" => object.last }
      # end
    end
  end
end


class Timespan
  include Mongoize

  protected

  include Mongoid::Fields::Timespan::Methods
  extend Mongoid::Fields::Timespan::Methods
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
timespan-0.4.1 lib/timespan/mongoid/mongoid_3x.rb
timespan-0.4.0 lib/timespan/mongoid/mongoid_3x.rb