Class: Rarity::Tracker

Inherits:
Object
  • Object
show all
Defined in:
lib/rarity/tracker.rb

Instance Method Summary collapse

Constructor Details

#initializeTracker

Returns a new instance of Tracker.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rarity/tracker.rb', line 4

def initialize
  @db_path = File.expand_path("~/.rarity.sqlite")
  @first_run = !File.exists?(@db_path)
  @db = Sequel.sqlite(@db_path)
  if @first_run
    @db.create_table :images do
      primary_key :id
      String :path
      Integer :png_o_level
      Integer :before_bytes
      Integer :after_bytes
      String :filetype
      Float :time_taken
    end
  end
  puts "Tracker initialised with a total of #{@db[:images].count} records"
end

Instance Method Details

#import_from_old_formatObject



21
22
23
24
25
26
27
28
29
# File 'lib/rarity/tracker.rb', line 21

def import_from_old_format
  @path = File.expand_path("~/.optimdone.dat")
  @done = []
  File.open(@path,'r'){|f|@done = f.read.split("\n")} rescue nil
  for r in @done
    @db[:images].insert(:path=>r, :png_o_level=>3)
  end
  puts "Imported #{@done.size} entries that we have already optimised from the old land, you can now delete ~/.optimdone.dat"
end

#is_done?(path, options) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
# File 'lib/rarity/tracker.rb', line 40

def is_done?(path, options)
  cleanpath = path.gsub("\n","").gsub("\r","").chomp
  if @db[:images].select(:id, :png_o_level).where(:path=>cleanpath).filter('png_o_level >= ?',options[:png_o_level]).count > 0
    return true
  end
  return false
end

#mark_done(path, options, results) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/rarity/tracker.rb', line 30

def mark_done(path, options, results)
  cleanpath = path.gsub("\n","").gsub("\r","").chomp
  if !is_done?(path, options)
    if @db[:images][:path=>cleanpath]
      @db[:images][:path=>cleanpath] = {:png_o_level => options[:png_o_level], :after_bytes=>results[:after], :time_taken=>(results[:time]+ @db[:images][:path=>cleanpath].time_taken)}
    else
      @db[:images].insert(:path=>cleanpath, :png_o_level=>options[:png_o_level], :before_bytes=>results[:before], :after_bytes=>results[:after], :filetype=>results[:filetype].to_s, :time_taken=>results[:time])
    end
  end
end

#resetObject



47
48
49
# File 'lib/rarity/tracker.rb', line 47

def reset
  @db[:images].delete
end