Class: Doing::Configuration

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(file = nil, options: {}) ⇒ Configuration

Returns a new instance of Configuration.



92
93
94
95
96
97
98
99
100
101
# File 'lib/doing/configuration.rb', line 92

def initialize(file = nil, options: {})
  if file
    cf = File.expand_path(file)
    # raise MissingConfigFile, "Config not found (#{cf})" unless File.exist?(cf)

    @config_file = cf
  end

  @settings = configure(options)
end

Instance Attribute Details

#ignore_local=(value) ⇒ Object (writeonly)

Sets the attribute ignore_local

Parameters:

  • value

    the value to set the attribute ignore_local to.



10
11
12
# File 'lib/doing/configuration.rb', line 10

def ignore_local=(value)
  @ignore_local = value
end

#settingsObject (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_configsObject



103
104
105
# File 'lib/doing/configuration.rb', line 103

def additional_configs
  @additional_configs ||= find_local_config
end

#config_fileObject



144
145
146
# File 'lib/doing/configuration.rb', line 144

def config_file
  @config_file ||= File.join(Util.user_home, '.doingrc')
end

#config_file=(file) ⇒ Object



148
149
150
# File 'lib/doing/configuration.rb', line 148

def config_file=(file)
  @config_file = file
end

#configure(opt = {}) ⇒ Object

Read user configuration and merge with defaults

Parameters:

  • opt (Hash) (defaults to: {})

    Additional Options



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/doing/configuration.rb', line 157

def configure(opt = {})
  @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

#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.



140
141
142
# File 'lib/doing/configuration.rb', line 140

def from(user_config)
  Util.deep_merge_hashes(DEFAULTS, Configuration[user_config].stringify_keys)
end

#value_for_key(keypath = '') ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/doing/configuration.rb', line 107

def value_for_key(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: 2)
        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('.')}")
  result = {}
  result[real_path[-1]] = cfg
  result
end