Class: Larch::Config
- Inherits:
-
Object
- Object
- Larch::Config
- Defined in:
- lib/larch/config.rb,
lib/larch/errors.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- DEFAULT =
{ 'all' => false, 'all-subscribed' => false, 'config' => File.join('~', '.larch', 'config.yaml'), 'database' => File.join('~', '.larch', 'larch.db'), 'delete' => false, 'dry-run' => false, 'exclude' => [], 'exclude-file' => nil, 'expunge' => false, 'from' => nil, 'from-folder' => nil, # actually INBOX; see validate() 'from-pass' => nil, 'from-user' => nil, 'max-retries' => 3, 'no-create-folder' => false, 'no-recurse' => false, 'ssl-certs' => nil, 'ssl-verify' => false, 'sync-flags' => false, 'to' => nil, 'to-folder' => nil, # actually INBOX; see validate() 'to-pass' => nil, 'to-user' => nil, 'verbosity' => 'info' }.freeze
Instance Attribute Summary collapse
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#section ⇒ Object
readonly
Returns the value of attribute section.
Instance Method Summary collapse
- #fetch(name) ⇒ Object (also: #[])
-
#initialize(section = 'default', filename = , override = {}) ⇒ Config
constructor
A new instance of Config.
- #load_file(filename) ⇒ Object
- #method_missing(name) ⇒ Object
-
#validate ⇒ Object
Validates the config and resolves conflicting settings.
Constructor Details
#initialize(section = 'default', filename = , override = {}) ⇒ Config
Returns a new instance of Config.
33 34 35 36 37 38 39 40 41 42 43 44 |
# 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 |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name) ⇒ Object
68 69 70 |
# File 'lib/larch/config.rb', line 68 def method_missing(name) fetch(name) end |
Instance Attribute Details
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
4 5 6 |
# File 'lib/larch/config.rb', line 4 def filename @filename end |
#section ⇒ Object (readonly)
Returns the value of attribute section.
4 5 6 |
# File 'lib/larch/config.rb', line 4 def section @section end |
Instance Method Details
#fetch(name) ⇒ Object Also known as: []
46 47 48 |
# File 'lib/larch/config.rb', line 46 def fetch(name) (@cached || {})[name.to_s.gsub('_', '-')] || nil end |
#load_file(filename) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/larch/config.rb', line 51 def load_file(filename) @filename = File.(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 |
#validate ⇒ Object
Validates the config and resolves conflicting settings.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# 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 |