Class: ConfigFile

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/config_file.rb

Defined Under Namespace

Classes: FormatError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, default_config = nil) ⇒ ConfigFile

Returns a new instance of ConfigFile.



21
22
23
24
25
# File 'lib/config_file.rb', line 21

def initialize(filename, default_config=nil)
  @filename = Pathname.new(filename)
  @default_config = default_config.freeze
  __load__
end

Instance Attribute Details

#default_configObject (readonly)

Returns the value of attribute default_config.



19
20
21
# File 'lib/config_file.rb', line 19

def default_config
  @default_config
end

Instance Method Details

#__load__(&block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/config_file.rb', line 27

def __load__(&block)
  config = nil
  if @filename.exist?
    @filename.open do |f|
      f.flock(File::LOCK_EX)
      config = real_load(f)
      f.flock(File::LOCK_UN)
    end
    block ? block[config] : check_config(config)
    config.freeze
  else
    if $VERBOSE
      Kernel.warn('warning: use default configuration because no ' +
                  'configuration file has been found')
    end
    config = @default_config
  end
  __setobj__(config)
end