Class: Lolcommits::Plugin::Base

Inherits:
Object
  • Object
show all
Includes:
ConfigurationHelper
Defined in:
lib/lolcommits/plugin/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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.options = [ :enabled ]
end

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



8
9
10
# File 'lib/lolcommits/plugin/base.rb', line 8

def configuration
  @configuration
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/lolcommits/plugin/base.rb', line 8

def name
  @name
end

#optionsObject

Returns the value of attribute options.



8
9
10
# File 'lib/lolcommits/plugin/base.rb', line 8

def options
  @options
end

#runnerObject

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) || default_options.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).

Returns:

  • the configured plugin options



51
52
53
# File 'lib/lolcommits/plugin/base.rb', line 51

def configure_options!
  configure_option_hash(options.map { |option| [ option, nil ] }.to_h.merge(default_options))
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_optionsObject



55
56
57
# File 'lib/lolcommits/plugin/base.rb', line 55

def default_options
  {}
end

#enabled?Boolean

Returns:



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, message)
  debug message
  debug error.backtrace.join("\n")
end


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_readyObject



21
# File 'lib/lolcommits/plugin/base.rb', line 21

def run_capture_ready; end

#run_post_captureObject



19
# File 'lib/lolcommits/plugin/base.rb', line 19

def run_post_capture; end

#run_pre_captureObject



17
# File 'lib/lolcommits/plugin/base.rb', line 17

def run_pre_capture; end

#valid_configuration?Boolean

check config is valid

Returns:



68
69
70
# File 'lib/lolcommits/plugin/base.rb', line 68

def valid_configuration?
  !configuration.empty?
end