Class: ALib::ConfigFile

Inherits:
Hash show all
Defined in:
lib/alib-0.5.1/configfile.rb

Overview

yaml configfile class

Constant Summary collapse

DEFAULT_CONIFG =

–{{{

{}
DEFAULT_SEARCH_PATH =
%w(. ~ /usr/local/etc /usr/etc /etc)
DEFAULT_BASENAME =
".#{ File::basename $0 }.conf"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Hash

#getopt, #hasopt?

Constructor Details

#initialize(path = 'default') ⇒ ConfigFile

Returns a new instance of ConfigFile.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/alib-0.5.1/configfile.rb', line 92

def initialize path = 'default'
#--{{{
  @path = nil 
  yaml = nil
  if path.nil? or path and path =~ /^\s*default/io
    yaml = self.class.default 
    @path = 'DEFAULT' 
  else path
    yaml = YAML::load(self.class.munge(open(path).read))
    @path = path
  end
  self.update yaml
#--}}}
end

Class Attribute Details

.basenameObject

Returns the value of attribute basename.



14
15
16
# File 'lib/alib-0.5.1/configfile.rb', line 14

def basename
  @basename
end

.configObject

–{{{



12
13
14
# File 'lib/alib-0.5.1/configfile.rb', line 12

def config
  @config
end

.default_textObject

Returns the value of attribute default_text.



15
16
17
# File 'lib/alib-0.5.1/configfile.rb', line 15

def default_text
  @default_text
end

.search_pathObject

Returns the value of attribute search_path.



13
14
15
# File 'lib/alib-0.5.1/configfile.rb', line 13

def search_path
  @search_path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



91
92
93
# File 'lib/alib-0.5.1/configfile.rb', line 91

def path
  @path
end

Class Method Details

.any(basename = @basename, *dirnames) ⇒ Object

–}}}



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/alib-0.5.1/configfile.rb', line 67

def any(basename = @basename, *dirnames)
#--{{{
  config = nil
  dirnames = @search_path if dirnames.empty?
  dirnames.each do |dirname|
    path = File::join dirname, basename 
    path = File::expand_path path
    if test ?e, path
      config = self::new path
      break
    end
  end
  config || self::new('default') 
#--}}}
end

.default(*a) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/alib-0.5.1/configfile.rb', line 58

def default(*a)
#--{{{
  if a.empty?
    @config ||= {}
  else
    self.default= a.first
  end
#--}}}
end

.default=(conf) ⇒ Object Also known as: set_default

–}}}



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/alib-0.5.1/configfile.rb', line 35

def default= conf
#--{{{
  if conf.respond_to?('read')
    @default_text = munge conf.read
    @config = YAML::load @default_text
  else
    case conf
      when Hash
        @config = conf
      when Pathname 
        open(conf) do |f| 
          @default_text = munge f.read
          @config = YAML::load @default_text
        end
      when String
        @default_text = munge conf 
        @config = YAML::load @default_text
    end
  end
  @config
#--}}}
end

.gen_template(port = nil) ⇒ Object

–}}}



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/alib-0.5.1/configfile.rb', line 23

def gen_template port = nil 
#--{{{
  port ||= STDOUT
  buf = @default_text || @config.to_yaml
  if port.respond_to? 'write' 
    port.write buf
    port.write "\n"
  else
    open("#{ port }", 'w'){|f| f.puts buf}
  end
#--}}}
end

.initObject



16
17
18
19
20
21
22
# File 'lib/alib-0.5.1/configfile.rb', line 16

def init
#--{{{
  @config = DEFAULT_CONIFG
  @search_path = DEFAULT_SEARCH_PATH
  @basename = DEFAULT_BASENAME
#--}}}
end

.munge(buf) ⇒ Object

–}}}



82
83
84
85
86
# File 'lib/alib-0.5.1/configfile.rb', line 82

def munge buf
#--{{{
  buf.gsub(%r/\t/o,' ')
#--}}}
end

Instance Method Details

#to_hashObject

–}}}



106
107
108
109
110
# File 'lib/alib-0.5.1/configfile.rb', line 106

def to_hash
#--{{{
  {}.update self
#--}}}
end