Class: Todd::DB

Inherits:
Object
  • Object
show all
Defined in:
lib/todd/model.rb

Class Method Summary collapse

Class Method Details

.find(query, category_ids, archived = false) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/todd/model.rb', line 16

def self.find query, category_ids, archived = false
  found = []
  query ||= 'last'

  case query
    when 'all'
      found = Task.find(:all, :conditions => {:category_id => category_ids, :archived => archived})
    when 'last'
      found << Task.find(:first, :conditions => {:category_id => category_ids, :archived => archived}, :order => "updated_at DESC")
    when 'active', 'running', 'started'
      found = Task.find(:all, :conditions => {:category_id => category_ids, :running => true, :archived => archived})
    when 'inactive', 'stopped'
      found = Task.find(:all, :conditions => {:category_id => category_ids, :running => false, :archived => archived})
    when /(\d+)\.\.(\d+)/
      found = Task.find(:all, :conditions => {:category_id => category_ids, :id => $1.to_i..$2.to_i, :archived => archived})
    when /(\d+)/
      found << Task.find($1.to_i, :conditions => {:category_id => category_ids, :archived => archived})
    else
      warn "Invalid query: #{query}"
      exit
  end
  
  raise ActiveRecord::RecordNotFound if found == [nil]

  if block_given?
    found.each do |f|
      yield f
    end
  end

  found
rescue ActiveRecord::RecordNotFound => e
  puts "Not task matched query"
end

.get_current_listObject



12
13
14
# File 'lib/todd/model.rb', line 12

def self.get_current_list
  TodoList.first(:conditions => { :md5_id => Base[:todd_hash] })
end

.initialize_dbObject



5
6
7
8
9
10
# File 'lib/todd/model.rb', line 5

def self.initialize_db
  conf_path = File.dirname(__FILE__) + '/../../db/database.yml'
  ActiveRecord::Base.establish_connection(YAML::load(File.open(conf_path)))
  ActiveRecord::Base.logger = Logger.new(File.open('logs/database.log', 'a'))
  ActiveRecord::Base.colorize_logging = false
end