Class: Togls::FeatureRepository

Inherits:
Object
  • Object
show all
Defined in:
lib/togls/feature_repository.rb

Overview

Feature Repository

The Feature Repository is the primary interface for storing and retrieving feature entities using the initialized prioritized drivers Array.

Instance Method Summary collapse

Constructor Details

#initialize(drivers) ⇒ FeatureRepository

Returns a new instance of FeatureRepository.



7
8
9
10
11
12
13
14
15
# File 'lib/togls/feature_repository.rb', line 7

def initialize(drivers)
  unless drivers.is_a?(Array)
    raise Togls::InvalidDriver, 'FeatureRepository requires a valid driver'
  end
  if drivers.empty?
    raise Togls::MissingDriver, 'FeatureRepository requires a driver'
  end
  @drivers = drivers
end

Instance Method Details

#extract_feature_data(feature) ⇒ Object



24
25
26
# File 'lib/togls/feature_repository.rb', line 24

def extract_feature_data(feature)
  { 'key' => feature.key, 'description' => feature.description, 'target_type' => feature.target_type.to_s }
end

#fetch_feature_data(id) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/togls/feature_repository.rb', line 28

def fetch_feature_data(id)
  feature_data = nil
  @drivers.reverse.each do |driver|
    feature_data = driver.get(id)
    break if feature_data
  end
  feature_data
end

#get(feature_id) ⇒ Object



42
43
44
45
46
# File 'lib/togls/feature_repository.rb', line 42

def get(feature_id)
  feature_data = fetch_feature_data(feature_id)
  validate_feature_data(feature_data)
  reconstitute_feature(feature_data)
end

#include?(feature_id) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
# File 'lib/togls/feature_repository.rb', line 37

def include?(feature_id)
  result = fetch_feature_data(feature_id)
  result.nil? ? false : true
end

#reconstitute_feature(feature_data) ⇒ Object



48
49
50
51
52
# File 'lib/togls/feature_repository.rb', line 48

def reconstitute_feature(feature_data)
  Togls::Feature.new(feature_data['key'],
                     feature_data['description'],
                     feature_data['target_type'].to_sym)
end

#store(feature) ⇒ Object



17
18
19
20
21
22
# File 'lib/togls/feature_repository.rb', line 17

def store(feature)
  feature_data = extract_feature_data(feature)
  @drivers.each do |driver|
    driver.store(feature.id, feature_data)
  end
end

#validate_feature_data(feature_data) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/togls/feature_repository.rb', line 54

def validate_feature_data(feature_data)
  if feature_data.nil?
    Togls.logger.warn("None of the feature repository drivers claim to have the feature")
    raise Togls::RepositoryFeatureDataInvalid, "None of the feature repository drivers claim to have the feature"
  end

  ['key', 'description', 'target_type'].each do |k|
    if !feature_data.has_key? k
      Togls.logger.warn("One of the feature repository drivers returned feature data that is missing the '#{k}'")
      raise Togls::RepositoryFeatureDataInvalid, "One of the feature repository drivers returned feature data that is missing the '#{k}'"
    end

    if !feature_data[k].is_a?(String)
      Togls.logger.warn("One of the feature repository drivers returned feature data with '#{k}' not being a string")
      raise Togls::RepositoryFeatureDataInvalid, "One of the feature repository drivers returned feature data with '#{k}' not being a string"
    end
  end
end