Sha256: 829fb50fce8d86fc44593d613aaf7b6c265feb2b745c1ea3ade1ee8e49333cfe

Contents?: true

Size: 1.86 KB

Versions: 5

Compression:

Stored size: 1.86 KB

Contents

require "timespan"
require "mongoid/fields"
require "timespan/mongoid/timespanned"

# http://whytheluckystiff.net/articles/seeingMetaclassesClearly.html
class Object 
  def meta_def name, &blk
    (class << self; self; end).instance_eval { define_method name, &blk }
  end
end

module Mongoid
  module Fields
    class Timespan
      class << self
        attr_writer :start_field, :end_field

        def start_field
          @start_field || :from
        end

        def end_field
          @end_field || :to
        end
      end

      # protected

      module Methods
        def from hash
          from_value = hash['from'] || hash[:from]
          raise ArgumentError, ":from is nil, #{hash.inspect}" if from_value.nil?
          deserialize_time from_value
        end

        def to hash
          to_value = hash['to'] || hash[:to]
          raise ArgumentError, ":to is nil, #{hash.inspect}" if to_value.nil?
          deserialize_time to_value
        end

        def serialize_time time
          raise ArgumentError, "Can't serialize time from nil" if time.nil?
          time.to_i
        end

        def deserialize_time millisecs
          raise ArgumentError, "Can't deserialize time from nil" if millisecs.nil?
          Time.at millisecs
        end      
      end

      include Methods
      extend Methods
    end
  end
end

if defined?(Mongoid::Fields) && Mongoid::Fields.respond_to?(:option)
  Mongoid::Fields.option :between do |model, field, options|  
    name = field.name.to_sym
    model.class_eval do
      meta_def :"#{name}_between" do |from, to|
        self.where(:"#{name}.#{TimeSpan.start_field}".gte => from.to_i, :"#{name}.#{TimeSpan.end_field}".lte => to.to_i)
      end
    end
  end
end

module Mongoid
  MAJOR_VERSION =  Mongoid::VERSION > '3' ? 3 : 2
end

require "timespan/mongoid/mongoid_#{Mongoid::MAJOR_VERSION}x"

TimeSpan = Mongoid::Fields::Timespan

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
timespan-0.4.6 lib/timespan/mongoid.rb
timespan-0.4.5 lib/timespan/mongoid.rb
timespan-0.4.4 lib/timespan/mongoid.rb
timespan-0.4.3 lib/timespan/mongoid.rb
timespan-0.4.2 lib/timespan/mongoid.rb