Class: Take2::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/take2/configuration.rb

Constant Summary collapse

CONFIG_ATTRS =
[:retries,
:retriable,
:retry_proc,
:retry_condition_proc,
:backoff_intervals].freeze

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Configuration

Returns a new instance of Configuration.



15
16
17
18
19
20
21
22
23
24
# File 'lib/take2/configuration.rb', line 15

def initialize(options = {})
  # Defaults
  @retries = 3
  @retriable = []
  @retry_proc = proc {}
  @retry_condition_proc = proc { false }
  @backoff_intervals = Backoff.new(:constant, 3).intervals

  merge_options!(options)
end

Instance Method Details

#[](value) ⇒ Object



32
33
34
# File 'lib/take2/configuration.rb', line 32

def [](value)
  public_send(value)
end

#merge_options!(options = {}) ⇒ Object



36
37
38
39
40
41
# File 'lib/take2/configuration.rb', line 36

def merge_options!(options = {})
  validate!(options).each do |key, value|
    public_send("#{key}=", value)
  end
  self
end

#to_hashObject



26
27
28
29
30
# File 'lib/take2/configuration.rb', line 26

def to_hash
  CONFIG_ATTRS.each_with_object({}) do |key, hash|
    hash[key] = public_send(key)
  end
end

#validate!(options) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/take2/configuration.rb', line 43

def validate!(options)
  options.each do |k, v|
    raise ArgumentError, "#{k} is not a valid configuration" unless CONFIG_ATTRS.include?(k)
    case k
    when :retries
      raise ArgumentError, "#{k} must be positive integer" unless v.is_a?(Integer) && v.positive?
    when :retriable
      raise ArgumentError, "#{k} must be array of retriable errors" unless v.is_a?(Array)
    when :backoff_intervals
      raise ArgumentError, "#{k} must be array of retriable errors" unless v.is_a?(Array)
      raise ArgumentError, "#{k} size must be greater or equal to number of retries" unless v.size >= retries
    when :retry_proc, :retry_condition_proc
      raise ArgumentError, "#{k} must be Proc" unless v.is_a?(Proc)
    end
  end
end