Module: Textbringer::Recentf

Defined in:
lib/textbringer/recentf.rb,
lib/textbringer/recentf/version.rb

Constant Summary collapse

VERSION =
"1"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.last_addedObject

Returns the value of attribute last_added.



17
18
19
# File 'lib/textbringer/recentf.rb', line 17

def last_added
  @last_added
end

Class Method Details

.add(path) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/textbringer/recentf.rb', line 42

def add(path)
  return if path.nil? || path.empty?
  path = File.expand_path(path)
  return if exclude_patterns.any? { |pat| pat.match?(path) }
  return if path == last_added

  list = load
  list.delete(path)
  list.unshift(path)
  save(list)
  self.last_added = path
end

.cleanupObject



55
56
57
58
59
# File 'lib/textbringer/recentf.rb', line 55

def cleanup
  list = load.select { |f| File.exist?(f) }
  save(list)
  list.size
end

.exclude_patternsObject



27
28
29
# File 'lib/textbringer/recentf.rb', line 27

def exclude_patterns
  CONFIG[:recentf_exclude_patterns]
end

.file_pathObject



19
20
21
# File 'lib/textbringer/recentf.rb', line 19

def file_path
  File.expand_path(CONFIG[:recentf_file])
end

.loadObject



31
32
33
34
# File 'lib/textbringer/recentf.rb', line 31

def load
  return [] unless File.exist?(file_path)
  File.readlines(file_path, chomp: true)
end

.max_itemsObject



23
24
25
# File 'lib/textbringer/recentf.rb', line 23

def max_items
  CONFIG[:recentf_max_items]
end

.save(list) ⇒ Object



36
37
38
39
40
# File 'lib/textbringer/recentf.rb', line 36

def save(list)
  dir = File.dirname(file_path)
  FileUtils.mkdir_p(dir) unless File.directory?(dir)
  File.write(file_path, list.take(max_items).join("\n") + "\n")
end