Skip to content
Browse files

Custom finder implementation and documentation

  • Loading branch information...
1 parent 9b32448 commit c4bebb3e22ae0c24a928cad7bc33469ac1289eac @KensoDev committed
View
22 README.md
@@ -130,6 +130,28 @@ Basically, the `ArgsParser` class allows you to keep passing any args you want t
`ArgsParser` also patched `resque-mailer` so you can pass in AR objects to mailers as well.
+## The custom finder
+I found the need to add a custom finder to the args parser.
+
+### Why?
+At Gogobot for example, we use slave databases, those sometimes have lag, so when the finder is executed it returns nil, even though the record is actually on the master.
+
+So, I added support for custom finders.
+
+#### Example:
+
+```ruby
+ class CustomFinder
+ def self.find(klass, id)
+ Octopus.using(:master) {
+ klass.where(id: id).first
+ } unless klass.where(id: id).first
+ end
+ end
+```
+
+So, at Gogobot for example, we will fall back to master if the record was not found on the slave DB.
+
## Contribute / Bug reports
View
9 lib/perform_later/args_parser.rb
@@ -21,7 +21,14 @@ def self.args_from_resque(args)
when CLASS_STRING_FORMAT
$1.constantize
when AR_STRING_FORMAT
- $1.constantize.where(id: $2).first
+ runner_class = $1.constantize
+ id = $2
+
+ if PerformLater::Plugins.finder_class
+ PerformLater::Plugins.finder_class.find(runner_class, id)
+ else
+ runner_class.where(id: id).first
+ end
when YAML_STRING_FORMAT
YAML.load(o)
else
View
4 lib/perform_later/plugins.rb
@@ -9,5 +9,9 @@ def self.add_finder(klass)
@@finder_class = klass
end
end
+
+ def self.clear_finder!
+ @@finder_class = nil
+ end
end
end
View
17 spec/lib/perform_later/args_parser_spec.rb
@@ -1,9 +1,26 @@
require 'spec_helper'
+class CustomFinder
+ def self.find(klass, id)
+ klass.find(id)
+ end
+end
+
describe PerformLater::ArgsParser do
subject { PerformLater::ArgsParser }
let(:user) { User.create }
+ context "Custom finder" do
+ it "should invoke the custom class finder method" do
+ CustomFinder.should_receive(:find).with(User, user.id.to_s)
+ PerformLater::Plugins.add_finder(CustomFinder)
+
+ subject.args_from_resque(["AR:User:#{user.id}"])
+
+ PerformLater::Plugins.clear_finder!
+ end
+ end
+
context "args to resque" do
it "should convert array of hashes correctly" do
View
1 spec/spec_helper.rb
@@ -27,6 +27,7 @@
end
config.after(:each) do
+ PerformLater::Plugins.clear_finder!
$redis.flushdb
end
end

0 comments on commit c4bebb3

Please sign in to comment.
Something went wrong with that request. Please try again.