Class: Hub::GitHubAPI::FileStore

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/hub/github_api.rb

Overview

Filesystem store suitable for Configuration

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ FileStore

Returns a new instance of FileStore.



251
252
253
254
255
# File 'lib/hub/github_api.rb', line 251

def initialize filename
  @filename = filename
  @data = Hash.new {|d, host| d[host] = [] }
  load if File.exist? filename
end

Instance Method Details

#entry_for_user(host, username) ⇒ Object



281
282
283
284
285
# File 'lib/hub/github_api.rb', line 281

def entry_for_user host, username
  entries = get(host)
  entries.find {|e| e['user'] == username } or
    (entries << {'user' => username}).last
end

#fetch_user(host) ⇒ Object



257
258
259
260
261
262
263
264
265
# File 'lib/hub/github_api.rb', line 257

def fetch_user host
  unless entry = get(host).first
    user = yield
    # FIXME: more elegant handling of empty strings
    return nil if user.nil? or user.empty?
    entry = entry_for_user(host, user)
  end
  entry['user']
end

#fetch_value(host, user, key) ⇒ Object



267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/hub/github_api.rb', line 267

def fetch_value host, user, key
  entry = entry_for_user host, user
  entry[key.to_s] || begin
    value = yield
    if value and !value.empty?
      entry[key.to_s] = value
      save
      value
    else
      raise "no value"
    end
  end
end

#loadObject



287
288
289
# File 'lib/hub/github_api.rb', line 287

def load
  @data.update YAML.load(File.read(@filename))
end

#saveObject



291
292
293
294
# File 'lib/hub/github_api.rb', line 291

def save
  FileUtils.mkdir_p File.dirname(@filename)
  File.open(@filename, 'w') {|f| f << YAML.dump(@data) }
end