Class: FeatureHub::Sdk::FeatureHubRepository
- Inherits:
-
InternalFeatureRepository
- Object
- InternalFeatureRepository
- FeatureHub::Sdk::FeatureHubRepository
- Defined in:
- lib/feature_hub/sdk/feature_repository.rb
Overview
the core implementation of a feature repository
Instance Attribute Summary collapse
-
#features ⇒ Object
readonly
Returns the value of attribute features.
Instance Method Summary collapse
- #apply(strategies, key, feature_id, context) ⇒ Object
- #close ⇒ Object
- #extract_feature_state ⇒ Object
- #feature(key, attrs = nil) ⇒ Object
- #find_interceptor(feature_key, feature_state = nil) ⇒ Object
-
#initialize(apply_features = nil, logger = nil) ⇒ FeatureHubRepository
constructor
A new instance of FeatureHubRepository.
- #not_ready! ⇒ Object
- #notify(status, data, source = "unknown") ⇒ Object
- #ready? ⇒ Boolean
- #register_interceptor(interceptor) ⇒ Object
- #register_raw_update_listener(listener) ⇒ Object
- #value(key, default_value = nil, attrs = nil) ⇒ Object
Constructor Details
#initialize(apply_features = nil, logger = nil) ⇒ FeatureHubRepository
Returns a new instance of FeatureHubRepository.
11 12 13 14 15 16 17 18 19 |
# File 'lib/feature_hub/sdk/feature_repository.rb', line 11 def initialize(apply_features = nil, logger = nil) super() @strategy_matcher = apply_features || FeatureHub::Sdk::Impl::ApplyFeature.new @interceptors = [] @raw_listeners = [] @features = {} @ready = false @logger = logger end |
Instance Attribute Details
#features ⇒ Object (readonly)
Returns the value of attribute features.
9 10 11 |
# File 'lib/feature_hub/sdk/feature_repository.rb', line 9 def features @features end |
Instance Method Details
#apply(strategies, key, feature_id, context) ⇒ Object
21 22 23 |
# File 'lib/feature_hub/sdk/feature_repository.rb', line 21 def apply(strategies, key, feature_id, context) @strategy_matcher.apply(strategies, key, feature_id, context) end |
#close ⇒ Object
87 88 89 90 |
# File 'lib/feature_hub/sdk/feature_repository.rb', line 87 def close @interceptors.each(&:close) @raw_listeners.each(&:close) end |
#extract_feature_state ⇒ Object
100 101 102 103 104 |
# File 'lib/feature_hub/sdk/feature_repository.rb', line 100 def extract_feature_state @features.values .filter(&:exists?) .map(&:feature_state) end |
#feature(key, attrs = nil) ⇒ Object
59 60 61 62 63 64 |
# File 'lib/feature_hub/sdk/feature_repository.rb', line 59 def feature(key, attrs = nil) holder = @features[key.to_sym] || make_feature_holder(key.to_sym) return holder unless attrs ClientContext.new(self, attrs).feature(key) end |
#find_interceptor(feature_key, feature_state = nil) ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/feature_hub/sdk/feature_repository.rb', line 79 def find_interceptor(feature_key, feature_state = nil) @interceptors.each do |interceptor| matched, value = interceptor.intercepted_value(feature_key, self, feature_state) return [true, value] if matched end [false, nil] end |
#not_ready! ⇒ Object
96 97 98 |
# File 'lib/feature_hub/sdk/feature_repository.rb', line 96 def not_ready! @ready = false end |
#notify(status, data, source = "unknown") ⇒ 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 |
# File 'lib/feature_hub/sdk/feature_repository.rb', line 25 def notify(status, data, source = "unknown") return unless status if status.to_sym == :failed @ready = false return end return if data.nil? case status.to_sym when :features update_features(data) @logger&.debug("[featurehubsdk] became ready through updates from #{source}") unless @ready @ready = true notify_raw_listeners_async { |l| l.process_updates(data, source) } @logger&.debug("[featurehubsdk] full updates from #{source} are #{data}") when :feature return if data.nil? || data["key"].nil? update_feature(data) @logger&.debug("[featurehubsdk] became ready through updates from #{source}") unless @ready @ready = true notify_raw_listeners_async { |l| l.process_update(data, source) } @logger&.debug("[featurehubsdk] single feature update from #{source} are #{data}") when :delete_feature return unless data && data["key"] delete_feature(data) notify_raw_listeners_async { |l| l.delete_feature(data, source) } @logger&.debug("[featurehubsdk] delete from #{source} are #{data}") end end |
#ready? ⇒ Boolean
92 93 94 |
# File 'lib/feature_hub/sdk/feature_repository.rb', line 92 def ready? @ready end |
#register_interceptor(interceptor) ⇒ Object
71 72 73 |
# File 'lib/feature_hub/sdk/feature_repository.rb', line 71 def register_interceptor(interceptor) @interceptors.push(interceptor) end |
#register_raw_update_listener(listener) ⇒ Object
75 76 77 |
# File 'lib/feature_hub/sdk/feature_repository.rb', line 75 def register_raw_update_listener(listener) @raw_listeners.push(listener) end |
#value(key, default_value = nil, attrs = nil) ⇒ Object
66 67 68 69 |
# File 'lib/feature_hub/sdk/feature_repository.rb', line 66 def value(key, default_value = nil, attrs = nil) f = feature(key, attrs) f.present? ? f.value : default_value end |