lib/timespan/mongoid.rb in timespan-0.3.2 vs lib/timespan/mongoid.rb in timespan-0.4.0
- old
+ new
@@ -6,25 +6,13 @@
def meta_def name, &blk
(class << self; self; end).instance_eval { define_method name, &blk }
end
end
-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
-
-# Mongoid serialization support for Timespan type.
module Mongoid
module Fields
class Timespan
- include Mongoid::Fields::Serializable
-
class << self
attr_writer :start_field, :end_field
def start_field
@start_field || :from
@@ -33,60 +21,55 @@
def end_field
@end_field || :to
end
end
- def self.instantiate(name, options = {})
- super
- end
+ # protected
- # 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
+ 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
- # 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)
+ 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
- {:from => serialize_time(timespan.start_time), :to => serialize_time(timespan.end_time.to_i), :duration => timespan.duration.total }
- end
- protected
+ def serialize_time time
+ raise ArgumentError, "Can't serialize time from nil" if time.nil?
+ time.to_i
+ end
- def from hash
- from_value = hash['from'] || hash[:from]
- raise ArgumentError, ":from is nil, #{hash.inspect}" if from_value.nil?
- deserialize_time from_value
+ def deserialize_time millisecs
+ raise ArgumentError, "Can't deserialize time from nil" if millisecs.nil?
+ Time.at millisecs
+ end
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
+ include Methods
+ extend Methods
+ end
+ end
+end
- def serialize_time time
- time.to_i
+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
-
- def deserialize_time millisecs
- Time.at millisecs
- 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
\ No newline at end of file