Constants
- VERSION
"0.0.5"- Directory
Pathname.new("~/.pit").expand_path
Public Class methods
Get name setting from current profile. If not opts specified, this just returns setting from current profile. If require specified, check keys on setting and open $EDITOR.
# File lib/pit.rb, line 46
46: def self.get(name, opts={})
47: ret = self.load[name] || {}
48: if opts[:require]
49: unless opts[:require].keys.all? {|k| ret[k] }
50: ret = opts[:require].update(ret)
51: ret = self.set(name, :config => ret)
52: end
53: end
54: ret || {"username"=>"", "password"=>""}
55: end
Set name setting to current profile. If not opts specified, this opens $EDITOR with current profile setting. If `data` specified, this just sets it to current profile. If `config` specified, this opens $EDITOR with merged hash with specified hash and current profile.
# File lib/pit.rb, line 17
17: def self.set(name, opts={})
18: profile = self.load
19: if opts.key?(:data)
20: result = opts[:data]
21: else
22: if ENV["EDITOR"].nil? || !$stdout.tty?
23: return {}
24: end
25: c = (opts[:config] || self.get(name)).to_yaml
26: t = Tempfile.new("pit")
27: t << c
28: t.close
29: system(ENV["EDITOR"], t.path)
30: t.open
31: result = t.read
32: if result == c
33: warn "No Changes"
34: return profile[name]
35: end
36: result = YAML.load(result)
37: end
38: profile[name] = result
39: @@profile.open("w") {|f| YAML.dump(profile, f) }
40: result
41: end
Switch to current profile to name. Profile is set of settings. You can switch some settings using profile.
# File lib/pit.rb, line 59
59: def self.switch(name, opts={})
60: @@profile = Directory + "#{name}.yaml"
61: config = self.config
62: ret = config["profile"]
63: config["profile"] = name
64: @@config.open("w") {|f| f << config.to_yaml }
65: ret
66: end
Protected Class methods
# File lib/pit.rb, line 84
84: def self.config
85: YAML.load(@@config.read)
86: end
# File lib/pit.rb, line 69
69: def self.load
70: Directory.mkpath unless Directory.exist?
71: Directory.chmod 0700
72: unless @@config.exist?
73: @@config.open("w") {|f| f << {"profile"=>"default"}.to_yaml }
74: @@config.chmod(0600)
75: end
76: self.switch(self.config["profile"])
77: unless @@profile.exist?
78: @@profile.open("w") {|f| f << {}.to_yaml }
79: @@profile.chmod(0600)
80: end
81: YAML.load(@@profile.read) || {}
82: end