Class: RQ::ConfigFile

Inherits:
Hash
  • Object
show all
Defined in:
lib/rq/configfile.rb

Overview

the ConfigFile class is a thin class that munges yaml input and populates itself

Constant Summary collapse

DEFAULT_CONFIG =

–{{{

LIBDIR + 'defaultconfig.txt'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ ConfigFile

Returns a new instance of ConfigFile.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rq/configfile.rb', line 58

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

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



57
58
59
# File 'lib/rq/configfile.rb', line 57

def path
  @path
end

Class Method Details

.any(basename, *dirnames) ⇒ Object

–}}}



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rq/configfile.rb', line 38

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

.gen_template(arg = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rq/configfile.rb', line 18

def gen_template(arg = nil)
#--{{{
  @data ||= IO::read(DEFAULT_CONFIG)
  case arg 
    when IO 
      arg.write @data
    when String
      open(arg, 'w'){|f| f.write @data}
    else
      STDOUT.write @data 
  end
  self
#--}}}
end

.load_defaultObject

–}}}



32
33
34
35
36
37
# File 'lib/rq/configfile.rb', line 32

def load_default
#--{{{
  @data ||= IO::read(DEFAULT_CONFIG)
  @default ||= YAML::load(munge(@data)) || {}
#--}}}
end

.munge(buf) ⇒ Object

–}}}



51
52
53
54
55
# File 'lib/rq/configfile.rb', line 51

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

Instance Method Details

#to_hashObject

–}}}



72
73
74
75
76
# File 'lib/rq/configfile.rb', line 72

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