Skip to content

Commit

Permalink
Add Configurable#merge_configuration helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
p-lambert committed Oct 16, 2017
1 parent 3797a4e commit 3485012
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 14 deletions.
32 changes: 18 additions & 14 deletions lib/ddtrace/configurable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,32 @@ def self.included(base)
base.singleton_class.send(:include, ClassMethods)
end

def merge_configuration(options, defaults = self.class)
defaults.to_h.merge(options)
end

# ClassMethods
module ClassMethods
def set_option(name, value)
assert_valid!(name)
__assert_valid!(name)

options[name][:value] = options[name][:setter].call(value)
__options[name][:value] = __options[name][:setter].call(value)
end

def get_option(name)
assert_valid!(name)
__assert_valid!(name)

options[name][:value] || options[name][:default]
__options[name][:value] || __options[name][:default]
end

def to_h
options.each_with_object({}) do |(key, meta), hash|
hash[key] = meta[:value]
__options.each_with_object({}) do |(key, _), hash|
hash[key] = get_option(key)
end
end

def reset_options!
options.each do |name, meta|
__options.each do |name, meta|
set_option(name, meta[:default])
end
end
Expand All @@ -39,19 +43,19 @@ def reset_options!
def option(name, meta = {})
name = name.to_sym
meta[:setter] ||= IDENTITY
options[name] = meta
__options[name] = meta
end

def options
@options ||= {}
def __options
@__options ||= {}
end

def assert_valid!(name)
return if options.key?(name)
raise(InvalidOptionError, "#{pretty_name} doesn't have the option: #{name}")
def __assert_valid!(name)
return if __options.key?(name)
raise(InvalidOptionError, "#{__pretty_name} doesn't have the option: #{name}")
end

def pretty_name
def __pretty_name
entry = Datadog.registry.find { |el| el.klass == self }

return entry.name if entry
Expand Down
30 changes: 30 additions & 0 deletions test/configurable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,35 @@ def test_invalid_option
@module.get_option(:bad_option)
end
end

def test_merge_configuration
klass = Class.new do
include Configurable
option :x, default: :default_x
option :y, default: :default_y

attr_reader :options

def initialize(options = {})
@options = merge_configuration(options)
end
end

instance = klass.new(x: :custom_x, z: :custom_z)

assert_equal(:custom_x, instance.options[:x])
assert_equal(:default_y, instance.options[:y])
assert_equal(:custom_z, instance.options[:z])
end

def test_to_h
@module.class_eval do
option :x, default: 1
option :y, default: 2
end

@module.set_option(:y, 100)
assert_equal({ x: 1, y: 100 }, @module.to_h)
end
end
end

0 comments on commit 3485012

Please sign in to comment.