Class: FileStorage
- Inherits:
-
Object
- Object
- FileStorage
- Defined in:
- lib/mygit.rb
Constant Summary collapse
- CONFIG_DIR =
"#{Dir.home}/.mygit"
- CONFIG_FILE =
"#{CONFIG_DIR}/config.yml"
- REPOS_FILE =
"#{CONFIG_DIR}/repos.yml"
- KEEP_KEYS =
['name', 'ssh_url', 'html_url']
Instance Attribute Summary collapse
-
#repo ⇒ Object
readonly
Returns the value of attribute repo.
Class Method Summary collapse
- .get_input(prompt, echo = true) ⇒ Object
- .remove_keys(array) ⇒ Object
- .save(data) ⇒ Object
- .update(filename = CONFIG_FILE) ⇒ Object
Instance Method Summary collapse
-
#initialize ⇒ FileStorage
constructor
A new instance of FileStorage.
Constructor Details
#initialize ⇒ FileStorage
Returns a new instance of FileStorage.
24 25 26 |
# File 'lib/mygit.rb', line 24 def initialize @repo ||= YAML.load_file REPOS_FILE if File.exist?(REPOS_FILE) end |
Instance Attribute Details
#repo ⇒ Object (readonly)
Returns the value of attribute repo.
22 23 24 |
# File 'lib/mygit.rb', line 22 def repo @repo end |
Class Method Details
.get_input(prompt, echo = true) ⇒ Object
49 50 51 |
# File 'lib/mygit.rb', line 49 def get_input prompt, echo = true ask(prompt) {|q| q.echo = echo} end |
.remove_keys(array) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/mygit.rb', line 41 def remove_keys array keep = [] array.each do |a| keep << a.keep_if {|k| KEEP_KEYS.include? k } end keep end |
.save(data) ⇒ Object
53 54 55 56 |
# File 'lib/mygit.rb', line 53 def save data File.open(REPOS_FILE, "w+") {|f| f.puts(data.to_yaml) } # File.open(fnm, ‘w’) { |out| YAML.dump(h, out) } end |
.update(filename = CONFIG_FILE) ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/mygit.rb', line 29 def update filename = CONFIG_FILE # Get username from prompt if file missing file = File. filename conf = YAML.load_file file gh = GitHub.new(conf['user'], get_input("Enter password: ", '*')) repo = gh.repos(conf['repo']) keep = remove_keys repo save keep end |