diff --git a/lib/ldclient-rb/config.rb b/lib/ldclient-rb/config.rb index fb1c2170..20ab4008 100644 --- a/lib/ldclient-rb/config.rb +++ b/lib/ldclient-rb/config.rb @@ -399,6 +399,30 @@ def diagnostic_opt_out? # attr_reader :wrapper_version + # + # Returns a copy of this configuration with different wrapper information. + # + # This is intended for use by wrapper libraries, such as the LaunchDarkly OpenFeature provider, which need to + # identify themselves rather than the application which created the configuration. + # + # The copy is shallow: it shares the objects the original configuration references, such as the feature store, + # the logger, and the socket factory. Mutating one of those objects affects both configurations. + # + # @param wrapper_name [String, nil] see {#wrapper_name} + # @param wrapper_version [String, nil] see {#wrapper_version} + # + # @return [LaunchDarkly::Config] + # + def with_wrapper_information(wrapper_name, wrapper_version = nil) + updated = dup + updated.wrapper_name = wrapper_name + updated.wrapper_version = wrapper_version + + updated + end + + protected attr_writer :wrapper_name, :wrapper_version + # # The factory used to construct sockets for HTTP operations. The factory must # provide the method `open(uri, timeout)`. The `open` method must return a diff --git a/spec/config_spec.rb b/spec/config_spec.rb index c3f5939f..687b9b09 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -99,6 +99,34 @@ module LaunchDarkly end end end + describe "with_wrapper_information" do + it "returns a copy with the provided wrapper information" do + config = subject.new(wrapper_name: "original", wrapper_version: "1.0.0", offline: true) + + wrapped = config.with_wrapper_information("wrapper", "2.0.0") + + expect(wrapped.wrapper_name).to eq "wrapper" + expect(wrapped.wrapper_version).to eq "2.0.0" + expect(wrapped.offline?).to eq true + end + + it "does not modify the original configuration" do + config = subject.new(wrapper_name: "original", wrapper_version: "1.0.0") + + config.with_wrapper_information("wrapper", "2.0.0") + + expect(config.wrapper_name).to eq "original" + expect(config.wrapper_version).to eq "1.0.0" + end + + it "defaults the wrapper version to nil" do + config = subject.new(wrapper_name: "original", wrapper_version: "1.0.0") + + wrapped = config.with_wrapper_information("wrapper") + + expect(wrapped.wrapper_version).to be_nil + end + end describe ".omit_anonymous_contexts" do it "defaults to false" do expect(subject.new.omit_anonymous_contexts).to eq false