Sha256: 7ab8b48429c5b4652e992f6a227d693292d7193e4603e4e29cc548e0b79ffd2d

Contents?: true

Size: 1.09 KB

Versions: 3

Compression:

Stored size: 1.09 KB

Contents

require "confstruct/hash_with_struct_access"

module Confstruct
  
  class Configuration < HashWithStructAccess
  
    def initialize hash={}, default=nil, &block
      super(hash, default)
      @default_values = 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!(HashWithStructAccess.from_hash(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.deep_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

3 entries across 3 versions & 1 rubygems

Version Path
confstruct-1.1.0 lib/confstruct/configuration.rb
confstruct-1.0.2 lib/confstruct/configuration.rb
confstruct-1.0.1 lib/confstruct/configuration.rb