Class: Confstruct::Configuration

Inherits:
HashWithStructAccess show all
Defined in:
lib/confstruct/configuration.rb

Instance Attribute Summary

Attributes inherited from HashWithStructAccess

#default_values

Instance Method Summary collapse

Methods inherited from HashWithStructAccess

#[], #convert_key, #deep_copy, #deferred!, from_hash, #has?, #i18n!, #inspect, #lookup!, #method_missing, structurize, #values

Constructor Details

#initialize(hash = {}, default = nil, &block) ⇒ Configuration

Returns a new instance of Configuration.



7
8
9
10
11
12
# File 'lib/confstruct/configuration.rb', line 7

def initialize hash={}, default=nil, &block
  super(hash, default)
  @default_values = HashWithStructAccess.from_hash(hash)
  eval_or_yield @default_values, &block
  reset_defaults!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Confstruct::HashWithStructAccess

Instance Method Details

#after_config!(obj) ⇒ Object



14
15
# File 'lib/confstruct/configuration.rb', line 14

def after_config! obj
end

#configure(*args, &block) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/confstruct/configuration.rb', line 17

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

#pop!Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/confstruct/configuration.rb', line 32

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

#push!(*args, &block) ⇒ Object



26
27
28
29
30
# File 'lib/confstruct/configuration.rb', line 26

def push! *args, &block
  _stash.push(self.deep_copy)
  configure *args, &block if args.length > 0 or block_given?
  self
end

#reset_defaults!Object



44
45
46
# File 'lib/confstruct/configuration.rb', line 44

def reset_defaults!
  self.replace(default_values.deep_copy)
end