Class: ALib::ConfigFile
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
–}}}.
Methods inherited from Hash
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
.basename ⇒ Object
Returns the value of attribute basename.
14 15 16 |
# File 'lib/alib-0.5.1/configfile.rb', line 14 def basename @basename end |
.config ⇒ Object
–{{{
12 13 14 |
# File 'lib/alib-0.5.1/configfile.rb', line 12 def config @config end |
.default_text ⇒ Object
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_path ⇒ Object
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
#path ⇒ Object (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:: 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 |
.init ⇒ Object
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_hash ⇒ Object
–}}}
106 107 108 109 110 |
# File 'lib/alib-0.5.1/configfile.rb', line 106 def to_hash #--{{{ {}.update self #--}}} end |