Class: Doing::Configuration
- Inherits:
-
Object
- Object
- Doing::Configuration
- Defined in:
- lib/doing/configuration.rb
Overview
Configuration object
Constant Summary collapse
- MissingConfigFile =
Class.new(RuntimeError)
- DEFAULTS =
{ 'autotag' => { 'whitelist' => [], 'synonyms' => {} }, 'editors' => { 'default' => ENV['DOING_EDITOR'] || ENV['GIT_EDITOR'] || ENV['EDITOR'], 'doing_file' => nil, 'config' => nil }, 'plugins' => { 'plugin_path' => File.join(Util.user_home, '.config', 'doing', 'plugins'), 'command_path' => File.join(Util.user_home, '.config', 'doing', 'commands') }, 'doing_file' => '~/what_was_i_doing.md', 'current_section' => 'Currently', 'paginate' => false, 'never_time' => [], 'never_finish' => [], 'timer_format' => 'text', 'templates' => { 'default' => { 'date_format' => '%Y-%m-%d %H:%M', 'template' => '%date | %title%note', 'wrap_width' => 0, 'order' => 'asc' }, 'today' => { 'date_format' => '%_I:%M%P', 'template' => '%date: %title %interval%note', 'wrap_width' => 0, 'order' => 'asc' }, 'last' => { 'date_format' => '%-I:%M%P on %a', 'template' => '%title (at %date)%odnote', 'wrap_width' => 88 }, 'recent' => { 'date_format' => '%_I:%M%P', 'template' => '%shortdate: %title (%section)', 'wrap_width' => 88, 'count' => 10, 'order' => 'asc' } }, 'export_templates' => {}, 'views' => { 'done' => { 'date_format' => '%_I:%M%P', 'template' => '%date | %title%note', 'wrap_width' => 0, 'section' => 'All', 'count' => 0, 'order' => 'desc', 'tags' => 'done complete cancelled', 'tags_bool' => 'OR' }, 'color' => { 'date_format' => '%F %_I:%M%P', 'template' => '%boldblack%date %boldgreen| %boldwhite%title%default%note', 'wrap_width' => 0, 'section' => 'Currently', 'count' => 10, 'order' => 'asc' } }, 'marker_tag' => 'flagged', 'marker_color' => 'red', 'default_tags' => [], 'tag_sort' => 'name', 'include_notes' => true }
Instance Attribute Summary collapse
-
#ignore_local ⇒ Object
writeonly
Sets the attribute ignore_local.
-
#settings ⇒ Object
readonly
Returns the value of attribute settings.
Instance Method Summary collapse
- #additional_configs ⇒ Object
- #choose_config ⇒ Object
- #config_dir ⇒ Object
- #config_file ⇒ Object
- #config_file=(file) ⇒ Object
-
#configure(opt = {}) ⇒ Object
Read user configuration and merge with defaults.
- #default_config_file ⇒ Object
-
#from(user_config) ⇒ Object
It takes the input, fills in the defaults where values do not exist.
-
#initialize(file = nil, options: {}) ⇒ Configuration
constructor
A new instance of Configuration.
- #resolve_key_path(keypath) ⇒ Object
- #update_deprecated_config ⇒ Object
- #value_for_key(keypath = '') ⇒ Object
Constructor Details
#initialize(file = nil, options: {}) ⇒ Configuration
Returns a new instance of Configuration.
92 93 94 95 96 |
# File 'lib/doing/configuration.rb', line 92 def initialize(file = nil, options: {}) @config_file = file.nil? ? default_config_file : File.(file) @settings = configure() end |
Instance Attribute Details
#ignore_local=(value) ⇒ Object (writeonly)
Sets the attribute ignore_local
10 11 12 |
# File 'lib/doing/configuration.rb', line 10 def ignore_local=(value) @ignore_local = value end |
#settings ⇒ Object (readonly)
Returns the value of attribute settings.
8 9 10 |
# File 'lib/doing/configuration.rb', line 8 def settings @settings end |
Instance Method Details
#additional_configs ⇒ Object
124 125 126 |
# File 'lib/doing/configuration.rb', line 124 def additional_configs @additional_configs ||= find_local_config end |
#choose_config ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/doing/configuration.rb', line 128 def choose_config if @additional_configs.count.positive? choices = [@config_file] choices.concat(@additional_configs) res = Doing::WWID.new.choose_from(choices.uniq.sort.reverse, sorted: false, prompt: 'Local configs found, select which to update > ') raise UserCancelled, 'Cancelled' unless res res.strip || @config_file else @config_file end end |
#config_dir ⇒ Object
102 103 104 105 |
# File 'lib/doing/configuration.rb', line 102 def config_dir @config_dir ||= File.join(Util.user_home, '.config', 'doing') # @config_dir ||= Util.user_home end |
#config_file ⇒ Object
98 99 100 |
# File 'lib/doing/configuration.rb', line 98 def config_file @config_file ||= default_config_file end |
#config_file=(file) ⇒ Object
119 120 121 |
# File 'lib/doing/configuration.rb', line 119 def config_file=(file) @config_file = file end |
#configure(opt = {}) ⇒ Object
Read user configuration and merge with defaults
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/doing/configuration.rb', line 225 def configure(opt = {}) update_deprecated_config if config_file == default_config_file @ignore_local = opt[:ignore_local] if opt[:ignore_local] config = read_config.dup plugin_config = Util.deep_merge_hashes(DEFAULTS['plugins'], config['plugins'] || {}) load_plugins(plugin_config['plugin_path']) Plugins.plugins.each do |_type, plugins| plugins.each do |title, plugin| plugin_config[title] = plugin[:config] if plugin[:config] && !plugin[:config].empty? config['export_templates'][title] ||= nil if plugin[:templates] && !plugin[:templates].empty? end end config = Util.deep_merge_hashes({ 'plugins' => plugin_config }, config) config = find_deprecations(config) if !File.exist?(config_file) || opt[:rewrite] Util.write_to_file(config_file, YAML.dump(config), backup: true) Doing.logger.warn('Config:', "Config file written to #{config_file}") end Hooks.trigger :post_config, self # config = local_config.deep_merge(config) unless @ignore_local config = Util.deep_merge_hashes(config, local_config) unless @ignore_local Hooks.trigger :post_local_config, self config end |
#default_config_file ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/doing/configuration.rb', line 107 def default_config_file raise DoingRuntimeError, "#{config_dir} exists but is not a directory" if File.exist?(config_dir) && !File.directory?(config_dir) unless File.exist?(config_dir) FileUtils.mkdir_p(config_dir) Doing.logger.log_now(:warn, "Config directory created at #{config_dir}") end # File.join(config_dir, 'config.yml') File.join(config_dir, 'config.yml') end |
#from(user_config) ⇒ Object
It takes the input, fills in the defaults where values do not exist.
user_config - a Hash or Configuration of overrides.
Returns a Configuration filled with defaults.
187 188 189 |
# File 'lib/doing/configuration.rb', line 187 def from(user_config) Util.deep_merge_hashes(DEFAULTS, Configuration[user_config].stringify_keys) end |
#resolve_key_path(keypath) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/doing/configuration.rb', line 142 def resolve_key_path(keypath) cfg = @settings real_path = [] unless keypath =~ /^[.*]?$/ paths = keypath.split(/[:.]/) while paths.length.positive? && !cfg.nil? path = paths.shift new_cfg = nil cfg.each do |key, val| next unless key =~ path.to_rx(distance: 4) real_path << key new_cfg = val break end if new_cfg.nil? Doing.logger.error("Key match not found: #{path}") break end cfg = new_cfg end end Doing.logger.debug('Config:', "translated key path #{keypath} to #{real_path.join('.')}") real_path end |
#update_deprecated_config ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/doing/configuration.rb', line 191 def update_deprecated_config # return # Until further notice return if File.exist?(default_config_file) old_file = File.join(Util.user_home, '.doingrc') return unless File.exist?(old_file) wwid = Doing::WWID.new Doing.logger.log_now(:warn, 'Deprecated:', "main config file location has changed to #{config_file}") res = wwid.yn("Move #{old_file} to new location, preserving settings?", default_response: true) if res if File.exist?(default_config_file) res = wwid.yn("#{default_config_file} already exists, overwrite it?", default_response: false) unless res @config_file = old_file return end end FileUtils.mv old_file, default_config_file, force: true Doing.logger.log_now(:warn, 'Config:', "Config file moved to #{default_config_file}") Doing.logger.log_now(:warn, 'Config:', %(If ~/.config/doing/config.yml exists in the future, it will be considered a local config and its values will override the default configuration.)) Process.exit 0 end end |
#value_for_key(keypath = '') ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/doing/configuration.rb', line 169 def value_for_key(keypath = '') cfg = @settings real_path = ['config'] unless keypath =~ /^[.*]?$/ real_path = resolve_key_path(keypath) return nil unless real_path && real_path.count.positive? cfg = cfg.dig(*real_path) end cfg.nil? ? nil : { real_path[-1] => cfg } end |