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) ⇒ Base

Returns a new instance of Base.



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

def initialize(runner)
  self.runner = runner
  self.options = ['enabled']
end

Instance Attribute Details

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



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

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.

Returns:

  • (Array)

    the position(s) (:precapture and/or :postcapture)



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

def self.runner_order
  []
end

Instance Method Details

#configurationObject



29
30
31
32
33
# File 'lib/lolcommits/plugin/base.rb', line 29

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

#configure_options!Object

ask for plugin options



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/lolcommits/plugin/base.rb', line 36

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)


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

def configured?
  !configuration.empty?
end

#debug(msg) ⇒ Object

uniform debug logging for plugins



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

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

#enabled?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/lolcommits/plugin/base.rb', line 66

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

#execute_postcaptureObject



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

def execute_postcapture
  return unless valid_configuration?
  return unless enabled?
  debug 'I am enabled, about to run postcapture'
  run_postcapture
end

#execute_precaptureObject



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

def execute_precapture
  return unless valid_configuration?
  return unless enabled?
  debug 'I am enabled, about to run precapture'
  run_precapture
end

#log_error(e, message) ⇒ Object

helper to log errors with a message via debug



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

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

#parse_user_input(str) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/lolcommits/plugin/base.rb', line 51

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

#puts(*args) ⇒ Object

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



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

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

#run_postcaptureObject



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

def run_postcapture; end

#run_precaptureObject



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

def run_precapture; end

#valid_configuration?Boolean

check config is valid

Returns:

  • (Boolean)


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

def valid_configuration?
  configured?
end