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.
271 272 273 274 275 |
# File 'lib/hub/github_api.rb', line 271 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
301 302 303 304 305 |
# File 'lib/hub/github_api.rb', line 301 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
277 278 279 280 281 282 283 284 285 |
# File 'lib/hub/github_api.rb', line 277 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
287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/hub/github_api.rb', line 287 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
307 308 309 310 |
# File 'lib/hub/github_api.rb', line 307 def load existing_data = File.read(@filename) @data.update YAML.load(existing_data) unless existing_data.strip.empty? end |
#save ⇒ Object
312 313 314 315 |
# File 'lib/hub/github_api.rb', line 312 def save FileUtils.mkdir_p File.dirname(@filename) File.open(@filename, 'w', 0600) {|f| f << YAML.dump(@data) } end |