Skip to content

Commit

Permalink
General cleanup, add rpsec, add dynamic methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Brendan Loudermilk committed Oct 22, 2011
1 parent 451d842 commit d623b80
Show file tree
Hide file tree
Showing 11 changed files with 183 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .rspec
@@ -0,0 +1,2 @@
--colour
--format <%= ENV["CI"] ? 'progress' : 'documentation'%>
9 changes: 9 additions & 0 deletions .travis.yml
@@ -0,0 +1,9 @@
rvm:
- 1.8.7
- 1.9.2
- 1.9.3
- ruby-head
- jruby
- rbx
- rbx-2.0
- ree
4 changes: 4 additions & 0 deletions Gemfile
@@ -0,0 +1,4 @@
source :rubygems
gemspec

gem "rake"
70 changes: 70 additions & 0 deletions Gemfile.lock
@@ -0,0 +1,70 @@
PATH
remote: .
specs:
acts_as_async (0.1.0)
activerecord
activesupport
resque
resque-scheduler

GEM
remote: http://rubygems.org/
specs:
activemodel (3.1.1)
activesupport (= 3.1.1)
builder (~> 3.0.0)
i18n (~> 0.6)
activerecord (3.1.1)
activemodel (= 3.1.1)
activesupport (= 3.1.1)
arel (~> 2.2.1)
tzinfo (~> 0.3.29)
activesupport (3.1.1)
multi_json (~> 1.0)
arel (2.2.1)
builder (3.0.0)
diff-lcs (1.1.3)
i18n (0.6.0)
multi_json (1.0.3)
rack (1.3.5)
rack-protection (1.1.4)
rack
rake (0.9.2)
redis (2.2.2)
redis-namespace (1.0.3)
redis (< 3.0.0)
resque (1.19.0)
multi_json (~> 1.0)
redis-namespace (~> 1.0.2)
sinatra (>= 0.9.2)
vegas (~> 0.1.2)
resque-scheduler (1.9.9)
redis (>= 2.0.1)
resque (>= 1.8.0)
rufus-scheduler
rspec (2.7.0)
rspec-core (~> 2.7.0)
rspec-expectations (~> 2.7.0)
rspec-mocks (~> 2.7.0)
rspec-core (2.7.1)
rspec-expectations (2.7.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.7.0)
rufus-scheduler (2.0.11)
tzinfo (>= 0.3.23)
sinatra (1.3.1)
rack (~> 1.3, >= 1.3.4)
rack-protection (~> 1.1, >= 1.1.2)
tilt (~> 1.3, >= 1.3.3)
tilt (1.3.3)
tzinfo (0.3.30)
vegas (0.1.8)
rack (>= 1.0.0)

PLATFORMS
ruby

DEPENDENCIES
acts_as_async!
rake
rspec (~> 2.7.0)
21 changes: 21 additions & 0 deletions Rakefile
@@ -0,0 +1,21 @@
require "bundler"
Bundler.setup

require "rake"
require "rspec"
require "rspec/core/rake_task"

$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
require "acts_as_async/version"

RSpec::Core::RakeTask.new("spec")
task :default => :spec

task :build do
system "gem build acts_as_async.gemspec"
end

task :install => :build do
system "sudo gem install acts_as_async-#{ActsAsAsync::VERSION}.gem"
end

Binary file added acts_as_async-0.1.0.gem
Binary file not shown.
34 changes: 16 additions & 18 deletions acts_as_async.gemspec
@@ -1,27 +1,25 @@
$LOAD_PATH.unshift 'lib' $LOAD_PATH.unshift "lib"
require 'acts_as_async/version'


Gem::Specification.new do |s| require "acts_as_async/version"
s.name = "acts_as_async"
s.version = ActsAsAsync::VERSION
s.date = Time.now.strftime('%Y-%m-%d')
s.summary = "Acts As Async is a Resque-backed ActiveRecord delayed job tool"
s.homepage = "http://github.com/bloudermilk/acts_as_async"
s.email = "brendan@gophilosophie.com"
s.authors = [ "Brendan Loudermilk" ]

