Permalink
Browse files

Load custom time extensions for Numeric when activesupport isn't avai…

…lable

* Removes activesupport as a hard runtime dependency
* If activesupport is available in the current environment then use that
  for maximum compatibility and to avoid conflicting monkey patches
* If activesupport isn't available, then our version provides all the
  functionality required for specifying cron schedules
  • Loading branch information...
1 parent 8456222 commit a65c1ae9b4b23a7a7a49fb0da5dbe3390edab0d1 @yob yob committed Jun 16, 2014
Showing with 60 additions and 6 deletions.
  1. +0 −4 Gemfile
  2. +9 −1 lib/whenever.rb
  3. +51 −0 lib/whenever/numeric_extensions.rb
  4. +0 −1 whenever.gemspec
View
@@ -2,7 +2,3 @@ source "http://rubygems.org"
# Specify your gem's dependencies in whenever.gemspec
gemspec
-
-if RUBY_VERSION < "1.9.3"
- gem "activesupport", "< 4.0.0"
-end
View
@@ -1,5 +1,13 @@
require 'thread'
-require 'active_support/all'
+
+# Although whenever doesn't require activesupport, we prefer to use their Numeric
+# extensions if they're available. If activesupport isn't available, load our own
+# minimal version of the extensions.
+begin
+ require 'active_support/core_ext/numeric/time'
+rescue LoadError
+ require 'whenever/numeric_extensions'
+end
module Whenever
autoload :JobList, 'whenever/job_list'
@@ -0,0 +1,51 @@
+# These are a minimal version of the active support Numeric extensions, for
+# environments where activesupport isn't available.
+#
+# They don't have all the features of the activesupport extensions (for example:
+# the returned values are just Fixnums, so you can't do things like 1.week.from_now),
+# but they do cover the use cases needed for describing cron schedules.
+#
+class Numeric
+ # Return the number of seconds represented by the current number
+ def seconds
+ self.to_i
+ end
+ alias :second :seconds
+
+ # Return the number of minutes represented by the current number
+ def minutes
+ self.to_i * 60
+ end
+ alias :minute :minutes
+
+ # Return the number of hours represented by the current number
+ def hours
+ self.to_i * 3_600
+ end
+ alias :hour :hours
+
+ # Return the number of days represented by the current number
+ def days
+ self.to_i * 86_400
+ end
+ alias :day :days
+
+ # Return the number of weeks represented by the current number
+ def weeks
+ self.to_i * 604_800
+ end
+ alias :week :weeks
+
+ # Return the number of months represented by the current number
+ def months
+ self.to_i * 2_592_000
+ end
+ alias :month :months
+
+ # Return the number of years represented by the current number
+ def years
+ self.to_i * 31_557_600
+ end
+ alias :year :years
+
+end
View
@@ -18,7 +18,6 @@ Gem::Specification.new do |s|
s.require_paths = ["lib"]
s.add_dependency "chronic", ">= 0.6.3"
- s.add_dependency "activesupport", ">= 2.3.4"
s.add_development_dependency "mocha", ">= 0.9.5"
s.add_development_dependency "rake"

0 comments on commit a65c1ae

Please sign in to comment.