Skip to content

Commit

Permalink
refactor: changed client metaprogramming to send with real methods
Browse files Browse the repository at this point in the history
Signed-off-by: Max VelDink <[email protected]>
  • Loading branch information
maxveldink committed Oct 2, 2024
1 parent 3ca368d commit aa098db
Showing 1 changed file with 57 additions and 17 deletions.
74 changes: 57 additions & 17 deletions lib/open_feature/sdk/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:)

Check warning on line 44 in lib/open_feature/sdk/client.rb

View check run for this annotation

Codecov / codecov/patch

lib/open_feature/sdk/client.rb#L44

Added line #L44 was not covered by tests
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:)

Check warning on line 52 in lib/open_feature/sdk/client.rb

View check run for this annotation

Codecov / codecov/patch

lib/open_feature/sdk/client.rb#L52

Added line #L52 was not covered by tests
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
Expand Down

0 comments on commit aa098db

Please sign in to comment.