Permalink
Browse files

Merge pull request #10 from wintersolutions/mongoid-3

Support Mongoid 3
  • Loading branch information...
2 parents f500105 + d6daa57 commit 59c8b9c1bdcbd451091c61c048cd978c3948afe2 @peleteiro committed Mar 4, 2013
Showing with 51 additions and 27 deletions.
  1. +0 −4 .travis.yml
  2. +10 −2 README.md
  3. +39 −19 lib/duration/mongoid.rb
  4. +1 −1 lib/duration/version.rb
  5. +1 −1 ruby-duration.gemspec
View
@@ -1,10 +1,6 @@
language: ruby
rvm:
- - 1.8.7
- - 1.9.2
- 1.9.3
- - jruby-18mode
- jruby-19mode
- ruby-head
- jruby-head
- - ree
View
@@ -16,7 +16,7 @@ Features
* Construtor can receive the amount of time in seconds or a Hash with unit and amount of time.
* Format method to display the time with i18n support.
* Mongoid serialization support. Use `require 'duration/mongoid'`.
- * Tested on mri 1.8.7, ree 1.8.7, mri 1.9.2, jruby and rubinius. Kudos to rvm!
+ * Tested on mri 1.9.3 and jruby. Kudos to rvm!
Show me the code
@@ -37,7 +37,10 @@ Show me the code
Duration.new(:weeks => 1, :days => 20).iso8601 => "P3W6DT0H0M0S"
### Mongoid support
-The current version of this gem supports Mongoid >= [2.1.0](https://github.com/mongoid/mongoid). For lower Mongoid versions try the tag [v1.0.0](https://github.com/peleteiro/ruby-duration/tree/v1.0.0)
+The current version of this gem supports Mongoid >= [3.0.0](https://github.com/mongoid/mongoid).
+For lower Mongoid versions try:
+ * < 3.0.0 tag [v2.1.4](https://github.com/peleteiro/ruby-duration/tree/v2.1.4)
+ * < 2.1.0 tag [v1.0.0](https://github.com/peleteiro/ruby-duration/tree/v1.0.0)
require 'duration/mongoid'
@@ -46,6 +49,11 @@ The current version of this gem supports Mongoid >= [2.1.0](https://github.com/m
field :duration, type => Duration
end
+### Dependencies
+The current version of this gem runs only on Ruby Versions >= 1.9.3.
+If you are running a older version of Ruby try:
+* tag [v2.1.4](https://github.com/peleteiro/ruby-duration/tree/v2.1.4)
+
Note on Patches/Pull Requests
-----------------------------
View
@@ -5,30 +5,50 @@
module Mongoid
module Fields
class Duration
- include Mongoid::Fields::Serializable
- # Deserialize a Duration given the amount of seconds stored by Mongodb
- #
- # @param [Integer, nil] duration in seconds
- # @return [Duration] deserialized Duration
- def deserialize(seconds)
- return if !seconds
+ # Instantiates a new Duration object
+ def initialize(seconds)
::Duration.new(seconds)
end
- # Serialize a Duration or a Hash (with duration units) or a amount of seconds to
- # a BSON serializable type.
- #
- # @param [Duration, Hash, Integer] value
- # @return [Integer] duration in seconds
- def serialize(value)
- return if value.blank?
- if value.is_a?(Hash)
- value.delete_if{|k, v| v.blank? || !::Duration::UNITS.include?(k.to_sym)}
+ # Converts the Duration object into a MongoDB friendly value.
+ def mongoize
+ self.to_i
+ end
+
+ class << self
+ # Deserialize a Duration given the amount of seconds stored by Mongodb
+ #
+ # @param [Integer, nil] duration in seconds
+ # @return [Duration] deserialized Duration
+ def demongoize(seconds)
+ return if !seconds
+ ::Duration.new(seconds)
+ end
+
+ # Serialize a Duration or a Hash (with duration units) or a amount of seconds to
+ # a BSON serializable type.
+ #
+ # @param [Duration, Hash, Integer] value
+ # @return [Integer] duration in seconds
+ def mongoize(value)
return if value.blank?
- ::Duration.new(value).to_i
- elsif value.respond_to?(:to_i)
- value.to_i
+ if value.is_a?(Hash)
+ value.delete_if{|k, v| v.blank? || !::Duration::UNITS.include?(k.to_sym)}
+ return if value.blank?
+ ::Duration.new(value).to_i
+ elsif value.respond_to?(:to_i)
+ value.to_i
+ end
+ end
+
+ # Converts the object that was supplied to a criteria and converts it
+ # into a database friendly form.
+ def evolve(object)
+ case object
+ when ::Duration then object.mongoize
+ else object
+ end
end
end
end
View
@@ -1,3 +1,3 @@
class Duration
- VERSION = "2.1.4"
+ VERSION = "3.0.0"
end
View
@@ -22,7 +22,7 @@ Gem::Specification.new do |s|
s.add_development_dependency "yard", ">= 0"
s.add_development_dependency "rake", ">= 0"
s.add_development_dependency "simplecov", ">= 0.3.5"
- s.add_development_dependency "mongoid", "~> 2.4.0"
+ s.add_development_dependency "mongoid", ">= 3.0.0"
s.files = `git ls-files`.split("\n")
s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact

0 comments on commit 59c8b9c

Please sign in to comment.