config-factory
A gem for creating configuration classes using the Abstract Factory, pattern, with run-time configuration provided by hashes or YAML files.
Example
class SourceConfig
include Config::Factory
key :protocol
end
class OAISourceConfig < SourceConfig
protocol 'OAI'
def initialize(oai_base_url:, metadata_prefix:, set: nil, seconds_granularity: false)
end
end
class ResyncSourceConfig < SourceConfig
protocol 'Resync'
def initialize(capability_list_url:)
end
end
Single-environment example
Configuration file:
source:
protocol: OAI
oai_base_url: http://oai.example.org/oai
metadata_prefix: some_prefix
set: some_set
seconds_granularity: true
Loading:
environment = Environment.load_file('spec/data/single-environment.yml')
# => #<Config::Factory::Environment:0x007fe8d3883240 @name=:production, @configs={"source"=>{"protocol"=>"OAI", "oai_base_url"=>"http://oai.example.org/oai", "metadata_prefix"=>"some_prefix", "set"=>"some_set", "seconds_granularity"=>true}}>
source_config = SourceConfig.for_environment(environment, :source)
# => #<OAISourceConfig:0x007fe8d38b3990 @oai_base_url="http://oai.example.org/oai", @metadata_prefix="some_prefix", @set="some_set", @seconds_granularity=true>
Multiple-environment example
Configuration file:
test:
source:
protocol: Resync
capability_list_url: http://localhost:8888/capabilitylist.xml
production:
source:
protocol: OAI
oai_base_url: http://oai.example.org/oai
metadata_prefix: some_prefix
set: some_set
seconds_granularity: true
Loading:
environments = Environments.load_file('spec/data/multiple_environments.yml')
# => {:test=>#<Config::Factory::Environment:0x007fe8d3863dc8 @name=:test, @configs={"source"=>{"protocol"=>"Resync", "capability_list_url"=>"http://localhost:8888/capabilitylist.xml"}}>, :production=>#<Config::Factory::Environment:0x007fe8d3863be8 @name=:production, @configs={"source"=>{"protocol"=>"OAI", "oai_base_url"=>"http://oai.example.org/oai", "metadata_prefix"=>"some_prefix", "set"=>"some_set", "seconds_granularity"=>true}}>}
test_env = environments[:test]
# => #<Config::Factory::Environment:0x007fe8d383a400 @name=:test, @configs={"source"=>{"protocol"=>"Resync", "capability_list_url"=>"http://localhost:8888/capabilitylist.xml"}}>
source_config = SourceConfig.for_environment(test_env, :source)
# => #<ResyncSourceConfig:0x007fe8d48180c0 @capability_list_url="http://localhost:8888/capabilitylist.xml">