class Lolcommits::Plugin

Attributes

options[RW]
runner[RW]

Public Class Methods

name() click to toggle source

identifying plugin name (for config, listing)

# File lib/lolcommits/plugin.rb, line 119
def self.name
  'plugin'
end
new(runner) click to toggle source
# File lib/lolcommits/plugin.rb, line 7
def initialize(runner)
  debug 'Initializing'
  self.runner = runner
  self.options = ['enabled']
end
runner_order() click to toggle source

a plugin requests to be run by the runner in one of the possible positions. valid options are [:precapture, :postcapture]

# File lib/lolcommits/plugin.rb, line 125
def self.runner_order
  nil
end

Public Instance Methods

configuration() click to toggle source
# File lib/lolcommits/plugin.rb, line 39
def configuration
  config = runner.config.read_configuration if runner
  return {} unless config
  config[self.class.name] || {}
end
configure_options!() click to toggle source

ask for plugin options

# File lib/lolcommits/plugin.rb, line 46
def configure_options!
  puts "Configuring plugin: #{self.class.name}\n"
  options.reduce({}) do |acc, option|
    if option != 'enabled'
      print "#{option}: "
      val = parse_user_input(STDIN.gets.strip)
    else
      val = true
    end

    # 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?() click to toggle source

empty plugin configuration

# File lib/lolcommits/plugin.rb, line 96
def configured?
  !configuration.empty?
end
debug(msg) click to toggle source

uniform debug logging for plugins

Calls superclass method
# File lib/lolcommits/plugin.rb, line 114
def debug(msg)
  super("Plugin: #{self.class}: " + msg)
end
enabled?() click to toggle source
# File lib/lolcommits/plugin.rb, line 81
def enabled?
  configuration['enabled'] == true
end
execute_postcapture() click to toggle source
# File lib/lolcommits/plugin.rb, line 22
def execute_postcapture
  if enabled?
    debug 'I am enabled, about to run postcapture'
    run_postcapture
  else
    debug 'Disabled, doing nothing for postcapture execution'
  end
end
execute_precapture() click to toggle source
# File lib/lolcommits/plugin.rb, line 13
def execute_precapture
  if enabled?
    debug 'I am enabled, about to run precapture'
    run_precapture
  else
    debug 'Disabled, doing nothing for precapture execution'
  end
end
log_error(e, message) click to toggle source

helper to log errors with a message via debug

# File lib/lolcommits/plugin.rb, line 108
def log_error(e, message)
  debug message
  debug e.backtrace.join("\n")
end
parse_user_input(str) click to toggle source
# File lib/lolcommits/plugin.rb, line 66
def parse_user_input(str)
  # cater for bools, strings, ints and blanks
  if 'true'.casecmp(str) == 0
    true
  elsif 'false'.casecmp(str) == 0
    false
  elsif str =~ /^[0-9]+$/
    str.to_i
  elsif str.strip.empty?
    nil
  else
    str
  end
end
puts(*args) click to toggle source

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

Calls superclass method
# File lib/lolcommits/plugin.rb, line 102
def puts(*args)
  return if runner && runner.capture_stealth
  super(args)
end
run_postcapture() click to toggle source
# File lib/lolcommits/plugin.rb, line 35
def run_postcapture
  debug 'base plugin, does nothing to anything'
end
run_precapture() click to toggle source
# File lib/lolcommits/plugin.rb, line 31
def run_precapture
  debug 'base plugin, does nothing to anything'
end
valid_configuration?() click to toggle source

check config is valid

# File lib/lolcommits/plugin.rb, line 86
def valid_configuration?
  if configured?
    true
  else
    puts "Missing #{self.class.name} config - configure with: lolcommits --config -p #{self.class.name}"
    false
  end
end