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 }
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



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

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

#reconstitute_feature(feature_data) ⇒ Object



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

def reconstitute_feature(feature_data)
  Togls::Feature.new(feature_data['key'],
                     feature_data['description'])
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