Module: BillTrap::Config

Extended by:
Config
Included in:
Config
Defined in:
lib/billtrap/config.rb

Constant Summary collapse

CONFIG_PATH =
File.join(BILLTRAP_HOME, 'billtrap.yml')

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/billtrap/config.rb', line 44

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

#configure!Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/billtrap/config.rb', line 57

def configure!
  configs = if File.exist?(CONFIG_PATH)
    defaults.merge(YAML.load_file(CONFIG_PATH))
  else
    defaults
  end
  File.open(CONFIG_PATH, 'w') do |fh|
    fh.puts(configs.to_yaml)
  end
end

#defaultsObject

Application defaults.

These are written to BILLTRAP_CONFIG_PATH or <HOME>/.billtrap/billtrap.yml by executing billtrap configure



13
14
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
# File 'lib/billtrap/config.rb', line 13

def defaults
  {
    # Database identifier, defaults to 'sqlite://<BILLTRAP_HOME>/.billtrap.db'
    'database' => "sqlite://#{BILLTRAP_HOME}/billtrap.db",
    # Timetrap database, used to import Entries
    'timetrap_database' => "sqlite://#{ENV['HOME']}/.timetrap.db",
    # We'll also need the round specifier
    'round_in_seconds' => 900,
# Path to invoice archive
'billtrap_archive' => "#{ENV['HOME']}/Documents/billtrap/invoices",
    # Currency to use (see RubyMoney for codes)
    'currency' => 'USD',
    # Default rate in the above currency
    'default_rate' => '25.00',
# Invoice numbering scheme
    # TODO possible values
'invoice_number_format' => "%Y%m%d_%{invoice_id}",
    # Money formatter
    # See http://rubydoc.info/gems/money/Money/Formatting for options
    'currency_format' =>  { :with_currency => true , :symbol => false},
    # Date output format
    'date_format' => '%Y-%m-%d',
# Due date in days
'due_date' => 30,
    # Default invoice adapter to use when none is specified
    'default_formatter' => 'serenity',
    # Serenity adapter: Path to invoice template
    'serenity_template' => "#{BILLTRAP_HOME}/.billtrap_template.odt",
  }
end

#erb_render(content) ⇒ Object



53
54
55
# File 'lib/billtrap/config.rb', line 53

def erb_render(content)
  ERB.new(content).result
end