|
Revision 3968, 1.1 kB
(checked in by cho45, 5 years ago)
|
|
lang/ruby/misc/vox-upload-photo-atom.rb,
lang/ruby/pit,
lang/ruby/pit/test,
lang/ruby/pit/test/test_helper.rb,
lang/ruby/pit/test/pit_test.rb,
lang/ruby/pit/Rakefile,
lang/ruby/pit/lib,
lang/ruby/pit/lib/pit.rb,
lang/ruby/pit/ChangeLog,
lang/ruby/pit/bin,
lang/ruby/pit/README:
import
|
| Line | |
|---|
| 1 | |
|---|
| 2 | require "yaml" |
|---|
| 3 | require "pathname" |
|---|
| 4 | require "tempfile" |
|---|
| 5 | |
|---|
| 6 | module Pit |
|---|
| 7 | Directory = Pathname.new("~/.pit").expand_path |
|---|
| 8 | @@config = Directory + "default.yaml" |
|---|
| 9 | |
|---|
| 10 | def self.set(name, opts={}) |
|---|
| 11 | if opts.key?(:data) |
|---|
| 12 | result = opts[:data] |
|---|
| 13 | else |
|---|
| 14 | c = self.get(name).to_yaml |
|---|
| 15 | t = Tempfile.new("pit") |
|---|
| 16 | t << c |
|---|
| 17 | t.close |
|---|
| 18 | system(ENV["EDITOR"], t.path) |
|---|
| 19 | t.open |
|---|
| 20 | result = t.read |
|---|
| 21 | if result == c |
|---|
| 22 | $stderr.puts "No Changes" |
|---|
| 23 | return |
|---|
| 24 | end |
|---|
| 25 | result = YAML.load(result) |
|---|
| 26 | end |
|---|
| 27 | config = self.load |
|---|
| 28 | config[name] = result |
|---|
| 29 | @@config.open("w") {|f| YAML.dump(config, f) } |
|---|
| 30 | nil |
|---|
| 31 | end |
|---|
| 32 | |
|---|
| 33 | def self.get(name, opts={}) |
|---|
| 34 | self.load[name] || {"username"=>"", "password"=>""} |
|---|
| 35 | end |
|---|
| 36 | |
|---|
| 37 | def self.switch(name, opts={}) |
|---|
| 38 | # TODO |
|---|
| 39 | @@config = Directory + "#{name}.yaml" |
|---|
| 40 | end |
|---|
| 41 | |
|---|
| 42 | def self.load |
|---|
| 43 | Directory.mkpath unless Directory.exist? |
|---|
| 44 | unless @@config.exist? |
|---|
| 45 | @@config.open("w") {|f| f << {}.to_yaml } |
|---|
| 46 | @@config.chmod(0600) |
|---|
| 47 | end |
|---|
| 48 | YAML.load(@@config.read) || {} |
|---|
| 49 | end |
|---|
| 50 | end |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | __END__ |
|---|
| 54 | p Pit.set("test") |
|---|
| 55 | p Pit.get("test") |
|---|
| 56 | |
|---|
| 57 | config = Pit.get("twitter") |
|---|
| 58 | p config["password"] |
|---|
| 59 | p config["username"] |
|---|
| 60 | |
|---|