Class: Togls::RuleRepository
- Inherits:
-
Object
- Object
- Togls::RuleRepository
- Defined in:
- lib/togls/rule_repository.rb
Overview
Rule Repository
The Rule Repository is the intended interface to store and retrieve rules. It does these by interfacing with Rule Repository Drivers which are passed in during construction as an Array.
Instance Method Summary collapse
- #extract_storage_payload(rule) ⇒ Object
- #fetch_rule_data(id) ⇒ Object
- #get(rule_id) ⇒ Object
-
#initialize(drivers) ⇒ RuleRepository
constructor
A new instance of RuleRepository.
- #reconstitute_rule(rule_data) ⇒ Object
- #store(rule) ⇒ Object
Constructor Details
#initialize(drivers) ⇒ RuleRepository
Returns a new instance of RuleRepository.
8 9 10 11 12 13 14 15 16 |
# File 'lib/togls/rule_repository.rb', line 8 def initialize(drivers) unless drivers.is_a?(Array) raise Togls::InvalidDriver, 'RuleRepository requires a valid driver' end if drivers.empty? raise Togls::MissingDriver, 'RuleRepository requires a driver' end @drivers = drivers end |
Instance Method Details
#extract_storage_payload(rule) ⇒ Object
25 26 27 |
# File 'lib/togls/rule_repository.rb', line 25 def extract_storage_payload(rule) { 'klass' => rule.class, 'data' => rule.data } end |
#fetch_rule_data(id) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/togls/rule_repository.rb', line 29 def fetch_rule_data(id) rule_data = nil @drivers.reverse.each do |driver| rule_data = driver.get(id) break if rule_data end rule_data end |
#get(rule_id) ⇒ Object
38 39 40 41 |
# File 'lib/togls/rule_repository.rb', line 38 def get(rule_id) rule_data = fetch_rule_data(rule_id) reconstitute_rule(rule_data) end |
#reconstitute_rule(rule_data) ⇒ Object
43 44 45 |
# File 'lib/togls/rule_repository.rb', line 43 def reconstitute_rule(rule_data) rule_data['klass'].new(rule_data['data']) end |
#store(rule) ⇒ Object
18 19 20 21 22 23 |
# File 'lib/togls/rule_repository.rb', line 18 def store(rule) rule_data = extract_storage_payload(rule) @drivers.each do |driver| driver.store(rule.id, rule_data) end end |