From 3485012819e9375a1665c0b32feefa4a565d2a01 Mon Sep 17 00:00:00 2001 From: Pedro Lambert Date: Mon, 16 Oct 2017 14:45:20 -0400 Subject: [PATCH] Add `Configurable#merge_configuration` helper method --- lib/ddtrace/configurable.rb | 32 ++++++++++++++++++-------------- test/configurable_test.rb | 30 ++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 14 deletions(-) diff --git a/lib/ddtrace/configurable.rb b/lib/ddtrace/configurable.rb index caa762f5725..110a9c77f18 100644 --- a/lib/ddtrace/configurable.rb +++ b/lib/ddtrace/configurable.rb @@ -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 @@ -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 diff --git a/test/configurable_test.rb b/test/configurable_test.rb index 19cb54d4595..cd54fd33274 100644 --- a/test/configurable_test.rb +++ b/test/configurable_test.rb @@ -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