class Togls::FeatureRepository

Feature Repository

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

Public Class Methods

new(drivers) click to toggle source
# 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

Public Instance Methods

extract_feature_data(feature) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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