Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Retriable #4

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/cool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

# The list of classes in Cool module
module Cool
autoload :Retriable, 'cool/retriable'
end
22 changes: 22 additions & 0 deletions lib/cool/retriable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Cool
# Retriable
module Retriable
def retry_(new_method, on: StandardError, times: 3, &condition) # rubocop:disable Metrics/MethodLength, Metrics/LineLength
condition ||= ->(*) { false }
orig_method = "__retry_orig_#{new_method}".to_sym
alias_method orig_method, new_method
define_method(new_method) do |*args, &block|
(times - 1).times do
retval =
begin
method(orig_method).call(*args, &block)
rescue on
next
end
return retval unless condition.call(retval)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use yield instead of condition.call.

end
return method(orig_method).call(*args, &block)
end
end
end
end
138 changes: 138 additions & 0 deletions spec/lib/cool/retriable_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
RSpec.describe Cool::Retriable do
describe '#retry_' do
it 'works' do
retriable = described_class
klass = Class.new do
extend retriable

ENUM = [:no, :no, :yes].to_enum
def do_it
ENUM.next
end
retry_(:do_it) { |retval| retval == :no }
end

expect(klass.new.do_it).to eq(:yes)
end

it 'works' do
retriable = described_class
klass = Class.new do
extend retriable

ENUM = [:no, :no, :no].to_enum
def do_it
ENUM.next
end
retry_(:do_it) { |retval| retval == :no }
end

expect(klass.new.do_it).to eq(:no)
end

it 'works' do
retriable = described_class
klass = Class.new do
extend retriable

ENUM = [nil, nil, :yes].to_enum
def do_it
ENUM.next
end
retry_(:do_it, &:nil?)
end

expect(klass.new.do_it).to eq(:yes)
end

it 'works' do
retriable = described_class
klass = Class.new do
extend retriable

ENUM = [[], {}, :yes].to_enum
def do_it
ENUM.next
end
retry_(:do_it, &:empty?)
end

expect(klass.new.do_it).to eq(:yes)
end

it 'works' do
retriable = described_class
klass = Class.new do
extend retriable

ENUM = [:no, :no, :yes].to_enum
def do_it
ENUM.next.tap { |value| raise if value == :no }
end
retry_(:do_it)
end

expect(klass.new.do_it).to eq(:yes)
end

it 'works' do
retriable = described_class
klass = Class.new do
extend retriable

ENUM = [:no, :no, :no].to_enum
def do_it
ENUM.next.tap { |value| raise if value == :no }
end
retry_(:do_it)
end

expect { klass.new.do_it }.to raise_error(StandardError)
end

it 'works' do
retriable = described_class
klass = Class.new do
extend retriable

ENUM = [:no].to_enum
def do_it
ENUM.next.tap { |value| raise if value == :no }
end
retry_(:do_it, times: 1)
end

expect { klass.new.do_it }.to raise_error(StandardError)
end

it 'works' do
retriable = described_class
klass = Class.new do
extend retriable

ENUM = [:error, nil, :yes].to_enum
def do_it
ENUM.next.tap { |value| raise if value == :error }
end
retry_(:do_it, &:nil?)
end

expect(klass.new.do_it).to eq(:yes)
end

it 'works' do
retriable = described_class
klass = Class.new do
extend retriable

ENUM = [:yes, :error].to_enum
def do_it
ENUM.next.tap { |value| raise if value == :error }
end
retry_(:do_it)
end

expect(klass.new.do_it).to eq(:yes)
end
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,5 @@
# as the one that triggered the failure.
Kernel.srand config.seed
end

require 'cool'