Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Bollinger committed May 1, 2015
1 parent 4dabf57 commit 1cab43f
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions lib/pester.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,26 @@
require 'pester/version'

module Pester
def self.configure(&block)
extend self
attr_accessor :environments

def configure(&block)
Config.configure(&block)
unless Config.environments.nil?
self.environments = Hash[Config.environments.select { |_, e| e.is_a?(Hash) }.map { |k, e| [k.to_sym, Environment.new(e)] }]
valid_environments = Config.environments.select { |_, e| e.is_a?(Hash) }
@environments = Hash[valid_environments.map { |k, e| [k.to_sym, Environment.new(e)] }]
end
end

def self.retry(options = {}, &block)
def retry_constant(options = {}, &block)
retry_action(options.merge(on_retry: Behaviors::Sleep::Constant), &block)
end

def self.retry_with_backoff(options = {}, &block)
def retry_with_backoff(options = {}, &block)
retry_action(options.merge(on_retry: Behaviors::Sleep::Linear), &block)
end

def self.retry_with_exponential_backoff(options = {}, &block)
def retry_with_exponential_backoff(options = {}, &block)
retry_action({ delay_interval: 1 }.merge(options).merge(on_retry: Behaviors::Sleep::Exponential), &block)
end

Expand Down Expand Up @@ -49,7 +53,7 @@ def self.retry_with_exponential_backoff(options = {}, &block)
# FileUtils.rm_r(directory)
# end

def self.retry_action(opts = {}, &block)
def retry_action(opts = {}, &block)
merge_defaults(opts)
if opts[:retry_error_classes] && opts[:reraise_error_classes]
fail 'You can only have one of retry_error_classes or reraise_error_classes'
Expand Down Expand Up @@ -79,25 +83,21 @@ def self.retry_action(opts = {}, &block)
nil
end

def respond_to?(method_sym)
def respond_to?(method_sym, options = {}, &block)
super || Config.environments.key?(method_sym)
end

def method_missing(method_sym)
if Config.environments.key?(method_sym)
Config.environments[method_sym]
def method_missing(method_sym, options = {}, &block)
if @environments.key?(method_sym)
@environments[method_sym]
else
super
end
end

This comment has been minimized.

Copy link
@slpsys

slpsys May 1, 2015

Contributor

This is what actually fixes the issue. The access was wrong, plus it was not a class method, so...the docs weren't correct.

ad0c35f is the spec that was missing that would have caught this 😬


class << self
attr_accessor :environments
end

private

def self.should_retry?(e, opts = {})
def should_retry?(e, opts = {})
retry_error_classes = opts[:retry_error_classes]
retry_error_messages = opts[:retry_error_messages]
reraise_error_classes = opts[:reraise_error_classes]
Expand All @@ -117,7 +117,7 @@ def self.should_retry?(e, opts = {})
end
end

def self.merge_defaults(opts)
def merge_defaults(opts)
opts[:retry_error_classes] = opts[:retry_error_classes] ? Array(opts[:retry_error_classes]) : nil
opts[:retry_error_messages] = opts[:retry_error_messages] ? Array(opts[:retry_error_messages]) : nil
opts[:reraise_error_classes] = opts[:reraise_error_classes] ? Array(opts[:reraise_error_classes]) : nil
Expand All @@ -127,4 +127,6 @@ def self.merge_defaults(opts)
opts[:on_max_attempts_exceeded] ||= Behaviors::WarnAndReraise
opts[:logger] ||= Config.logger
end

alias_method :retry, :retry_action
end

0 comments on commit 1cab43f

Please sign in to comment.