Sha256: 4d9ecc3fea93986d20322a510e064f3e7cb509c8d30015d04f80b80a94f5eadb

Contents?: true

Size: 867 Bytes

Versions: 4

Compression:

Stored size: 867 Bytes

Contents

module Timetrap
  module Config
    extend self
    PATH = ENV['TIMETRAP_CONFIG_FILE'] || File.join(ENV['HOME'], '.timetrap.yml')

    # Application defaults.
    #
    # These are written to a config file by invoking:
    # <code>
    # t configure
    # </code>
    def defaults
      {
        # Path to the sqlite db
        'database_file' => "#{ENV['HOME']}/.timetrap.db",
        # Unit of time for rounding (-r) in seconds
        'round_in_seconds' => 900
      }
    end

    def [](key)
      overrides = File.exist?(PATH) ? YAML.load(File.read(PATH)) : {}
      defaults.merge(overrides)[key]
    rescue => e
      puts "invalid config file"
      puts e.message
      defaults[key]
    end

    def configure!
      unless File.exist?(PATH)
        File.open(PATH, 'w') do |fh|
          fh.puts(defaults.to_yaml)
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
timetrap-1.4.1 lib/timetrap/config.rb
timetrap-1.4.0 lib/timetrap/config.rb
timetrap-1.3.0 lib/timetrap/config.rb
timetrap-1.2.1 lib/timetrap/config.rb