Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
94df156
commit c2ad0cb
Showing
3 changed files
with
82 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |