Skip to content

Commit

Permalink
support mongoid 3x
Browse files Browse the repository at this point in the history
  • Loading branch information
kristianmandrup committed Jun 5, 2012
1 parent 94df156 commit c2ad0cb
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 41 deletions.
55 changes: 14 additions & 41 deletions lib/timespan/mongoid.rb
Expand Up @@ -8,21 +8,9 @@ def meta_def 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

Expand All @@ -35,35 +23,6 @@ def end_field
end
end

def self.instantiate(name, options = {})
super
end

# 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

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

protected

def from hash
Expand All @@ -84,9 +43,23 @@ def serialize_time time

def deserialize_time millisecs
Time.at millisecs
end
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

mongoid_version = Mongoid::VERSION > '3' ? 3 : 2
require "timespan/mongoid/mongoid_#{mongoid_version}x"

TimeSpan = Mongoid::Fields::Timespan
37 changes: 37 additions & 0 deletions lib/timespan/mongoid/mongoid_2x.rb
@@ -0,0 +1,37 @@
# Mongoid serialization support for Timespan type.
module Mongoid
module Fields
class Timespan
include Mongoid::Fields::Serializable

def self.instantiate(name, options = {})
super
end

# 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

# 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)
end
{:from => serialize_time(timespan.start_time), :to => serialize_time(timespan.end_time.to_i), :duration => timespan.duration.total }
end
end
end
end
31 changes: 31 additions & 0 deletions lib/timespan/mongoid/mongoid_3x.rb
@@ -0,0 +1,31 @@
# Mongoid serialization support for Timespan type.
module Mongoid
module Fields
class Timespan
# Deserialize a Timespan given the hash stored by Mongodb
#
# @param [Hash] Timespan as hash
# @return [Timespan] deserialized Timespan
def demongoize(value)
return if !value
::Timespan.new :from => from(value), :to => to(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 mongoize(value)
return if value.blank?
timespan = case value
when ::Timespan
value
else
::Timespan.new(value)
end
{:from => serialize_time(timespan.start_time), :to => serialize_time(timespan.end_time.to_i), :duration => timespan.duration.total }
end
end
end
end

0 comments on commit c2ad0cb

Please sign in to comment.