Sha256: c30d87a075cfed2ad54bb66678b7c7e5fdc2b0c024e60b269e4ced39166e3b2b

Contents?: true

Size: 855 Bytes

Versions: 1

Compression:

Stored size: 855 Bytes

Contents

require 'active_support/all'
require 'enumerable/associate'
require 'mongoid'

module Mongoid
  class TimeRange
    attr_accessor :from, :to
    delegate :inspect, :[], to: :to_h

    def initialize(from = nil, to = nil)
      self.from = from || Time.now
      self.to = to
    end

    def mongoize
      self.class.mongoize(self)
    end

    def ==(other)
      self.from == other.from && self.to == other.to
    end

    def to_h
      { from: from, to: to }
    end

    def to_a
      [from, to]
    end

    class << self
      def mongoize(object)
        [:from, :to].associate { |key| Time.mongoize(object[key]) }
      end

      def demongoize(hash)
        hash ||= {}
        hash = hash.symbolize_keys
        hash = [:from, :to].associate { |key| Time.demongoize(hash[key]) }
        
        new(*hash.values)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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