class Lolcommits::Plugin::Base
Attributes
Public Class Methods
# File lib/lolcommits/plugin/base.rb, line 12 def initialize(runner: nil, name: nil, config: {}) self.runner = runner self.name = name || self.class.to_s self.configuration = config || {} self.options = [:enabled] end
Public Instance Methods
# File lib/lolcommits/plugin/base.rb, line 63 def config_option(*keys) configuration.dig(*keys) || default_options.dig(*keys) end
Prompts the user to configure all plugin options.
Available options can be defined in an Array (@options instance var) and/or a Hash (by overriding the `default_options` method).
By default (on initialize), `@options` is set with `[:enabled]`. This is mandatory since `enabled?` checks this option is true before running any capture hooks.
Using a Hash to define default options allows you to:
- including default values - define nested options, user is prompted for each nested option key
`configure_option_hash` will iterate over all options prompting the user for input and building the configuration Hash.
Lolcommits
will save this Hash to a YAML file. During the capture process the configuration is loaded, parsed and available in the plugin class as `@configuration`. Or if you want to fall back to default values, you should use `config_option` to fetch option values.
Alternatively you can override this method entirely to customise the process. A helpful `parse_user_input` method is available to help parse strings from STDIN (into boolean, integer or nil values).
@return [Hash] the configured plugin options
# File lib/lolcommits/plugin/base.rb, line 53 def configure_options! configure_option_hash( (options.map { |option| [option, nil] }.to_h).merge(default_options) ) end
uniform debug logging for plugins
# File lib/lolcommits/plugin/base.rb, line 97 def debug(msg) super("#{self.class}: " + msg) end
# File lib/lolcommits/plugin/base.rb, line 59 def default_options {} end
# File lib/lolcommits/plugin/base.rb, line 67 def enabled? configuration[:enabled] == true end
helper to log errors with a message via debug
# File lib/lolcommits/plugin/base.rb, line 91 def log_error(error, message) debug message debug error.backtrace.join("\n") end
# File lib/lolcommits/plugin/base.rb, line 84 def print(*args) return if runner&.capture_stealth super(*args) end
uniform puts and print for plugins dont puts or print if the runner wants to be silent (stealth mode)
# File lib/lolcommits/plugin/base.rb, line 78 def puts(*args) return if runner&.capture_stealth super(*args) end
# File lib/lolcommits/plugin/base.rb, line 23 def run_capture_ready; end
# File lib/lolcommits/plugin/base.rb, line 21 def run_post_capture; end
# File lib/lolcommits/plugin/base.rb, line 19 def run_pre_capture; end
check config is valid
# File lib/lolcommits/plugin/base.rb, line 72 def valid_configuration? !configuration.empty? end
Private Instance Methods
# File lib/lolcommits/plugin/base.rb, line 103 def configure_option_hash(option_hash, spacing_count = 0) option_hash.keys.reduce({}) do |acc, option| option_value = option_hash[option] prefix = ' ' * spacing_count if option_value.is_a?(Hash) puts "#{prefix}#{option}:\n" acc.merge(option => configure_option_hash(option_value, (spacing_count + 1))) else print "#{prefix}#{option.to_s.tr('_', ' ')}#{" (#{option_value})" unless option_value.nil?}: " user_value = parse_user_input(gets.chomp.strip) # if not enabled, disable and return without setting more options # useful with nested hash configs, place enabled as first sub-option # if answer is !true, no further sub-options will be prompted for return { option => false } if option == :enabled && user_value != true acc.merge(option => user_value) end end end