Class: ALib::ConfigFile
- Inherits:
-
Hash
- Object
- Hash
- ALib::ConfigFile
- 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
-
.basename ⇒ Object
Returns the value of attribute basename.
-
.config ⇒ Object
–{{{.
-
.default_text ⇒ Object
Returns the value of attribute default_text.
-
.search_path ⇒ Object
Returns the value of attribute search_path.
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
-
.any(basename = @basename, *dirnames) ⇒ Object
–}}}.
- .default(*a) ⇒ Object
-
.default=(conf) ⇒ Object
(also: set_default)
–}}}.
-
.gen_template(port = nil) ⇒ Object
–}}}.
- .init ⇒ Object
-
.munge(buf) ⇒ Object
–}}}.
Instance Method Summary collapse
-
#initialize(path = 'default') ⇒ ConfigFile
constructor
A new instance of ConfigFile.
-
#to_hash ⇒ Object
–}}}.
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
.basename ⇒ Object
Returns the value of attribute basename.
1111 1112 1113 |
# File 'lib/alib.rb', line 1111 def basename @basename end |
.config ⇒ Object
–{{{
1109 1110 1111 |
# File 'lib/alib.rb', line 1109 def config @config end |
.default_text ⇒ Object
Returns the value of attribute default_text.
1112 1113 1114 |
# File 'lib/alib.rb', line 1112 def default_text @default_text end |
.search_path ⇒ Object
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
#path ⇒ Object (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:: 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 |
.init ⇒ Object
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_hash ⇒ Object
–}}}
1203 1204 1205 1206 1207 |
# File 'lib/alib.rb', line 1203 def to_hash #--{{{ {}.update self #--}}} end |