Class: Summoner::Fetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/summoner/fetcher.rb

Class Method Summary collapse

Class Method Details

.evaluate_match(allowed_values, attribute, entity) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/summoner/fetcher.rb', line 45

def self.evaluate_match(allowed_values, attribute, entity)
  if allowed_values.is_a?(Array) && allowed_values.include?('*')
    return true
  elsif allowed_values == '*'
    return true
  end

  unless entity.respond_to?(attribute)
    Rails.logger.warn "[Summoner] Entity #{entity.class.name} does not respond to #{attribute}. Cannot evaluate match for feature #{feature.key}."
    return false
  end

  entity_value = entity.public_send(attribute)

  if allowed_values.is_a?(Array)
    allowed_values.include?(entity_value)
  else
    allowed_values == entity_value
  end
end

.fetch_from_db(feature_key, entity) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/summoner/fetcher.rb', line 19

def self.fetch_from_db(feature_key, entity)
  feature = Summoner::Feature.find_by(key: feature_key)
  return nil unless feature

  raw_value = resolve_raw_value(feature, entity)      

  if feature.match_attribute.present? && entity.present?
    return evaluate_match(raw_value, feature.match_attribute, entity)
  end

  raw_value
end

.resolve_raw_value(feature, entity) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/summoner/fetcher.rb', line 32

def self.resolve_raw_value(feature, entity)
  if entity.present?
    override = Summoner::FeatureOverride.find_by(
      feature_key: feature.key,
      flaggable: entity
    )

    return override.value if override.present?
  end

  feature.default_value
end

.value_for(feature_key, entity: nil) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/summoner/fetcher.rb', line 3

def self.value_for(feature_key, entity: nil)
  config = Summoner.configuration

  return fetch_from_db(feature_key, entity) unless config.cache_enabled

  cache_key = if entity
    "#{config.cache_namespace}:#{feature_key}:#{entity.class.name.underscore}:#{entity.id}"
  else
    "#{config.cache_namespace}:#{feature_key}"
  end

  Rails.cache.fetch(cache_key, expires_in: config.cache_expires_in) do
    fetch_from_db(feature_key, entity)
  end
end