Class: ALib::ConfigFile

Inherits:
Hash
  • Object
show all
Defined in:
lib/alib.rb,
lib/alib-0.3.1.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

Constructor Details

#initialize(path = 'default') ⇒ ConfigFile

Returns a new instance of ConfigFile.



1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
# File 'lib/alib.rb', line 1189

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.



1111
1112
1113
# File 'lib/alib.rb', line 1111

def basename
  @basename
end

.configObject

–{{{



1109
1110
1111
# File 'lib/alib.rb', line 1109

def config
  @config
end

.default_textObject

Returns the value of attribute default_text.



1112
1113
1114
# File 'lib/alib.rb', line 1112

def default_text
  @default_text
end

.search_pathObject

Returns the value of attribute search_path.



1110
1111
1112
# File 'lib/alib.rb', line 1110

def search_path
  @search_path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



1188
1189
1190
# File 'lib/alib.rb', line 1188

def path
  @path
end

Class Method Details

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

–}}}



1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
# File 'lib/alib.rb', line 1164

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



1155
1156
1157
1158
1159
1160
1161
1162
1163
# File 'lib/alib.rb', line 1155

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

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

–}}}



1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
# File 'lib/alib.rb', line 1132

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

–}}}



1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
# File 'lib/alib.rb', line 1120

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



1113
1114
1115
1116
1117
1118
1119
# File 'lib/alib.rb', line 1113

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

.munge(buf) ⇒ Object

–}}}



1179
1180
1181
1182
1183
# File 'lib/alib.rb', line 1179

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

Instance Method Details

#to_hashObject

–}}}



1203
1204
1205
1206
1207
# File 'lib/alib.rb', line 1203

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