Class: Hub::GitHubAPI::FileStore
- Inherits:
-
Object
- Object
- Hub::GitHubAPI::FileStore
- Extended by:
- Forwardable
- Defined in:
- lib/hub/github_api.rb
Overview
Filesystem store suitable for Configuration
Instance Method Summary collapse
- #fetch_value(host, user, key) ⇒ Object
-
#initialize(filename) ⇒ FileStore
constructor
A new instance of FileStore.
- #load ⇒ Object
- #mkdir_p(dir) ⇒ Object
- #persist_next_change! ⇒ Object
- #save ⇒ Object
- #save_if_needed ⇒ Object
- #yaml_dump(data) ⇒ Object
- #yaml_load(string) ⇒ Object
Constructor Details
#initialize(filename) ⇒ FileStore
Returns a new instance of FileStore.
355 356 357 358 359 360 |
# File 'lib/hub/github_api.rb', line 355 def initialize filename @filename = filename @data = Hash.new {|d, host| d[host] = [] } @persist_next_change = false load if File.exist? filename end |
Instance Method Details
#fetch_value(host, user, key) ⇒ Object
362 363 364 365 366 367 368 369 370 371 372 373 |
# File 'lib/hub/github_api.rb', line 362 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 ⇒ Object
384 385 386 387 |
# File 'lib/hub/github_api.rb', line 384 def load existing_data = File.read(@filename) @data.update yaml_load(existing_data) unless existing_data.strip.empty? end |
#mkdir_p(dir) ⇒ Object
394 395 396 397 398 399 400 |
# File 'lib/hub/github_api.rb', line 394 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! ⇒ Object
375 376 377 |
# File 'lib/hub/github_api.rb', line 375 def persist_next_change! @persist_next_change = true end |
#save ⇒ Object
389 390 391 392 |
# File 'lib/hub/github_api.rb', line 389 def save mkdir_p File.dirname(@filename) File.open(@filename, 'w', 0600) {|f| f << yaml_dump(@data) } end |
#save_if_needed ⇒ Object
379 380 381 382 |
# File 'lib/hub/github_api.rb', line 379 def save_if_needed @persist_next_change && save @persist_next_change = false end |
#yaml_dump(data) ⇒ Object
422 423 424 425 426 427 428 429 430 431 432 433 434 435 |
# File 'lib/hub/github_api.rb', line 422 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) ⇒ Object
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 |
# File 'lib/hub/github_api.rb', line 402 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 |