Module: Pit

Defined in:
lib/pit.rb

Constant Summary collapse

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

Class Method Summary collapse

Class Method Details

.configObject



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

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

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



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

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



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

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



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

def self.set(name, opts={})
  profile = self.load
  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
      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



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

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