class ElasticAPM::Config::Options::Option

@api private

Attributes

converter[R]
default[R]
key[R]
type[R]
value[RW]

Public Class Methods

new( key, value: nil, type: :string, default: nil, converter: nil ) click to toggle source
# File lib/elastic_apm/config/options.rb, line 26
def initialize(
  key,
  value: nil,
  type: :string,
  default: nil,
  converter: nil
)
  @key = key
  @type = type
  @default = default
  @converter = converter

  set(value || default)
end

Public Instance Methods

env_key() click to toggle source
# File lib/elastic_apm/config/options.rb, line 48
def env_key
  "ELASTIC_APM_#{key.upcase}"
end
set(value) click to toggle source
# File lib/elastic_apm/config/options.rb, line 44
def set(value)
  @value = normalize(value)
end

Private Instance Methods

normalize(val) click to toggle source

rubocop:disable Metrics/CyclomaticComplexity

# File lib/elastic_apm/config/options.rb, line 55
def normalize(val)
  return if val.nil?

  if @converter
    return @converter.call(val)
  end

  case type
  when :string then val.to_s
  when :int then val.to_i
  when :float then val.to_f
  when :bool then normalize_bool(val)
  when :list then normalize_list(val)
  when :dict then normalize_dict(val)
  when :url then normalize_url(val)
  else
    # raise "Unknown options type '#{type.inspect}'"
    val
  end
end
normalize_bool(val) click to toggle source

rubocop:enable Metrics/CyclomaticComplexity

# File lib/elastic_apm/config/options.rb, line 77
def normalize_bool(val)
  return val unless val.is_a?(String)
  !%w[0 false].include?(val.strip.downcase)
end
normalize_dict(val) click to toggle source
# File lib/elastic_apm/config/options.rb, line 87
def normalize_dict(val)
  return val unless val.is_a?(String)
  Hash[val.split(/[&,]/).map { |kv| kv.split('=') }]
end
normalize_list(val) click to toggle source
# File lib/elastic_apm/config/options.rb, line 82
def normalize_list(val)
  return Array(val) unless val.is_a?(String)
  val.split(/[ ,]/)
end
normalize_url(val) click to toggle source
# File lib/elastic_apm/config/options.rb, line 92
def normalize_url(val)
  val = val.to_s
  val.end_with?('/') ? val.chomp('/') : val
end