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.



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

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



10
11
12
# File 'lib/lolcommits/plugin/base.rb', line 10

def configuration
  @configuration
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/lolcommits/plugin/base.rb', line 10

def name
  @name
end

#optionsObject

Returns the value of attribute options.



10
11
12
# File 'lib/lolcommits/plugin/base.rb', line 10

def options
  @options
end

#runnerObject

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) || 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:

  • (Hash)

    the configured plugin options



53
54
55
56
57
# 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

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



59
60
61
# File 'lib/lolcommits/plugin/base.rb', line 59

def default_options
  {}
end

#enabled?Boolean

Returns:

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


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_readyObject



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

def run_capture_ready; end

#run_post_captureObject



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

def run_post_capture; end

#run_pre_captureObject



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

def run_pre_capture; end

#valid_configuration?Boolean

check config is valid

Returns:

  • (Boolean)


72
73
74
# File 'lib/lolcommits/plugin/base.rb', line 72

def valid_configuration?
  !configuration.empty?
end