Class: Lolcommits::Plugin::Base
- Inherits:
-
Object
- Object
- Lolcommits::Plugin::Base
- Includes:
- ConfigurationHelper
- Defined in:
- lib/lolcommits/plugin/base.rb
Instance Attribute Summary collapse
-
#configuration ⇒ Object
Returns the value of attribute configuration.
-
#name ⇒ Object
Returns the value of attribute name.
-
#options ⇒ Object
Returns the value of attribute options.
-
#runner ⇒ Object
Returns the value of attribute runner.
Instance Method Summary collapse
- #config_option(*keys) ⇒ Object
-
#configure_options! ⇒ Hash
Prompts the user to configure all plugin options.
-
#debug(msg) ⇒ Object
uniform debug logging for plugins.
- #default_options ⇒ Object
- #enabled? ⇒ Boolean
-
#initialize(runner: nil, name: nil, config: {}) ⇒ Base
constructor
A new instance of Base.
-
#log_error(error, message) ⇒ Object
helper to log errors with a message via debug.
- #print(*args) ⇒ Object
-
#puts(*args) ⇒ Object
uniform puts and print for plugins dont puts or print if the runner wants to be silent (stealth mode).
- #run_capture_ready ⇒ Object
- #run_post_capture ⇒ Object
- #run_pre_capture ⇒ Object
-
#valid_configuration? ⇒ Boolean
check config is valid.
Methods included from ConfigurationHelper
#parse_user_input, #prompt_autocomplete_hash
Constructor Details
#initialize(runner: nil, name: nil, config: {}) ⇒ Base
Returns a new instance of Base.
12 13 14 15 16 17 |
# 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. = [:enabled] end |
Instance Attribute Details
#configuration ⇒ Object
Returns the value of attribute configuration.
10 11 12 |
# File 'lib/lolcommits/plugin/base.rb', line 10 def configuration @configuration end |
#name ⇒ Object
Returns the value of attribute name.
10 11 12 |
# File 'lib/lolcommits/plugin/base.rb', line 10 def name @name end |
#options ⇒ Object
Returns the value of attribute options.
10 11 12 |
# File 'lib/lolcommits/plugin/base.rb', line 10 def @options end |
#runner ⇒ Object
Returns the value of attribute runner.
10 11 12 |
# File 'lib/lolcommits/plugin/base.rb', line 10 def runner @runner end |
Instance Method Details
#config_option(*keys) ⇒ Object
63 64 65 |
# File 'lib/lolcommits/plugin/base.rb', line 63 def config_option(*keys) configuration.dig(*keys) || .dig(*keys) end |
#configure_options! ⇒ Hash
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).
53 54 55 56 57 |
# File 'lib/lolcommits/plugin/base.rb', line 53 def configure_option_hash( .map { |option| [option, nil] }.to_h.merge() ) end |
#debug(msg) ⇒ Object
uniform debug logging for plugins
97 98 99 |
# File 'lib/lolcommits/plugin/base.rb', line 97 def debug(msg) super("#{self.class}: " + msg) end |
#default_options ⇒ Object
59 60 61 |
# File 'lib/lolcommits/plugin/base.rb', line 59 def {} end |
#enabled? ⇒ Boolean
67 68 69 |
# File 'lib/lolcommits/plugin/base.rb', line 67 def enabled? configuration[:enabled] == true end |
#log_error(error, message) ⇒ Object
helper to log errors with a message via debug
91 92 93 94 |
# File 'lib/lolcommits/plugin/base.rb', line 91 def log_error(error, ) debug debug error.backtrace.join("\n") end |
#print(*args) ⇒ Object
84 85 86 87 88 |
# File 'lib/lolcommits/plugin/base.rb', line 84 def print(*args) return if runner&.capture_stealth super end |
#puts(*args) ⇒ Object
uniform puts and print for plugins dont puts or print if the runner wants to be silent (stealth mode)
78 79 80 81 82 |
# File 'lib/lolcommits/plugin/base.rb', line 78 def puts(*args) return if runner&.capture_stealth super end |
#run_capture_ready ⇒ Object
23 |
# File 'lib/lolcommits/plugin/base.rb', line 23 def run_capture_ready; end |
#run_post_capture ⇒ Object
21 |
# File 'lib/lolcommits/plugin/base.rb', line 21 def run_post_capture; end |
#run_pre_capture ⇒ Object
19 |
# File 'lib/lolcommits/plugin/base.rb', line 19 def run_pre_capture; end |
#valid_configuration? ⇒ Boolean
check config is valid
72 73 74 |
# File 'lib/lolcommits/plugin/base.rb', line 72 def valid_configuration? !configuration.empty? end |