Class: Lolcommits::Plugin

Inherits:
Object
  • Object
show all
Includes:
Methadone::CLILogging
Defined in:
lib/lolcommits/plugin.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(runner) ⇒ Plugin

Returns a new instance of Plugin.



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

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

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#runnerObject

Returns the value of attribute runner.



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

def runner
  @runner
end

Class Method Details

.nameObject

identifying plugin name (for config, listing)



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

def self.name
  'plugin'
end

.runner_orderObject

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



121
122
123
# File 'lib/lolcommits/plugin.rb', line 121

def self.runner_order
  nil
end

Instance Method Details

#configurationObject



40
41
42
43
44
# File 'lib/lolcommits/plugin.rb', line 40

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

#configure_options!Object

ask for plugin options



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/lolcommits/plugin.rb', line 47

def configure_options!
  puts "Configuring plugin: #{self.class.name}\n"
  options.reduce({}) do |acc, option|
    print "#{option}: "
    val = parse_user_input(STDIN.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)


92
93
94
# File 'lib/lolcommits/plugin.rb', line 92

def configured?
  !configuration.empty?
end

#debug(msg) ⇒ Object

uniform debug logging for plugins



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

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

#enabled?Boolean

Returns:

  • (Boolean)


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

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

#execute_postcaptureObject



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

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_precaptureObject



14
15
16
17
18
19
20
21
# File 'lib/lolcommits/plugin.rb', line 14

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

helper to log errors with a message via debug



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

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

#parse_user_input(str) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/lolcommits/plugin.rb', line 62

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

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



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

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

#run_postcaptureObject



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

def run_postcapture
  debug 'base plugin, does nothing to anything'
end

#run_precaptureObject



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

def run_precapture
  debug 'base plugin, does nothing to anything'
end

#valid_configuration?Boolean

check config is valid

Returns:

  • (Boolean)


82
83
84
85
86
87
88
89
# File 'lib/lolcommits/plugin.rb', line 82

def valid_configuration?
  if configured?
    true
  else
    puts "Missing #{self.class.name} config - configure with: lolcommits --config -p #{self.class.name}"
    false
  end
end