Class: GRI::Config

Inherits:
Object show all
Defined in:
lib/gri/config.rb

Constant Summary collapse

ROOT_PATH =
'/usr/local/gri'
DEFAULT_PATH =
ROOT_PATH + '/gri.conf'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



6
7
8
# File 'lib/gri/config.rb', line 6

def initialize
  @h = {}
end

Class Method Details

.[](key) ⇒ Object



75
76
77
# File 'lib/gri/config.rb', line 75

def self.[](key)
  @config[key]
end

.[]=(key, value) ⇒ Object



71
72
73
# File 'lib/gri/config.rb', line 71

def self.[]=(key, value)
  @config[key] = value
end

.get_targets_from_lines(lines) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/gri/config.rb', line 122

def self.get_targets_from_lines lines
  targets = []
  for line in lines
    if line =~ /^\s*(\w\S*)(?:\s+(.*))?/
      host, remain = $1, $2
      options = parse_options remain
      targets.push [host, options]
    end
  end
  targets
end

.getvar(key) ⇒ Object



67
68
69
# File 'lib/gri/config.rb', line 67

def self.getvar key
  @config.getvar key
end

.has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/gri/config.rb', line 83

def self.has_key? key
  @config.has_key? key
end

.init(path = nil) ⇒ Object



59
60
61
# File 'lib/gri/config.rb', line 59

def self.init path=nil
  @config = path ? load_from_file(path) : new
end

.keysObject



79
80
81
# File 'lib/gri/config.rb', line 79

def self.keys
  @config.keys
end

.load_from_file(path) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/gri/config.rb', line 42

def self.load_from_file path
  conf = self.new
  if File.exist? path
    File.open(path) {|f|
      while line = f.gets
        line.chomp!
        next if line =~ /^\s*(#|$)/
        if line =~ /^\s*(\S+)\s+(.*)/
          key, value = $1, $2
          conf[key] = value
        end
      end
    }
  end
  conf
end

.option_if_match(matchstr, varname, config) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/gri/config.rb', line 109

def self.option_if_match matchstr, varname, config
  h = {}
  if (varlines = config.getvar varname)
    for line in varlines
      pat, *optstr = line.split
      if matchstr =~ Regexp.new(pat)
        h.merge!(parse_options(*optstr))
      end
    end
  end
  h
end

.parse_options(*strs) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/gri/config.rb', line 87

def self.parse_options(*strs)
  options = {}
  for s in strs
    next unless s
    for var, value in s.scan(/([^=\s]+)(?:=((?:`[^`]+`)|(?:"[^"]+")|\S+))?/)
      if value
        if value =~ /^[`"]([^`"]+)[`"]/
          value = $1
        end
        options[var] = value
      else
        if var =~ /^no-/
          options[$~.post_match] = false
        else
          options[var] = var
        end
      end
    end
  end
  options
end

.setvar(key, value) ⇒ Object



63
64
65
# File 'lib/gri/config.rb', line 63

def self.setvar key, value
  @config.setvar key, value
end

Instance Method Details

#[](key) ⇒ Object



22
23
24
# File 'lib/gri/config.rb', line 22

def [](key)
  (Array === (v = @h[key])) ? v.last : v
end

#[]=(key, value) ⇒ Object



18
19
20
# File 'lib/gri/config.rb', line 18

def []=(key, value)
  (@h[key] ||= []).push value
end

#getvar(key) ⇒ Object



14
15
16
# File 'lib/gri/config.rb', line 14

def getvar key
  @h[key]
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/gri/config.rb', line 30

def has_key? key
  @h.has_key? key
end

#keysObject



26
27
28
# File 'lib/gri/config.rb', line 26

def keys
  @h.keys
end

#setvar(key, value) ⇒ Object



10
11
12
# File 'lib/gri/config.rb', line 10

def setvar key, value
  @h[key] = (Array === value) ? value : [value]
end

#to_hObject



34
35
36
37
38
39
40
# File 'lib/gri/config.rb', line 34

def to_h
  @h.inject({}) {|h, item|
    k, v = item
    h[k] = (item.last.size > 1) ? v : v.first
    h
  }
end