Class: Tenax::Configuration

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

Overview

Top-level configuration object. Holds named profiles and global settings. Accessed via Tenax.configure or Tenax.configuration.

Examples:

Tenax.configure do |c|
  c.instrumentation = MyStatsdSink.new
  c.profile :stripe do |p|
    p.max_attempts 5
    p.backoff :exponential, base: 0.2, cap: 5.0
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ Configuration

Returns a new instance of Configuration.



22
23
24
25
# File 'lib/tenax/configuration.rb', line 22

def initialize
  @profiles = {}
  @instrumentation = Instrumentation::Null.new
end

Instance Attribute Details

#instrumentation ⇒ Object

Returns the value of attribute instrumentation.



20
21
22
# File 'lib/tenax/configuration.rb', line 20

def instrumentation
  @instrumentation
end

Instance Method Details

#finalize! ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Freeze the configuration and all profiles within it.



67
68
69
70
71
# File 'lib/tenax/configuration.rb', line 67

def finalize!
  @profiles.each_value(&:finalize!)
  @profiles.freeze
  freeze
end

#profile(name) {|profile| ... } ⇒ Tenax::Profile

Define a new profile or fetch an existing one.

When called with a block, creates (or reopens) a profile and yields it for configuration. When called without a block, fetches an existing profile by name.

Parameters:

  • name (Symbol) —

    the profile name

Yield Parameters:

Returns:

Raises:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/tenax/configuration.rb', line 37

def profile(name, &block)
  unless name.is_a?(Symbol)
    raise InvalidOptionError.new(
      profile_name: nil,
      option: :name,
      value: name,
      expected: "must be a Symbol"
    )
  end

  if block
    raise ConfigurationError, "configuration is frozen; reset! before reconfiguring" if frozen?

    prof = (@profiles[name] ||= Profile.new(name))
    block.call(prof)
    prof
  else
    @profiles.fetch(name) do
      raise UnknownProfileError.new(name, @profiles.keys)
    end
  end
end

#profile_names ⇒ Array<Symbol>

Returns names of all configured profiles.

Returns:

  • (Array<Symbol>) —

    names of all configured profiles



61
62
63
# File 'lib/tenax/configuration.rb', line 61

def profile_names
  @profiles.keys
end