Class: Todd::Base

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/todd/config.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



15
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
# File 'lib/todd/config.rb', line 15

def initialize
  @config = {
    :default_db_path    => "/home/carl/dev/todd/test.db",
    :config_filename    => ".todd",
    :default_category   => "default",
    :default_config     => "todd_hash: %s\n",
    :output_format      => "minimal"
  }

  if File.exists? @config[:config_filename]
    begin
      conf = YAML.load(File.open(@config[:config_filename], 'r').read)
      conf.each { |key, val|
        @config[key.to_sym] = val
      } unless conf == nil
    rescue scripterror => e
      warn("error reading #{@config[:config_filename]}, you might have an error in your local .todd file.")
      puts "exception trace:"
      pp e
      exit
    end
  else
    unless ARGV.include? "init"
      warn("Error finding #{@config[:config_filename]}")
      warn("If you want to use Todd in this dir, please run todd init")
      exit
    end
  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



13
14
15
# File 'lib/todd/config.rb', line 13

def config
  @config
end

Class Method Details

.[](key) ⇒ Object



9
10
11
# File 'lib/todd/config.rb', line 9

def self.[](key)
  @__instance__.config[key]
end

.init_local_dirObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/todd/config.rb', line 45

def self.init_local_dir
  if File.exists? self[:config_filename]
    warn "Todd is already initialized in the local directory"
    return
  end

  current_dir = Dir.getwd
  current_dir_md5 = Digest::MD5.hexdigest(current_dir)
  
  TodoList.create(:md5_id => current_dir_md5)

  File.open(self[:config_filename], 'w') do |f|
    f.write(sprintf(self[:default_config], current_dir_md5))
  end

  puts "Initialized Todd in #{current_dir}"
end

.set_formatting(format) ⇒ Object



63
64
65
# File 'lib/todd/config.rb', line 63

def self.set_formatting format
  @__instance__.config[:output_format] = format
end

.setupObject



5
6
7
# File 'lib/todd/config.rb', line 5

def self.setup
  self.instance
end