diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/timetrackr.rb | 10 | ||||
| -rw-r--r-- | lib/timetrackr/yaml.rb | 22 |
2 files changed, 18 insertions, 14 deletions
diff --git a/lib/timetrackr.rb b/lib/timetrackr.rb index cf64d5c..75bf520 100644 --- a/lib/timetrackr.rb +++ b/lib/timetrackr.rb @@ -1,13 +1,13 @@ -autoload 'YamlTimeTracker', 'timetrackr/yaml' -autoload 'SqliteTimeTracker', 'timetrackr/sqlite' +autoload 'YamlTimeTrackr', 'timetrackr/yaml' +autoload 'SqliteTimeTrackr', 'timetrackr/sqlite' -class TimeTracker +class TimeTrackr def self.create(type,options={}) case type.to_s when 'yaml' begin require 'yaml' - log = YamlTimeTracker.new(options) + log = YamlTimeTrackr.new(options[:path]) puts 'Loaded yaml tracker' if $verbose rescue LoadError puts 'Yaml not found' @@ -15,7 +15,7 @@ class TimeTracker when 'sqlite' begin require 'sqlite3' - log = SqliteTimeTracker.new(options) + log = SqliteTimeTrackr.new(options[:path]) puts 'Loaded sqlite tracker' if $verbose rescue LoadError puts 'Sqlite not found' diff --git a/lib/timetrackr/yaml.rb b/lib/timetrackr/yaml.rb index f9dc6cb..ac5e299 100644 --- a/lib/timetrackr/yaml.rb +++ b/lib/timetrackr/yaml.rb @@ -1,12 +1,12 @@ -class YamlTimeTracker < TimeTracker +class YamlTimeTrackr < TimeTrackr - def initialize(options) - @log_path = options[:log_path] || File.join(ENV['HOME'],'.timetrackr.db') - if File.exist? @log_path - @db = YAML.load_file(@log_path) - else + def initialize(path) + @log_path = path + if !File.exist? @log_path @db = {:current => [], :tasks => {}} + write_file end + @db = YAML.load_file(@log_path) puts "Using log file '#{@log_path}'" if $verbose end @@ -48,9 +48,7 @@ class YamlTimeTracker < TimeTracker end def close - File.open(@log_path,'w') do |fh| - YAML.dump(@db,fh) - end + write_file end def clear(task) @@ -68,4 +66,10 @@ class YamlTimeTracker < TimeTracker @db[:tasks][task] = Array[details] end end + + def write_file + File.open(@log_path,'w') do |fh| + YAML.dump(@db,fh) + end + end end |
