Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Support Mongoid 3 #10

Merged
merged 3 commits into from

5 participants

@wintersolutions

Support for mongoid >= 3 added. Main work was done by xiy I only bumped the version (needed b/c its not compatible with mongoid 2 anymore) and added documentation.

xiy and others added some commits
@gcirne
Collaborator

Hi,

Before I merge this could you please take a look at the failing travis build? Apparently mongoid >= 3.0.0 only supports ruby >= 1.9.3 so maybe we should remove ruby versions < 1.9.3 from the travis build. What do you think?

@wintersolutions

Hi,

I hadn't seen that, I'll make the changes to the travis build and write a short notice in the documentation about suported Ruby versions.

@gcirne
Collaborator
@wintersolutions

Travis builds passed.

@peleteiro peleteiro merged commit 59c8b9c into peleteiro:master

1 check passed

Details default The Travis build passed
@kenn

When is this going to be released?

@peleteiro
Owner

Done. Sorry for the delay.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 30, 2012
  1. @xiy
Commits on Mar 1, 2013
  1. @wintersolutions

    Bumped Version to 3.0.0

    wintersolutions authored
    Updated Documentation
    Changed 'mongoid' gem version to 3
Commits on Mar 4, 2013
  1. @wintersolutions
This page is out of date. Refresh to see the latest.
View
4 .travis.yml
@@ -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
12 README.md
@@ -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
58 lib/duration/mongoid.rb
@@ -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
2  lib/duration/version.rb
@@ -1,3 +1,3 @@
class Duration
- VERSION = "2.1.4"
+ VERSION = "3.0.0"
end
View
2  ruby-duration.gemspec
@@ -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
Something went wrong with that request. Please try again.