Class: Whodunit::Chronicles::LedgerFactory
- Inherits:
-
Object
- Object
- Whodunit::Chronicles::LedgerFactory
- Defined in:
- lib/whodunit/chronicles/ledger_factory.rb,
sig/whodunit/chronicles/ledger_factory.rbs
Overview
Builds ledger instances from simple configuration hashes.
Class Method Summary collapse
-
.build(config) ⇒ Ledger
Build a ledger from configuration.
Instance Method Summary collapse
-
#build ⇒ Ledger
Build the configured ledger.
-
#initialize(config) ⇒ LedgerFactory
constructor
Create a factory.
-
#required_value(config, key) ⇒ Object
Fetch a required config value with a user-facing error.
-
#stringify_keys(value) ⇒ Object
Convert hash keys to strings recursively.
Constructor Details
#initialize(config) ⇒ LedgerFactory
Create a factory.
23 24 25 |
# File 'lib/whodunit/chronicles/ledger_factory.rb', line 23 def initialize(config) @config = stringify_keys(config) end |
Class Method Details
.build(config) ⇒ Ledger
Build a ledger from configuration.
16 17 18 |
# File 'lib/whodunit/chronicles/ledger_factory.rb', line 16 def self.build(config) new(config).build end |
Instance Method Details
#build ⇒ Ledger
Build the configured ledger.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/whodunit/chronicles/ledger_factory.rb', line 30 def build ledger = stringify_keys(@config.fetch('ledger', @config)) adapter = required_value(ledger, 'adapter') case adapter when 'memory' Ledgers::MemoryLedger.new when 'file' Ledgers::FileLedger.new(path: required_value(ledger, 'path')) when 'sqlite' Ledgers::SQLiteLedger.new(path: required_value(ledger, 'path'), table_name: ledger.fetch('table_name', Ledgers::SQLiteLedger::DEFAULT_TABLE)) else raise ConfigurationError, "unsupported ledger adapter: #{adapter.inspect}" end end |
#required_value(config, key) ⇒ Object
Fetch a required config value with a user-facing error.
59 60 61 62 63 |
# File 'lib/whodunit/chronicles/ledger_factory.rb', line 59 def required_value(config, key) config.fetch(key) rescue KeyError raise ConfigurationError, "missing ledger #{key}" end |
#stringify_keys(value) ⇒ Object
Convert hash keys to strings recursively.
49 50 51 52 53 54 55 56 |
# File 'lib/whodunit/chronicles/ledger_factory.rb', line 49 def stringify_keys(value) case value when Hash value.to_h { |key, nested| [key.to_s, stringify_keys(nested)] } else value end end |