Class: LedgerWeb::Config
- Inherits:
-
Object
- Object
- LedgerWeb::Config
- Defined in:
- lib/ledger_web/config.rb
Constant Summary collapse
- @@should_load_user_config =
true
- @@instance =
nil
Instance Attribute Summary collapse
-
#hooks ⇒ Object
readonly
Returns the value of attribute hooks.
-
#vars ⇒ Object
readonly
Returns the value of attribute vars.
Class Method Summary collapse
- .from_file(filename) ⇒ Object
- .instance ⇒ Object
- .should_load_user_config ⇒ Object
- .should_load_user_config=(val) ⇒ Object
Instance Method Summary collapse
- #_add_hook(phase, hook) ⇒ Object
- #add_hook(phase, &block) ⇒ Object
- #get(key) ⇒ Object
-
#initialize ⇒ Config
constructor
A new instance of Config.
- #load_user_config(user_dir) ⇒ Object
- #override_with(config) ⇒ Object
- #run_hooks(phase, data) ⇒ Object
- #set(key, value) ⇒ Object
Constructor Details
#initialize ⇒ Config
Returns a new instance of Config.
17 18 19 20 21 22 23 24 |
# File 'lib/ledger_web/config.rb', line 17 def initialize @vars = {} @hooks = {} if block_given? yield self end end |
Instance Attribute Details
#hooks ⇒ Object (readonly)
Returns the value of attribute hooks.
4 5 6 |
# File 'lib/ledger_web/config.rb', line 4 def hooks @hooks end |
#vars ⇒ Object (readonly)
Returns the value of attribute vars.
4 5 6 |
# File 'lib/ledger_web/config.rb', line 4 def vars @vars end |
Class Method Details
.from_file(filename) ⇒ Object
82 83 84 85 86 |
# File 'lib/ledger_web/config.rb', line 82 def self.from_file(filename) File.open(filename) do |file| return eval(file.read, nil, filename) end end |
.instance ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/ledger_web/config.rb', line 88 def self.instance @@instance ||= LedgerWeb::Config.new do |config| config.set :database_url, "postgres://localhost/ledger" config.set :port, "9090" config.set :ledger_file, ENV['LEDGER_FILE'] config.set :report_directories, ["#{File.dirname(__FILE__)}/reports"] config.set :session_secret, 'SomethingSecretThisWayPassed' config.set :session_expire, 60*60 config.set :watch_interval, 5 config.set :watch_stable_count, 3 config.set :ledger_bin_path, "ledger" config.set :ledger_format, "%(quoted(xact.beg_line)),%(quoted(date)),%(quoted(payee)),%(quoted(account)),%(quoted(commodity)),%(quoted(quantity(scrub(display_amount)))),%(quoted(cleared)),%(quoted(virtual)),%(quoted(join(note | xact.note))),%(quoted(cost))\n" config.set :price_lookup_skip_symbols, ['$'] func = Proc.new do |symbol, min_date, max_date| LedgerWeb::YahooPriceLookup.new(symbol, min_date, max_date).lookup end config.set :price_function, func end end |
.should_load_user_config ⇒ Object
9 10 11 |
# File 'lib/ledger_web/config.rb', line 9 def self.should_load_user_config @@should_load_user_config end |
.should_load_user_config=(val) ⇒ Object
13 14 15 |
# File 'lib/ledger_web/config.rb', line 13 def self.should_load_user_config=(val) @@should_load_user_config = val end |
Instance Method Details
#_add_hook(phase, hook) ⇒ Object
38 39 40 41 |
# File 'lib/ledger_web/config.rb', line 38 def _add_hook(phase, hook) @hooks[phase] ||= [] @hooks[phase] << hook end |
#add_hook(phase, &block) ⇒ Object
34 35 36 |
# File 'lib/ledger_web/config.rb', line 34 def add_hook(phase, &block) _add_hook(phase, block) end |
#get(key) ⇒ Object
30 31 32 |
# File 'lib/ledger_web/config.rb', line 30 def get(key) @vars[key] end |
#load_user_config(user_dir) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/ledger_web/config.rb', line 64 def load_user_config(user_dir) if LedgerWeb::Config.should_load_user_config && File.directory?(user_dir) if File.directory? "#{user_dir}/reports" dirs = self.get(:report_directories) dirs.unshift "#{user_dir}/reports" self.set :report_directories, dirs end if File.directory? "#{user_dir}/migrate" self.set :user_migrate_dir, "#{user_dir}/migrate" end if File.exists? "#{user_dir}/config.rb" self.override_with(LedgerWeb::Config.from_file("#{user_dir}/config.rb")) end end end |
#override_with(config) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/ledger_web/config.rb', line 52 def override_with(config) config.vars.each do |key, value| set key, value end config.hooks.each do |phase, hooks| hooks.each do |hook| _add_hook phase, hook end end end |
#run_hooks(phase, data) ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/ledger_web/config.rb', line 43 def run_hooks(phase, data) if @hooks.has_key? phase @hooks[phase].each do |hook| hook.call(data) end end return data end |
#set(key, value) ⇒ Object
26 27 28 |
# File 'lib/ledger_web/config.rb', line 26 def set(key, value) @vars[key] = value end |