Module: Rfd::Bookmark
- Defined in:
- lib/rfd/bookmark.rb
Constant Summary collapse
- CONFIG_DIR =
File.join(ENV.fetch('XDG_CONFIG_HOME') { File.('~/.config') }, 'rfd')
- BOOKMARK_FILE =
File.join(CONFIG_DIR, 'bookmarks')
Class Attribute Summary collapse
-
.bookmarks ⇒ Object
Returns the value of attribute bookmarks.
Class Method Summary collapse
- .add(path) ⇒ Object
- .include?(path) ⇒ Boolean
- .load ⇒ Object
- .remove(path) ⇒ Object
- .save ⇒ Object
- .toggle(path) ⇒ Object
Class Attribute Details
.bookmarks ⇒ Object
Returns the value of attribute bookmarks.
11 12 13 |
# File 'lib/rfd/bookmark.rb', line 11 def bookmarks @bookmarks end |
Class Method Details
.add(path) ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/rfd/bookmark.rb', line 13 def add(path) path = File.(path) return if @bookmarks.include?(path) @bookmarks << path save end |
.include?(path) ⇒ Boolean
27 28 29 |
# File 'lib/rfd/bookmark.rb', line 27 def include?(path) @bookmarks.include?(File.(path)) end |
.load ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/rfd/bookmark.rb', line 39 def load return unless File.exist?(BOOKMARK_FILE) @bookmarks = File.readlines(BOOKMARK_FILE, chomp: true) .map { |line| File.(line) } .select { |path| File.directory?(path) } rescue Errno::EACCES, Errno::ENOENT @bookmarks = [] end |
.remove(path) ⇒ Object
21 22 23 24 25 |
# File 'lib/rfd/bookmark.rb', line 21 def remove(path) path = File.(path) @bookmarks.delete(path) save end |
.save ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/rfd/bookmark.rb', line 49 def save dir = File.dirname(BOOKMARK_FILE) FileUtils.mkdir_p(dir) unless File.directory?(dir) File.write(BOOKMARK_FILE, @bookmarks.join("\n") + "\n") rescue Errno::EACCES, Errno::ENOENT # Silently fail if we can't write end |
.toggle(path) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/rfd/bookmark.rb', line 31 def toggle(path) if include?(path) remove(path) else add(path) end end |