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
- #entry_for_user(host, username) ⇒ Object
- #fetch_user(host) ⇒ Object
- #fetch_value(host, user, key) ⇒ Object
-
#initialize(filename) ⇒ FileStore
constructor
A new instance of FileStore.
- #load ⇒ Object
- #save ⇒ Object
Constructor Details
#initialize(filename) ⇒ FileStore
Returns a new instance of FileStore.
285 286 287 288 289 |
# File 'lib/hub/github_api.rb', line 285 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
315 316 317 318 319 |
# File 'lib/hub/github_api.rb', line 315 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
291 292 293 294 295 296 297 298 299 |
# File 'lib/hub/github_api.rb', line 291 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
301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/hub/github_api.rb', line 301 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 |
#load ⇒ Object
321 322 323 324 |
# File 'lib/hub/github_api.rb', line 321 def load existing_data = File.read(@filename) @data.update YAML.load(existing_data) unless existing_data.strip.empty? end |
#save ⇒ Object
326 327 328 329 |
# File 'lib/hub/github_api.rb', line 326 def save FileUtils.mkdir_p File.dirname(@filename) File.open(@filename, 'w', 0600) {|f| f << YAML.dump(@data) } end |