File: configuration.rb

package info (click to toggle)
ruby-retryable 3.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 152 kB
  • sloc: ruby: 512; makefile: 6
file content (66 lines) | stat: -rw-r--r-- 1,446 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
module Retryable
  # Used to set up and modify settings for the retryable.
  class Configuration
    VALID_OPTION_KEYS = [
      :contexts,
      :ensure,
      :exception_cb,
      :log_method,
      :matching,
      :not,
      :on,
      :sleep,
      :sleep_method,
      :tries
    ].freeze

    attr_accessor(*VALID_OPTION_KEYS)

    attr_accessor :enabled

    def initialize
      @contexts     = {}
      @ensure       = proc {}
      @exception_cb = proc {}
      @log_method   = proc {}
      @matching     = /.*/
      @not          = []
      @on           = StandardError
      @sleep        = 1
      @sleep_method = ->(seconds) { Kernel.sleep(seconds) }
      @tries        = 2

      @enabled      = true
    end

    def enable
      @enabled = true
    end
    alias enabled? enabled

    def disable
      @enabled = false
    end

    # Allows config options to be read like a hash
    #
    # @param [Symbol] option Key for a given attribute
    def [](option)
      send(option)
    end

    # Returns a hash of all configurable options
    def to_hash
      VALID_OPTION_KEYS.each_with_object({}) do |key, memo|
        memo[key] = instance_variable_get("@#{key}")
      end
    end

    # Returns a hash of all configurable options merged with +hash+
    #
    # @param [Hash] hash A set of configuration options that will take precedence over the defaults
    def merge(hash)
      to_hash.merge(hash)
    end
  end
end