Class: Database

Inherits:
Object
  • Object
show all
Defined in:
lib/delicious-cli/db.rb

Constant Summary collapse

@@filename =
configfile('delicious.marshal')
@@posts =
[]

Class Method Summary collapse

Class Method Details

.add(params) ⇒ Object



92
93
94
# File 'lib/delicious-cli/db.rb', line 92

def self.add(params)
  @@posts << params
end

.clear!Object



44
45
46
47
# File 'lib/delicious-cli/db.rb', line 44

def self.clear!
  File.delete @@filename if File.exists? @@filename
  @@posts = []
end

.find(words) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/delicious-cli/db.rb', line 100

def self.find(words)

  finders = words.map{|word| /#{word}/i }
  fields = %w[extended description href tag]
  
  @@posts.select do |post|
    finders.all? do |finder|
      fields.any? do |field|
        post[field].match finder
      end
    end
  end
  
end

.init!Object



38
39
40
41
42
# File 'lib/delicious-cli/db.rb', line 38

def self.init!
  $log.debug "Loading database..."
  @@posts = Marshal.load(open(@@filename)) if File.exists? @@filename
  $log.debug "done."
end

.last(n) ⇒ Object



96
97
98
# File 'lib/delicious-cli/db.rb', line 96

def self.last(n)
  @@posts[-n..-1]
end

.most_recent_timeObject



87
88
89
90
# File 'lib/delicious-cli/db.rb', line 87

def self.most_recent_time
  #@@posts.order(:time.desc).limit(1).first[:time]
  @@posts.last["time_string"]
end

.postsObject



34
35
36
# File 'lib/delicious-cli/db.rb', line 34

def self.posts
  @@posts
end

.save!Object



49
50
51
52
53
# File 'lib/delicious-cli/db.rb', line 49

def self.save!
  open(@@filename, "w") do |f|
    f.write Marshal.dump(@@posts)
  end
end

.sync(all = false) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/delicious-cli/db.rb', line 55

def self.sync(all=false)
  all = true if @@posts.empty?
  
  if all
    printflush "  |_ Retrieving all links..."
    posts = Delicious.posts_all
  else
    printflush "  |_ Retrieving new links..."
    posts = Delicious.posts_since(most_recent_time)
  end      
  
  $log.debug "sync: got #{posts.size} posts"
  
  puts " (#{posts.size} links found)"
  
  if posts.size == 0
    puts
    return
  end      
  
  printflush "  |_ Processing links..."
  posts.each { |post| post["time_string"] = post["time"]; post["time"] = DateTime.parse(post["time_string"]) }
  @@posts += posts.sort_by { |post| post["time"] }    
  puts "done!"
  
  printflush "  |_ Saving database..."
  save!
  
  puts "done!"
  puts
end