Class: FeatureHub::Sdk::Impl::ApplyFeature

Inherits:
Object
  • Object
show all
Defined in:
lib/feature_hub/sdk/impl/apply_features.rb

Overview

full mechanism for applying client side evaluation

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(percent_calculator = nil, matcher_repository = nil) ⇒ ApplyFeature

Returns a new instance of ApplyFeature.



20
21
22
23
# File 'lib/feature_hub/sdk/impl/apply_features.rb', line 20

def initialize(percent_calculator = nil, matcher_repository = nil)
  @percentage_calculator = percent_calculator || FeatureHub::Sdk::Impl::Murmur3PercentageCalculator.new
  @matcher_repository = matcher_repository || FeatureHub::Sdk::Impl::MatcherRegistry.new
end

Class Method Details

.determine_percentage_key(context, rsi) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/feature_hub/sdk/impl/apply_features.rb', line 99

def self.determine_percentage_key(context, rsi)
  if rsi.percentage_attributes?
    rsi.percentage_attributes.map { |attr| context.get_attr(attr, "<none>") }.join("$")
  else
    context.default_percentage_key
  end
end

Instance Method Details

#apply(strategies, _key, feature_value_id, context) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/feature_hub/sdk/impl/apply_features.rb', line 25

def apply(strategies, _key, feature_value_id, context)
  return Applied.new(false, nil) if context.nil? || strategies.nil? || strategies.empty?

  percentage = nil
  percentage_key = nil
  base_percentage = {}
  default_percentage_key = context.default_percentage_key

  strategies.each do |rsi|
    if (rsi.percentage != 0) && (!default_percentage_key.nil? || rsi.percentage_attributes?)
      new_percentage_key = ApplyFeature.determine_percentage_key(context, rsi)

      base_percentage[new_percentage_key] = 0 if base_percentage[new_percentage_key].nil?

      base_percentage_val = base_percentage[new_percentage_key]

      if percentage.nil? || new_percentage_key != percentage_key
        percentage_key = new_percentage_key
        percentage = @percentage_calculator.determine_client_percentage(percentage_key, feature_value_id)
        use_base_percentage = rsi.attributes? ? 0 : base_percentage_val

        # rubocop:disable Layout/MultilineOperationIndentation
        if percentage <= (use_base_percentage + rsi.percentage) &&
          (!rsi.attributes? || (rsi.attributes? && match_attribute(context, rsi)))
          return Applied.new(true, rsi.value)
        end

        # rubocop:enable Layout/MultilineOperationIndentation

        unless rsi.attributes?
          base_percentage[percentage_key] =
            base_percentage[percentage_key] + rsi.percentage
        end
      end
    end

    return Applied.new(true, rsi.value) if rsi.percentage.zero? && rsi.attributes? && match_attribute(context,
                                                                                                      rsi)
  end

  Applied.new(false, nil)
end

#match_attribute(context, rs_attr) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/feature_hub/sdk/impl/apply_features.rb', line 68

def match_attribute(context, rs_attr)
  rs_attr.attributes.each do |attr|
    supplied_values = context.get_attr(attr.field_name)
    if supplied_values.empty? && attr.field_name.downcase == "now"
      case attr.field_type
      when "DATE"
        supplied_values = [Time.new.utc.iso8601[0..9]]
      when "DATETIME"
        supplied_values = [Time.new.utc.iso8601]
      end
    end

    if attr.values.nil? && supplied_values.empty?
      return false unless attr.conditional.equals?

      next
    end

    return false if attr.values.nil? || supplied_values.empty?

    # this attribute has to match or we failed
    match = supplied_values.any? do |supplied_value|
      @matcher_repository.find_matcher(attr).match(supplied_value, attr)
    end

    return false unless match
  end

  true
end