Class: FileStorage

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFileStorage

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

#repoObject (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.expand_path 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