Class: Lolcommits::Plugin::SamplePlugin

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

Instance Method Summary collapse

Constructor Details

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

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

  • name (String) (defaults to: nil)

    name for this plugin, usually set from Gem name

  • config (Hash) (defaults to: nil)

    plugin config hash (parsed from saved config YAML)



20
21
22
# File 'lib/lolcommits/plugin/sample_plugin.rb', line 20

def initialize(runner: nil, name: nil, config: nil)
  super
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



123
124
125
# File 'lib/lolcommits/plugin/sample_plugin.rb', line 123

def configure_options!
  super
end

#default_optionsHash

Returns a hash of default options to be presented to the user when configuring (with ‘configure_options!`.

Returns:

  • (Hash)

    with the default option names (keys) and values



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

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 are valid.

Returns:

  • (Boolean)

    true/false indicating if plugin is enabled



90
91
92
# File 'lib/lolcommits/plugin/sample_plugin.rb', line 90

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.



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

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

  # describe the lolcommit
  if self.runner.capture_image?
    puts "(it was an image!)"
  elsif self.runner.capture_video
    puts "(it was a video!)"
  elsif self.runner.capture_gif
    puts "(it was a gif!)"
  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



44
45
46
47
# File 'lib/lolcommits/plugin/sample_plugin.rb', line 44

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



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

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



159
160
161
# File 'lib/lolcommits/plugin/sample_plugin.rb', line 159

def valid_configuration?
  super
end