Class: Lolcommits::Plugin::SamplePlugin

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Plugin initializer

The default superclass method sets @runner and @configuration instance vars and @options to ‘[:enabled]`

Override this method to change the default configurable option names

Parameters:

  • runner (Lolcommits::Runner) (defaults to: nil)

    an instance of a lolcommits runner

  • config (Hash) (defaults to: nil)

    plugin config hash (parsed from saved config YAML)



18
19
20
# File 'lib/lolcommits/plugin/sample_plugin.rb', line 18

def initialize(runner: nil, config: nil)
  super
end

Class Method Details

.runner_orderArray

Returns position(s) of when this 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,



34
35
36
# File 'lib/lolcommits/plugin/sample_plugin.rb', line 34

def self.runner_order
  [:pre_capture, :post_capture, :capture_ready]
end

Instance Method Details

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

- fall back to 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 dig the hash.

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



131
132
133
# File 'lib/lolcommits/plugin/sample_plugin.rb', line 131

def configure_options!
  super
end

#default_optionsObject



135
136
137
138
139
140
141
142
143
144
# File 'lib/lolcommits/plugin/sample_plugin.rb', line 135

def default_options
  {
    ask_for_cheese: true,
    always_a_great_commit?: true,
    camera_emoji: {
      enabled: false,
      emoji_multiplier: 2
    }
  }
end

#enabled?Boolean

Returns true/false indicating if the plugin is enabled or not.

The default superclass method returns true if the enabled option is true i.e. configuration == true

Override this method to define your own custom enabled logic. If this method always returns true, the only way to disable the plugin will be to uninstall the gem.

Note: a ‘valid_configuration?` method also exists and is checked before any plugin hooks execute. Use that to check individual config option values.

Returns:

  • (Boolean)

    true/false indicating if plugin is enabled



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

def enabled?
  super
end

#run_capture_readyObject

Capture ready hook, runs after lolcommits captures a snapshot.

Override this method to execute plugin code after the lolcommit snapshot is captured and all image processing in post capture hooks (from other plugins) has completed

Prints a short (emoji themed) message to STDOUT with the current commit sha.



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

def run_capture_ready
  if config_option(:always_a_great_commit?)
    puts "✨  wow! #{self.runner.sha} is your best looking commit yet! 😘  💻"
  end
end

#run_post_captureObject

Post-capture hook, run after lolcommits captures a snapshot.

Override this method to execute plugin code after the lolcommit snapshot is captured.

Prints a short (emoji themed) message to STDOUT



60
61
62
63
# File 'lib/lolcommits/plugin/sample_plugin.rb', line 60

def run_post_capture
  return unless config_option(:camera_emoji, :enabled)
  puts "#{"📸  " * config_option(:camera_emoji, :emoji_multiplier).to_i}Snap!"
end

#run_pre_captureObject

Pre-capture hook, runs before lolcommits captures a snapshot.

Override this method to execute plugin code before the lolcommit snapshot is captured.

Prints a short (emoji themed) message to STDOUT



47
48
49
# File 'lib/lolcommits/plugin/sample_plugin.rb', line 47

def run_pre_capture
  puts "✨  Say cheese 😁 !" if config_option(:ask_for_cheese)
end

#valid_configuration?Boolean

Returns true/false indicating if the plugin has been correctly configured.

The default superclass method simply checks if ‘@configuration` is present (not empty).

By default if this method returns false, plugin hooks will not execute and a warning message is shown prompting the user to re-configure the plugin.

Override to define your own configuration checks and/or messaging.

Returns:

  • (Boolean)

    true/false indicating if plugin is correct configured



161
162
163
# File 'lib/lolcommits/plugin/sample_plugin.rb', line 161

def valid_configuration?
  super
end