class Database

Public Class Methods

add(params) click to toggle source
# File lib/delicious-cli/db.rb, line 92
def self.add(params)
  @@posts << params
end
clear!() click to toggle source
# File lib/delicious-cli/db.rb, line 44
def self.clear!
  File.delete @@filename if File.exists? @@filename
  @@posts = []
end
find(words) click to toggle source
# 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!() click to toggle source
# 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) click to toggle source
# File lib/delicious-cli/db.rb, line 96
def self.last(n)
  @@posts[-n..-1]
end
most_recent_time() click to toggle source
# 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
posts() click to toggle source
# File lib/delicious-cli/db.rb, line 34
def self.posts
  @@posts
end
save!() click to toggle source
# 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) click to toggle source
# 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