class Larch::Config
Constants
- DEFAULT
Attributes
filename[R]
section[R]
Public Class Methods
new(section = 'default', filename = DEFAULT['config'], override = {})
click to toggle source
# File lib/larch/config.rb, line 33 def initialize(section = 'default', filename = DEFAULT['config'], override = {}) @section = section.to_s @override = {} override.each do |k, v| opt = k.to_s.gsub('_', '-') @override[opt] = v if DEFAULT.has_key?(opt) && override["#{k}_given".to_sym] && v != DEFAULT[opt] end load_file(filename) validate end
Public Instance Methods
fetch(name)
click to toggle source
# File lib/larch/config.rb, line 46 def fetch(name) (@cached || {})[name.to_s.gsub('_', '-')] || nil end
Also aliased as: []
load_file(filename)
click to toggle source
# File lib/larch/config.rb, line 51 def load_file(filename) @filename = File.expand_path(filename) config = {} if File.exist?(@filename) begin config = YAML.load_file(@filename) rescue => e raise Larch::Config::Error, "config error in #{filename}: #{e}" end end @lookup = [@override, config[@section] || {}, config['default'] || {}, DEFAULT] cache_config end
method_missing(name)
click to toggle source
# File lib/larch/config.rb, line 68 def method_missing(name) fetch(name) end
validate()
click to toggle source
Validates the config and resolves conflicting settings.
# File lib/larch/config.rb, line 73 def validate ['from', 'to'].each do |s| raise Error, "'#{s}' must be a valid IMAP URI (e.g. imap://example.com)" unless fetch(s) =~ IMAP::REGEX_URI end unless Logger::LEVELS.has_key?(verbosity.to_sym) raise Error, "'verbosity' must be one of: #{Logger::LEVELS.keys.join(', ')}" end if exclude_file raise Error, "exclude file not found: #{exclude_file}" unless File.file?(exclude_file) raise Error, "exclude file cannot be read: #{exclude_file}" unless File.readable?(exclude_file) end if @cached['all'] || @cached['all-subscribed'] # A specific source folder wins over 'all' and 'all-subscribed' if @cached['from-folder'] @cached['all'] = false @cached['all-subscribed'] = false @cached['to-folder'] ||= @cached['from-folder'] elsif @cached['all'] && @cached['all-subscribed'] # 'all' wins over 'all-subscribed' @cached['all-subscribed'] = false end # 'no-recurse' is not compatible with 'all' and 'all-subscribed' raise Error, "'no-recurse' option cannot be used with 'all' or 'all-subscribed'" if @cached['no-recurse'] else @cached['from-folder'] ||= 'INBOX' @cached['to-folder'] ||= 'INBOX' end @cached['exclude'].flatten! end
Private Instance Methods
cache_config()
click to toggle source
Merges configs such that those earlier in the lookup chain override those later in the chain.
# File lib/larch/config.rb, line 114 def cache_config @cached = {} @lookup.reverse.each do |c| c.each {|k, v| @cached[k] = config_merge(@cached[k] || {}, v) } end end
config_merge(master, value)
click to toggle source
# File lib/larch/config.rb, line 122 def config_merge(master, value) if value.is_a?(Hash) value.each {|k, v| master[k] = config_merge(master[k] || {}, v) } return master end value end