Class: Lolcommits::Plugin::Base

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(runner: nil, config: nil) ⇒ Base

Returns a new instance of Base.



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

def initialize(runner: nil, config: nil)
  self.runner = runner
  self.config = config || runner.config
  self.options = ['enabled']
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



4
5
6
# File 'lib/lolcommits/plugin/base.rb', line 4

def config
  @config
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/lolcommits/plugin/base.rb', line 4

def options
  @options
end

#runnerObject

Returns the value of attribute runner.



4
5
6
# File 'lib/lolcommits/plugin/base.rb', line 4

def runner
  @runner
end

Class Method Details

.nameObject

identifying plugin name (for config, listing)



115
116
117
# File 'lib/lolcommits/plugin/base.rb', line 115

def self.name
  'plugin'
end

.runner_orderArray

Returns position(s) of when a plugin should run during the capture process.

Defines when the plugin will execute in the capture process. This must be defined, if the method returns nil, or [] the plugin will never run. Three hook positions exist, your plugin code can execute in one or more of these.

:capture_ready)

Returns:

  • (Array)

    the position(s) (:pre_capture, :post_capture,



130
131
132
# File 'lib/lolcommits/plugin/base.rb', line 130

def self.runner_order
  []
end

Instance Method Details

#configurationObject



36
37
38
39
40
# File 'lib/lolcommits/plugin/base.rb', line 36

def configuration
  saved_config = config.read_configuration
  return {} unless saved_config
  saved_config[self.class.name] || {}
end

#configure_options!Object

ask for plugin options



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/lolcommits/plugin/base.rb', line 43

def configure_options!
  puts "Configuring plugin: #{self.class.name}\n"
  options.reduce({}) do |acc, option|
    print "#{option}: "
    val = parse_user_input(gets.strip)
    # check enabled option isn't a String
    if (option == 'enabled') && ![true, false].include?(val)
      puts "Aborting - please respond with 'true' or 'false'"
      exit 1
    else
      acc.merge(option => val)
    end
  end
end

#configured?Boolean

empty plugin configuration

Returns:

  • (Boolean)


87
88
89
# File 'lib/lolcommits/plugin/base.rb', line 87

def configured?
  !configuration.empty?
end

#configured_and_enabled?Boolean

Returns:

  • (Boolean)


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

def configured_and_enabled?
  valid_configuration? && enabled?
end

#debug(msg) ⇒ Object

uniform debug logging for plugins



110
111
112
# File 'lib/lolcommits/plugin/base.rb', line 110

def debug(msg)
  super("#{self.class}: " + msg)
end

#enabled?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/lolcommits/plugin/base.rb', line 77

def enabled?
  configuration['enabled'] == true
end

#execute_capture_readyObject



24
25
26
27
28
# File 'lib/lolcommits/plugin/base.rb', line 24

def execute_capture_ready
  return unless configured_and_enabled?
  debug 'I am enabled, about to run capture ready'
  run_capture_ready
end

#execute_post_captureObject



18
19
20
21
22
# File 'lib/lolcommits/plugin/base.rb', line 18

def execute_post_capture
  return unless configured_and_enabled?
  debug 'I am enabled, about to run post capture'
  run_post_capture
end

#execute_pre_captureObject



12
13
14
15
16
# File 'lib/lolcommits/plugin/base.rb', line 12

def execute_pre_capture
  return unless configured_and_enabled?
  debug 'I am enabled, about to run pre capture'
  run_pre_capture
end

#log_error(e, message) ⇒ Object

helper to log errors with a message via debug



104
105
106
107
# File 'lib/lolcommits/plugin/base.rb', line 104

def log_error(e, message)
  debug message
  debug e.backtrace.join("\n")
end

#parse_user_input(str) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/lolcommits/plugin/base.rb', line 58

def parse_user_input(str)
  # cater for bools, strings, ints and blanks
  if 'true'.casecmp(str).zero?
    true
  elsif 'false'.casecmp(str).zero?
    false
  elsif str =~ /^[0-9]+$/
    str.to_i
  elsif str.strip.empty?
    nil
  else
    str
  end
end


98
99
100
101
# File 'lib/lolcommits/plugin/base.rb', line 98

def print(args)
  return if runner && runner.capture_stealth
  super(args)
end

#puts(*args) ⇒ Object

uniform puts and print for plugins dont puts or print if the runner wants to be silent (stealth mode)



93
94
95
96
# File 'lib/lolcommits/plugin/base.rb', line 93

def puts(*args)
  return if runner && runner.capture_stealth
  super(args)
end

#run_capture_readyObject



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

def run_capture_ready; end

#run_post_captureObject



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

def run_post_capture; end

#run_pre_captureObject



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

def run_pre_capture; end

#valid_configuration?Boolean

check config is valid

Returns:

  • (Boolean)


82
83
84
# File 'lib/lolcommits/plugin/base.rb', line 82

def valid_configuration?
  configured?
end