class Hub::GitHubAPI::FileStore
Filesystem store suitable for Configuration
Filesystem store suitable for Configuration
Public Class Methods
new(filename)
click to toggle source
# File lib/hub/github_api.rb, line 437 def initialize filename @filename = filename @data = Hash.new {|d, host| d[host] = [] } @persist_next_change = false load if File.exist? filename end
Public Instance Methods
fetch_value(host, user, key) { || ... }
click to toggle source
# File lib/hub/github_api.rb, line 444 def fetch_value host, user, key entries = get(host) entries << {} if entries.empty? entry = entries.first entry.fetch(key.to_s) { value = yield raise "no value for key :#{key}" if value.nil? || value.empty? entry[key.to_s] = value save_if_needed value } end
load()
click to toggle source
# File lib/hub/github_api.rb, line 466 def load existing_data = File.read(@filename) @data.update yaml_load(existing_data) unless existing_data.strip.empty? end
mkdir_p(dir)
click to toggle source
# File lib/hub/github_api.rb, line 476 def mkdir_p(dir) dir.split('/').inject do |parent, name| d = File.join(parent, name) Dir.mkdir(d) unless File.exist?(d) d end end
persist_next_change!()
click to toggle source
# File lib/hub/github_api.rb, line 457 def persist_next_change! @persist_next_change = true end
save()
click to toggle source
# File lib/hub/github_api.rb, line 471 def save mkdir_p File.dirname(@filename) File.open(@filename, 'w', 0600) {|f| f << yaml_dump(@data) } end
save_if_needed()
click to toggle source
# File lib/hub/github_api.rb, line 461 def save_if_needed @persist_next_change && save @persist_next_change = false end
yaml_dump(data)
click to toggle source
# File lib/hub/github_api.rb, line 504 def yaml_dump(data) yaml = ['---'] data.each do |host, values| yaml << "#{host}:" values.each do |hash| dash = '-' hash.each do |key, value| yaml << "#{dash} #{key}: #{value}" dash = ' ' end end end yaml.join("\n") end
yaml_load(string)
click to toggle source
# File lib/hub/github_api.rb, line 484 def yaml_load(string) hash = {} host = nil string.split("\n").each do |line| case line when /^---\s*$/, /^\s*(?:#|$)/ # ignore when /^(.+):\s*$/ host = hash[$1] = [] when /^([- ]) (.+?): (.+)/ key, value = $2, $3 host << {} if $1 == '-' host.last[key] = value.gsub(/^'|'$/, '') else raise "unsupported YAML line: #{line}" end end hash end