Module: Pit

Defined in:
lib/pit.rb

Constant Summary collapse

Directory =
Pathname.new("~/.pit").expand_path
@@config =
Directory + "pit.yaml"
@@profile =
Directory + "default.yaml"

Class Method Summary collapse

Class Method Details

.configObject



66
67
68
# File 'lib/pit.rb', line 66

def self.config
	YAML.load(@@config.read)
end

.get(name, opts = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/pit.rb', line 34

def self.get(name, opts={})
	ret = self.load[name] || {}
	if opts[:require]
		unless opts[:require].keys.all? {|k| ret[k] }
			ret.update(opts[:require])
			ret = self.set(name, :config => ret)
		end
	end
	ret || {"username"=>"", "password"=>""}
end

.loadObject



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/pit.rb', line 52

def self.load
	Directory.mkpath unless Directory.exist?
	unless @@config.exist?
		@@config.open("w") {|f| f << {"profile"=>"default"}.to_yaml }
		@@config.chmod(0600)
	end
	self.switch(self.config["profile"])
	unless @@profile.exist?
		@@profile.open("w") {|f| f << {}.to_yaml }
		@@profile.chmod(0600)
	end
	YAML.load(@@profile.read) || {}
end

.set(name, opts = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/pit.rb', line 11

def self.set(name, opts={})
	if opts.key?(:data)
		result = opts[:data]
	else
		c = (opts[:config] || self.get(name)).to_yaml
		t = Tempfile.new("pit")
		t << c
		t.close
		system(ENV["EDITOR"], t.path)
		t.open
		result = t.read
		if result == c
			$stderr.puts "No Changes"
			return
		end
		result = YAML.load(result)
	end
	config = self.load
	config[name] = result
	@@profile.open("w") {|f| YAML.dump(config, f) }
	config[name]
end

.switch(name, opts = {}) ⇒ Object



45
46
47
48
49
50
# File 'lib/pit.rb', line 45

def self.switch(name, opts={})
	@@profile = Directory + "#{name}.yaml"
	config = self.config
	config["profile"] = name
	@@config.open("w") {|f| f << config.to_yaml }
end