Module: Pit

Defined in:
lib/pit.rb,
lib/pit/version.rb

Constant Summary collapse

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

Class Method Summary collapse

Class Method Details

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

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.



46
47
48
49
50
51
52
53
54
55
# File 'lib/pit.rb', line 46

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

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

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.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/pit.rb', line 17

def self.set(name, opts={})
	profile = self.load
	if opts.key?(:data)
		result = opts[:data]
	else
		if ENV["EDITOR"].nil? || !$stdout.tty?
			return {}
		end
		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
			warn "No Changes"
			return profile[name]
		end
		result = YAML.load(result)
	end
	profile[name] = result
	@@profile.open("w") {|f| YAML.dump(profile, f) }
	result
end

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

Switch to current profile to name. Profile is set of settings. You can switch some settings using profile.



59
60
61
62
63
64
65
66
# File 'lib/pit.rb', line 59

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