Sha256: 2a2ec073e7a6f3972141edd2b089e6e303f839adf7d4d2a6891141b4b9c0cf1b
Contents?: true
Size: 1.44 KB
Versions: 1
Compression:
Stored size: 1.44 KB
Contents
require 'usable/config_multi' require 'usable/config_register' module Usable # Store and manage configuration settings. Keep methods to a minimum since this class relies on method_missing to read # and write to the underlying @spec object class Config include ConfigRegister include ConfigMulti def initialize @spec = OpenStruct.new @lazy_loads = Set.new end def each(&block) @spec.to_h.each(&block) end def spec(key, value = nil) if value @spec[key.to_s.tr('=', '')] = value else # Handle the case where the value may be defined with a block, in which case it's a method @spec[key] ||= call_lazy_method(key) end end def _spec @spec end def [](key) spec key end def []=(key, val) spec key, val end def to_h @lazy_loads.each { |key| spec(key) } _spec.to_h end alias to_hash to_h def call_lazy_method(key) @lazy_loads.delete key @spec.public_send key.to_s.tr('=', '') end def method_missing(method_name, *args, &block) if block _spec.define_singleton_method(method_name) { yield } @lazy_loads << method_name else spec method_name, *args end rescue NoMethodError super end def respond_to_missing?(method_name, _private = false) method_name.to_s.end_with?('=') || _spec.respond_to?(method_name) end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
usable-3.1.0 | lib/usable/config.rb |