class Feature::Repository::ActiveRecordRepository
AcitveRecordRepository for active feature list Right now we assume you have at least name:string and active:boolean defined in your table.
Example usage:
repository = ActiveRecordRepository.new(FeatureToggle) repository.add_active_feature(:feature_name) # use repository with Feature
Public Class Methods
new(model)
click to toggle source
Constructor
# File lib/feature/repository/active_record_repository.rb, line 15 def initialize(model) @model = model end
Public Instance Methods
active_features()
click to toggle source
Returns list of active features
@return [Array<Symbol>] list of active features
# File lib/feature/repository/active_record_repository.rb, line 23 def active_features @model.where(active: true).map { |f| f.name.to_sym } end
add_active_feature(feature)
click to toggle source
Add an active feature to repository
@param [Symbol] feature the feature to be added
# File lib/feature/repository/active_record_repository.rb, line 31 def add_active_feature(feature) check_feature_is_not_symbol(feature) check_feature_already_in_list(feature) @model.create!(name: feature.to_s, active: true) end
Private Instance Methods
check_feature_already_in_list(feature)
click to toggle source
Checks if given feature is already added to list of active features and raises an exception if so
@param [Symbol] feature the feature to be checked
# File lib/feature/repository/active_record_repository.rb, line 51 def check_feature_already_in_list(feature) raise ArgumentError, "feature :#{feature} already added" if @model.exists?(name: feature.to_s) end
check_feature_is_not_symbol(feature)
click to toggle source
Checks if the given feature is a not symbol and raises an exception if so
@param [Sybmol] feature the feature to be checked
# File lib/feature/repository/active_record_repository.rb, line 41 def check_feature_is_not_symbol(feature) raise ArgumentError, "#{feature} is not a symbol" unless feature.instance_of?(Symbol) end