Class: SmoothChange::ClientFeatureToggler

Inherits:
Object
  • Object
show all
Defined in:
lib/smooth_change/client_feature_toggler.rb

Overview

Store client features requirements

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values = {}) ⇒ ClientFeatureToggler

Returns a new instance of ClientFeatureToggler.



6
7
8
# File 'lib/smooth_change/client_feature_toggler.rb', line 6

def initialize(values = {})
  @values = values.transform_keys(&:to_sym)
end

Class Method Details

.from_http_header(http_header_value) ⇒ Object

Http header is expected to be a comma separated list like :

"feature1, !feature2, feature3"

Spaces between elements are ignored A leading “!” means the feature request is opt-out



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/smooth_change/client_feature_toggler.rb', line 30

def self.from_http_header(http_header_value)
  values = http_header_value
           .split(",")
           .each_with_object({}) do |item, memo|
    feature_name = item.strip

    # NOTE: delete_prefix! returns nil if prefix is not found
    enabled = !feature_name.delete_prefix!("!")
    memo[feature_name] = enabled
  end
  new(values)
end

Instance Method Details

#enabled?(feature_name) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
# File 'lib/smooth_change/client_feature_toggler.rb', line 21

def enabled?(feature_name)
  feature = find_feature(feature_name)
  feature.enabled_with?(self)
end

#value_for(feature) ⇒ Object

if true : feature is wanted if false : feature is explicitly not wanted if nil : not specified



17
18
19
# File 'lib/smooth_change/client_feature_toggler.rb', line 17

def value_for(feature)
  values[feature.to_sym]
end

#valuesObject



10
11
12
# File 'lib/smooth_change/client_feature_toggler.rb', line 10

def values
  @values ||= {}
end