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.
10 11 12 13 14 15 |
# File 'lib/lolcommits/plugin/base.rb', line 10 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.
8 9 10 |
# File 'lib/lolcommits/plugin/base.rb', line 8 def configuration @configuration end |
#name ⇒ Object
Returns the value of attribute name.
8 9 10 |
# File 'lib/lolcommits/plugin/base.rb', line 8 def name @name end |
#options ⇒ Object
Returns the value of attribute options.
8 9 10 |
# File 'lib/lolcommits/plugin/base.rb', line 8 def end |
#runner ⇒ Object
Returns the value of attribute runner.
8 9 10 |
# File 'lib/lolcommits/plugin/base.rb', line 8 def runner @runner end |
Instance Method Details
#config_option(*keys) ⇒ Object
59 60 61 |
# File 'lib/lolcommits/plugin/base.rb', line 59 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).
51 52 53 |
# File 'lib/lolcommits/plugin/base.rb', line 51 def configure_option_hash(.map { |option| [ option, nil ] }.to_h.merge()) end |
#debug(msg) ⇒ Object
uniform debug logging for plugins
93 94 95 |
# File 'lib/lolcommits/plugin/base.rb', line 93 def debug(msg) super("#{self.class}: " + msg) end |
#default_options ⇒ Object
55 56 57 |
# File 'lib/lolcommits/plugin/base.rb', line 55 def {} end |
#enabled? ⇒ Boolean
63 64 65 |
# File 'lib/lolcommits/plugin/base.rb', line 63 def enabled? configuration[:enabled] == true end |
#log_error(error, message) ⇒ Object
helper to log errors with a message via debug
87 88 89 90 |
# File 'lib/lolcommits/plugin/base.rb', line 87 def log_error(error, ) debug debug error.backtrace.join("\n") end |
#print(*args) ⇒ Object
80 81 82 83 84 |
# File 'lib/lolcommits/plugin/base.rb', line 80 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)
74 75 76 77 78 |
# File 'lib/lolcommits/plugin/base.rb', line 74 def puts(*args) return if runner&.capture_stealth super end |
#run_capture_ready ⇒ Object
21 |
# File 'lib/lolcommits/plugin/base.rb', line 21 def run_capture_ready; end |
#run_post_capture ⇒ Object
19 |
# File 'lib/lolcommits/plugin/base.rb', line 19 def run_post_capture; end |
#run_pre_capture ⇒ Object
17 |
# File 'lib/lolcommits/plugin/base.rb', line 17 def run_pre_capture; end |
#valid_configuration? ⇒ Boolean
check config is valid
68 69 70 |
# File 'lib/lolcommits/plugin/base.rb', line 68 def valid_configuration? !configuration.empty? end |