From aa098dbfff50d85bd923f5af5e0264b305857c26 Mon Sep 17 00:00:00 2001 From: Max VelDink Date: Tue, 1 Oct 2024 20:49:03 -0400 Subject: [PATCH] refactor: changed client metaprogramming to send with real methods Signed-off-by: Max VelDink --- lib/open_feature/sdk/client.rb | 74 ++++++++++++++++++++++++++-------- 1 file changed, 57 insertions(+), 17 deletions(-) diff --git a/lib/open_feature/sdk/client.rb b/lib/open_feature/sdk/client.rb index 8f3749d..bb14824 100644 --- a/lib/open_feature/sdk/client.rb +++ b/lib/open_feature/sdk/client.rb @@ -5,9 +5,6 @@ module SDK # TODO: Write documentation # class Client - RESULT_TYPE = %i[boolean string number integer float object].freeze - SUFFIXES = %i[value details].freeze - attr_reader :metadata, :evaluation_context attr_accessor :hooks @@ -19,20 +16,63 @@ def initialize(provider:, domain: nil, evaluation_context: nil) @hooks = [] end - RESULT_TYPE.each do |result_type| - SUFFIXES.each do |suffix| - class_eval <<-RUBY, __FILE__, __LINE__ + 1 - # def fetch_boolean_details(flag_key:, default_value:, evaluation_context: nil) - # result = @provider.fetch_boolean_value(flag_key: flag_key, default_value: default_value, evaluation_context: evaluation_context) - # end - def fetch_#{result_type}_#{suffix}(flag_key:, default_value:, evaluation_context: nil) - built_context = EvaluationContextBuilder.new.call(api_context: OpenFeature::SDK.evaluation_context, client_context: self.evaluation_context, invocation_context: evaluation_context) - resolution_details = @provider.fetch_#{result_type}_value(flag_key:, default_value:, evaluation_context: built_context) - evaluation_details = EvaluationDetails.new(flag_key:, resolution_details:) - #{"evaluation_details.value" if suffix == :value} - end - RUBY - end + def fetch_boolean_details(flag_key:, default_value:, evaluation_context: nil) + fetch_details(type: :boolean, flag_key:, default_value:, evaluation_context:) + end + + def fetch_boolean_value(flag_key:, default_value:, evaluation_context: nil) + fetch_details(type: :boolean, flag_key:, default_value:, evaluation_context:).value + end + + def fetch_string_details(flag_key:, default_value:, evaluation_context: nil) + fetch_details(type: :string, flag_key:, default_value:, evaluation_context:) + end + + def fetch_string_value(flag_key:, default_value:, evaluation_context: nil) + fetch_details(type: :string, flag_key:, default_value:, evaluation_context:).value + end + + def fetch_number_details(flag_key:, default_value:, evaluation_context: nil) + fetch_details(type: :number, flag_key:, default_value:, evaluation_context:) + end + + def fetch_number_value(flag_key:, default_value:, evaluation_context: nil) + fetch_details(type: :number, flag_key:, default_value:, evaluation_context:).value + end + + def fetch_integer_details(flag_key:, default_value:, evaluation_context: nil) + fetch_details(type: :integer, flag_key:, default_value:, evaluation_context:) + end + + def fetch_integer_value(flag_key:, default_value:, evaluation_context: nil) + fetch_details(type: :integer, flag_key:, default_value:, evaluation_context:).value + end + + def fetch_float_details(flag_key:, default_value:, evaluation_context: nil) + fetch_details(type: :float, flag_key:, default_value:, evaluation_context:) + end + + def fetch_float_value(flag_key:, default_value:, evaluation_context: nil) + fetch_details(type: :float, flag_key:, default_value:, evaluation_context:).value + end + + def fetch_object_details(flag_key:, default_value:, evaluation_context: nil) + fetch_details(type: :object, flag_key:, default_value:, evaluation_context:) + end + + def fetch_object_value(flag_key:, default_value:, evaluation_context: nil) + fetch_details(type: :object, flag_key:, default_value:, evaluation_context:).value + end + + private + + def fetch_details(type:, flag_key:, default_value:, evaluation_context: nil) + built_context = EvaluationContextBuilder.new.call(api_context: OpenFeature::SDK.evaluation_context, client_context: self.evaluation_context, invocation_context: evaluation_context) + + EvaluationDetails.new( + flag_key:, + resolution_details: @provider.send(:"fetch_#{type}_value", flag_key:, default_value:, evaluation_context: built_context) + ) end end end