Class: RCV::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/rcv/config.rb

Constant Summary collapse

HOME =
ENV["HOME"] || ENV["AppData"] || "./"
CONF_DIR =
File.join(HOME, ".rcv")
BOOKMARK_PATH =
File.join(CONF_DIR, "bookmark.yaml")

Class Method Summary collapse

Class Method Details

.bookmark(key = nil) ⇒ Object



18
19
20
21
# File 'lib/rcv/config.rb', line 18

def bookmark(key = nil)
  bm = bookmarks
  bm[key] if bm && bm.key?(key)
end

.bookmark=(hash) ⇒ Object



11
12
13
14
15
16
# File 'lib/rcv/config.rb', line 11

def bookmark=(hash)
  FileUtils.mkpath(CONF_DIR) unless File.exist?(CONF_DIR)
  bm = bookmarks
  hash.update(bm) { |k, v| v } if bm
  write_bookmark hash
end

.bookmarksObject



33
34
35
# File 'lib/rcv/config.rb', line 33

def bookmarks
  YAML.load(File.read(BOOKMARK_PATH)) if File.exist?(BOOKMARK_PATH)
end

.clear_bookmarkObject



29
30
31
# File 'lib/rcv/config.rb', line 29

def clear_bookmark
  File.open(BOOKMARK_PATH, "w").close
end

.delete_bookmark(key) ⇒ Object



23
24
25
26
27
# File 'lib/rcv/config.rb', line 23

def delete_bookmark(key)
  hash = bookmarks.reject { |k, v| k == key }
  write_bookmark hash
  hash
end

.write_bookmark(hash) ⇒ Object



37
38
39
40
41
# File 'lib/rcv/config.rb', line 37

def write_bookmark(hash)
  open(BOOKMARK_PATH, "w") do |f|
    f.write hash.to_yaml
  end
end