Skip to content

Commit

Permalink
Merge pull request javan#471 from yob/no-activesupport-redux
Browse files Browse the repository at this point in the history
Remove runtime dependency on Active Support
  • Loading branch information
javan committed Jun 30, 2014
2 parents 8456222 + 4eaba3c commit 5b775af
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 8 deletions.
4 changes: 0 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 8 additions & 2 deletions lib/whenever.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
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'
Expand Down
13 changes: 12 additions & 1 deletion lib/whenever/job_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def every(frequency, options = {})
end

def job_type(name, template)
singleton_class.class_eval do
singleton_class_shim.class_eval do
define_method(name) do |task, *args|
options = { :task => task, :template => template }
options.merge!(args[0]) if args[0].is_a? Hash
Expand All @@ -67,6 +67,17 @@ def generate_cron_output

private

# The Object#singleton_class method only became available in MRI 1.9.2, so
# we need this to maintain 1.8 compatibility. Once 1.8 support is dropped,
# this can be removed
def singleton_class_shim
if self.respond_to?(:singleton_clas)
singleton_class
else
class << self; self; end
end
end

#
# Takes a string like: "variable1=something&variable2=somethingelse"
# and breaks it into variable/value pairs. Used for setting variables at runtime from the command line.
Expand Down
51 changes: 51 additions & 0 deletions lib/whenever/numeric_extensions.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 0 additions & 1 deletion whenever.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 5b775af

Please sign in to comment.