Class: Take2::Configuration
- Inherits:
-
Object
- Object
- Take2::Configuration
- Defined in:
- lib/take2/configuration.rb
Constant Summary collapse
- CONFIG_ATTRS =
[:retries, :retriable, :retry_proc, :retry_condition_proc, :time_to_sleep].freeze
Instance Method Summary collapse
- #[](value) ⇒ Object
-
#initialize(options = {}) ⇒ Configuration
constructor
A new instance of Configuration.
- #setter ⇒ Object
- #to_hash ⇒ Object
- #validate_options(options, &setter) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Configuration
Returns a new instance of Configuration.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/take2/configuration.rb', line 6 def initialize( = {}) # Defaults @retries = 3 @retriable = [ Net::HTTPServerException, Net::HTTPRetriableError, Errno::ECONNRESET, IOError, ].freeze @retry_proc = proc {} @retry_condition_proc = proc { false } @time_to_sleep = 3 # Overwriting the defaults (, &setter) end |
Instance Method Details
#[](value) ⇒ Object
28 29 30 |
# File 'lib/take2/configuration.rb', line 28 def [](value) self.public_send(value) end |
#setter ⇒ Object
49 50 51 |
# File 'lib/take2/configuration.rb', line 49 def setter proc { |key, value| instance_variable_set(:"@#{key}", value) } end |
#to_hash ⇒ Object
22 23 24 25 26 |
# File 'lib/take2/configuration.rb', line 22 def to_hash CONFIG_ATTRS.each_with_object({}) do |key, hash| hash[key] = public_send(key) end end |
#validate_options(options, &setter) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/take2/configuration.rb', line 32 def (, &setter) .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 :time_to_sleep raise ArgumentError, "#{k} must be positive number" unless (v.is_a?(Integer) || v.is_a?(Float)) && v >= 0 when :retriable raise ArgumentError, "#{k} must be array of retriable errors" unless v.is_a?(Array) when :retry_proc, :retry_condition_proc raise ArgumentError, "#{k} must be Proc" unless v.is_a?(Proc) end setter.call(k, v) if block_given? end end |