Class: Lolcommits::Plugin::SamplePlugin
- Inherits:
-
Base
- Object
- Base
- Lolcommits::Plugin::SamplePlugin
- Defined in:
- lib/lolcommits/plugin/sample_plugin.rb
Instance Method Summary collapse
-
#configure_options! ⇒ Hash
Prompts the user to configure all plugin options.
-
#default_options ⇒ Hash
Returns a hash of default options to be presented to the user when configuring (with ‘configure_options!`..
-
#enabled? ⇒ Boolean
Returns true/false indicating if the plugin is enabled or not.
-
#initialize(runner: nil, name: nil, config: nil) ⇒ SamplePlugin
constructor
Initializer.
-
#run_capture_ready ⇒ Object
Capture ready hook, runs after lolcommits captures a snapshot.
-
#run_post_capture ⇒ Object
Post-capture hook, run after lolcommits captures a snapshot.
-
#run_pre_capture ⇒ Object
Pre-capture hook, runs before lolcommits captures a snapshot.
-
#valid_configuration? ⇒ Boolean
Returns true/false indicating if the plugin has been correctly configured.
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
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).
123 124 125 |
# File 'lib/lolcommits/plugin/sample_plugin.rb', line 123 def super end |
#default_options ⇒ Hash
Returns a hash of default options to be presented to the user when configuring (with ‘configure_options!`.
133 134 135 136 137 138 139 140 141 142 |
# File 'lib/lolcommits/plugin/sample_plugin.rb', line 133 def { 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.
90 91 92 |
# File 'lib/lolcommits/plugin/sample_plugin.rb', line 90 def enabled? super end |
#run_capture_ready ⇒ Object
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_capture ⇒ Object
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_capture ⇒ Object
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.
159 160 161 |
# File 'lib/lolcommits/plugin/sample_plugin.rb', line 159 def valid_configuration? super end |