Sha256: e211f5369d9d1f7b0747138b2d66046fe45165f565c74f0b3a17aa8c8169ee5c

Contents?: true

Size: 1.08 KB

Versions: 4

Compression:

Stored size: 1.08 KB

Contents

require "confstruct/hash_with_struct_access"

module Confstruct
  
  class Configuration < HashWithStructAccess
  
    def initialize hash=@@hash_class.new, &block
      super({})
      @default_values = hash.is_a?(HashWithStructAccess) ? hash : HashWithStructAccess.from_hash(hash)
      eval_or_yield @default_values, &block
      reset_defaults!
    end
    
    def after_config! obj
    end
    
    def configure *args, &block
      if args[0].respond_to?(:each_pair)
        self.deep_merge!(args[0])
      end
      eval_or_yield self, &block
      after_config! self
      self
    end

    def push! *args, &block
      _stash.push(self.deep_copy)
      configure *args, &block if args.length > 0 or block_given?
      self
    end
    
    def pop!
      if _stash.empty?
        raise IndexError, "Stash is empty"
      else
        obj = _stash.pop
        self.clear
        self.merge! obj
        after_config! self
      end
      self
    end
    
    def reset_defaults!
      self.replace(default_values.deep_copy)
    end

    protected
    def _stash
      @stash ||= []
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
confstruct-0.2.4 lib/confstruct/configuration.rb
confstruct-0.2.3 lib/confstruct/configuration.rb
confstruct-0.2.2 lib/confstruct/configuration.rb
confstruct-0.2.1 lib/confstruct/configuration.rb