Class: VimGet::PathDB

Inherits:
Object
  • Object
show all
Defined in:
lib/vimget/db.rb

Constant Summary collapse

@@EXT =
".manifest"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent_path = "") ⇒ PathDB

Returns a new instance of PathDB.



22
23
24
25
26
27
# File 'lib/vimget/db.rb', line 22

def initialize(parent_path = "")
  parent_path = VimGet.configure.base_dir if parent_path.empty?
  
  @db_path = File.expand_path(File.join(parent_path, "db"))
  Dir.mkdir(@db_path) if !File.exist?(@db_path)
end

Instance Attribute Details

#db_pathObject (readonly)

Returns the value of attribute db_path.



20
21
22
# File 'lib/vimget/db.rb', line 20

def db_path
  @db_path
end

Instance Method Details

#add(script) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/vimget/db.rb', line 29

def add(script)
  file_path = db_file(script)
  File.open(file_path, 'w') do |f|
    f.truncate(0)
    
    ctx = "# this file is automatical generate by vim-get. Do not motifiy it manualy.\n \nname \#{script.name}\nid \#{script.sid}\nauthor \#{script.author}\ninstalled  \#{script.installed}\nversion \#{script.version}\ndownload \#{script.download}\n\nmanifest {\n\#{script.manifest.dup.collect! {|l| l = \"  \#{l}\\n\"}}}\n\n    SCRIPT\n    f.write(ctx)\n  end\nend\n" 

#find(name) ⇒ Object



95
96
97
# File 'lib/vimget/db.rb', line 95

def find(name)
  search(name).first
end

#find_with_id(sid) ⇒ Object



90
91
92
93
# File 'lib/vimget/db.rb', line 90

def find_with_id(sid)
  s = search { |script| script.sid == sid }
  s.first
end

#installed_scriptsObject



103
104
105
# File 'lib/vimget/db.rb', line 103

def installed_scripts
  search { |s| !s.installed.empty? }
end

#outdated_scriptsObject



99
100
101
# File 'lib/vimget/db.rb', line 99

def outdated_scripts
  search { |s| s.outdated? }
end

#remove(script) ⇒ Object



63
64
65
66
# File 'lib/vimget/db.rb', line 63

def remove(script)
  file_path = db_file(script)
  File.delete(file_path)
end

#search(pattern = "") ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/vimget/db.rb', line 68

def search(pattern = "")
  file_list = Array.new
  glob_pattern = (pattern.nil? or pattern.empty?) ? "*" : "*#{pattern}*"
  
  glob_pattern = File.expand_path(File.join(@db_path, glob_pattern + ".manifest"))
  file_list = Dir.glob(glob_pattern)
 
  ret = Array.new
  
  file_list.each do |f|
    script = parse_script(f)
    
    if block_given?
      ret << script if yield(script)
    else
      ret << script
    end
  end

  ret
end

#update(script) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/vimget/db.rb', line 53

def update(script)
  if script.manifest.empty?
    filepath = db_file(script)
    s = parse_script(filepath)
    script.manifest = s.manifest if s
  end
  
  add(script)
end