Sha256: 789987f1e3f8a4f84a0e67582ad4ec8f8ec83758731c0740af698bf626f9daf7
Contents?: true
Size: 1.92 KB
Versions: 1
Compression:
Stored size: 1.92 KB
Contents
require 'thread_safe' require 'dry/configurable/config' require 'dry/configurable/version' module Dry # A simple configuration mixin # # @example # # class App # extend Dry::Configurable # # setting :database do # setting :dsn, 'sqlite:memory' # end # end # # App.confiure do |config| # config.database.dsn = 'jdbc:sqlite:memory' # end # # App.config.database.dsn # # => "jdbc:sqlite:memory'" # # @api public module Configurable # @private def self.extended(base) attr_reader :_settings base.class_eval do @_config_mutex = Mutex.new @_settings = ThreadSafe::Cache.new end end # Return configuration # # @return [Dry::Configurable::Config] # # @api public def config @_config_mutex.synchronize do return @_config if defined?(@_config) @_config = Config.new(*_settings.keys).new(*_settings.values) unless _settings.empty? end end # Return configuration # # @yield [Dry::Configuration::Config] # # @return [Dry::Configurable::Config] # # @api public def configure yield(config) if block_given? end # Add a setting to the configuration # # @param [Mixed] key # The accessor key for the configuration value # @param [Mixed] default # The default config value # # @yield # If a block is given, it will be evaluated in the context of # and new configuration class, and bound as the default value # # @return [Dry::Configurable::Config] # # @api public def setting(key, default = nil, &block) default = _config_for(&block) if block_given? _settings[key] = default end private # @private def _config_for(&block) config_klass = Class.new { extend Dry::Configurable } config_klass.instance_eval(&block) config_klass.config end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
dry-configurable-0.1.1 | lib/dry/configurable.rb |