s.files = %w( README.md LICENSE )
s.files += Dir.glob("lib/**/*")


s.extra_rdoc_files = [ "LICENSE", "README.md" ] Gem::Specification.new do |s|
s.rdoc_options = ["--charset=UTF-8"] s.name = "acts_as_async"
s.version = ActsAsAsync::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ["Brendan Loudermilk"]
s.email = ["brendan@gophilosophie.com"]
s.homepage = "http://github.com/bloudermilk/acts_as_async"
s.summary = "The marriage of ActiveRecord and Resque"
s.description = "ActsAsAsync is an ActiveRecord extension that provides your models with easy-to-use Resque helpers"


# TODO: Find minimum versions # TODO: Find minimum versions
s.add_dependency "resque" s.add_dependency "resque"
s.add_dependency "resque-scheduler" s.add_dependency "resque-scheduler"
s.add_dependency "activesupport"
s.add_dependency "activerecord" s.add_dependency "activerecord"


s.description = <<description s.add_development_dependency "rspec", "~> 2.7.0"
This gem is so crazy you won't believe.
description s.files = Dir.glob("lib/**/*") + %w(LICENSE Rakefile README.md)
s.require_path = "lib"
end end
4 changes: 4 additions & 0 deletions lib/acts_as_async.rb
@@ -1,3 +1,7 @@
require "active_support/concern"

require "active_record"

require "resque" require "resque"
require "resque_scheduler" require "resque_scheduler"


Expand Down
49 changes: 48 additions & 1 deletion lib/acts_as_async/helper.rb
Expand Up @@ -2,6 +2,11 @@ module ActsAsAsync
module Helper module Helper
extend ActiveSupport::Concern extend ActiveSupport::Concern


included do
include SharedMethods
extend SharedMethods
end

module ClassMethods module ClassMethods
# Args will be an optional ID, a method name and optional arguments # Args will be an optional ID, a method name and optional arguments
def perform(*args) def perform(*args)
Expand All @@ -17,7 +22,7 @@ def perform(*args)
method = args.shift method = args.shift
end end


receiver.send(method, *args) receiver.__send__(method, *args)
end end


def async(method, *args) def async(method, *args)
Expand Down Expand Up @@ -46,5 +51,47 @@ def async_in(time, method, *args)
Resque.enqueue_in(time, self.class, id, method, *args) Resque.enqueue_in(time, self.class, id, method, *args)
end end
end end

module SharedMethods
METHOD_REGEXP = /\Aasync_([a-zA-Z]\w*?)(_at|_in)?(!)?\z/

def method_missing(method_id, *args, &block)
if method_id =~ METHOD_REGEXP
# Compose the method to be async'd, with an optional bang
method = "#{$1}#{$3}"

if respond_to? method
# If we're not using the _at or _in methods, just call async
if $2.nil?
__send__(:async, method, *args)

# Otherwise compose the Helper method
else
__send__("async#{$2}", args.shift, method, *args)
end
else
message = "Tried to async the method #{method} but it isn't defined."
raise NoMethodError, message
end
else
super
end
end

# Use respond_to_missing? on newer Ruby versions but fall back to
# overriding respond_to? on older versions. In both cases, return
# true for any match, even if the method to be async'd doesn't
# exist, since technically we do respond to that method but throw
# an exception.
if RUBY_VERSION >= "1.9.2"
def respond_to_missing?(method_id, include_private)
method =~ METHOD_REGEXP || super
end
else
def respond_to?(method_id, include_private)
method =~ METHOD_REGEXP || super
end
end
end
end end
end end
7 changes: 7 additions & 0 deletions spec/acts_as_async/base_extensions_spec.rb
@@ -0,0 +1,7 @@
require "spec_helper"

describe ActsAsAsync::BaseExtensions do
it "should kick ass!" do
ActsAsAsync.should == "kick ass!"
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
@@ -0,0 +1,2 @@
require "rspec"
require "acts_as_async"

0 comments on commit d623b80

Please sign in to comment.