Class: FeatureFlagger::Control

Inherits:
Object
  • Object
show all
Defined in:
lib/feature_flagger/control.rb

Constant Summary collapse

RELEASED_FEATURES =
'released_features'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(storage, notifier, cache_store = nil) ⇒ Control

Returns a new instance of Control.



7
8
9
10
11
# File 'lib/feature_flagger/control.rb', line 7

def initialize(storage, notifier, cache_store = nil)
  @storage = storage
  @notifier = notifier
  @cache_store = cache_store
end

Instance Attribute Details

#storageObject (readonly)

Returns the value of attribute storage.



3
4
5
# File 'lib/feature_flagger/control.rb', line 3

def storage
  @storage
end

Instance Method Details

#cache(name, options, &block) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/feature_flagger/control.rb', line 80

def cache(name, options, &block)
  if @cache_store
    @cache_store.fetch(name, force: options[:skip_cache]) do
      block.call
    end
  else
    block.call
  end
end

#feature_keysObject



76
77
78
# File 'lib/feature_flagger/control.rb', line 76

def feature_keys
  @storage.feature_keys - [FeatureFlagger::Control::RELEASED_FEATURES]
end

#release(feature_key, resource_id) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/feature_flagger/control.rb', line 19

def release(feature_key, resource_id)
  resource_name = Storage::Keys.extract_resource_name_from_feature_key(
    feature_key
  )

  @notifier.send(FeatureFlagger::Notifier::RELEASE, feature_key, resource_id)
  @storage.add(feature_key, resource_name, resource_id)
end

#release_to_all(feature_key) ⇒ Object



34
35
36
37
# File 'lib/feature_flagger/control.rb', line 34

def release_to_all(feature_key)
  @notifier.send(FeatureFlagger::Notifier::RELEASE_TO_ALL, feature_key)
  @storage.add_all(RELEASED_FEATURES, feature_key)
end

#released?(feature_key, resource_id, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
# File 'lib/feature_flagger/control.rb', line 13

def released?(feature_key, resource_id, options = {})
  cache "released/#{feature_key}/#{resource_id}", options do
    @storage.has_value?(RELEASED_FEATURES, feature_key) || @storage.has_value?(feature_key, resource_id)
  end
end

#released_features_to_all(options = {}) ⇒ Object



58
59
60
61
62
# File 'lib/feature_flagger/control.rb', line 58

def released_features_to_all(options = {})
  cache "released_features_to_all/#{RELEASED_FEATURES}", options do
    @storage.all_values(RELEASED_FEATURES)
  end
end

#released_to_all?(feature_key, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
67
68
# File 'lib/feature_flagger/control.rb', line 64

def released_to_all?(feature_key, options = {})
  cache "has_value/#{RELEASED_FEATURES}/#{feature_key}", options do
    @storage.has_value?(RELEASED_FEATURES, feature_key)
  end
end

#releases(resource_name, resource_id, options = {}) ⇒ Object



28
29
30
31
32
# File 'lib/feature_flagger/control.rb', line 28

def releases(resource_name, resource_id, options = {})
  cache "releases/#{resource_name}/#{resource_id}", options do
    @storage.fetch_releases(resource_name, resource_id, RELEASED_FEATURES)
  end
end

#resource_ids(feature_key, options = {}) ⇒ Object



52
53
54
55
56
# File 'lib/feature_flagger/control.rb', line 52

def resource_ids(feature_key, options = {})
  cache "all_values/#{feature_key}", options do
    @storage.all_values(feature_key)
  end
end

#search_keys(query) ⇒ Object

DEPRECATED: this method will be removed from public api on v2.0 version. use instead the feature_keys method.



72
73
74
# File 'lib/feature_flagger/control.rb', line 72

def search_keys(query)
  @storage.search_keys(query)
end

#unrelease(feature_key, resource_id) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/feature_flagger/control.rb', line 39

def unrelease(feature_key, resource_id)
  resource_name = Storage::Keys.extract_resource_name_from_feature_key(
    feature_key
  )
  @notifier.send(FeatureFlagger::Notifier::UNRELEASE, feature_key, resource_id)
  @storage.remove(feature_key, resource_name, resource_id)
end

#unrelease_to_all(feature_key) ⇒ Object



47
48
49
50
# File 'lib/feature_flagger/control.rb', line 47

def unrelease_to_all(feature_key)
  @notifier.send(FeatureFlagger::Notifier::UNRELEASE_TO_ALL, feature_key)
  @storage.remove_all(RELEASED_FEATURES, feature_key)
end