Class: Hairballs::Plugin
- Inherits:
-
Object
- Object
- Hairballs::Plugin
- Includes:
- LibraryHelpers
- Defined in:
- lib/hairballs/plugin.rb
Overview
Plugins provide means for adding functionality to your IRB sessions. They can be as simple as requiring other gems, or as complex as you want them to be.
One benefit of making a simple plugin for requiring other deps is Hairballs::LibraryHelpers#require_libraries method. When used in conjunction with Hairballs::LibraryHelpers#libraries, you don’t have to worry about installing the gems before using IRB. This is particularly helpful when using a Ruby manager (RVM, rbenv, etc) and you install a new Ruby; if you don’t think ahead to install your IRB deps, your IRB session won’t behave like you want; well, not until you ‘exit` and fix the problem. This simple pattern helps alleviate that small headache.
Next, Hairballs Plugins are lazily loaded; ‘require`ing their source files doesn’t mean the methods and such that you add there will do anything; it’s not until you ‘Hairballs.load_plugin(:blargh)` that the code that defines your plugin will get executed.
Instance Attribute Summary collapse
- #name ⇒ Symbol readonly
Instance Method Summary collapse
-
#initialize(name, **attributes) ⇒ Plugin
constructor
A new instance of Plugin.
-
#load!(**attributes) ⇒ Object
Loads the plugin using the
attributes
values. -
#on_load(&block) ⇒ Object
Everything in the
block
given here will be the last code to get evaluated when #load! is called.
Methods included from LibraryHelpers
#do_bundler_extending, #find_latest_gem, #libraries, #require_libraries, #undo_bundler_extending
Constructor Details
#initialize(name, **attributes) ⇒ Plugin
Returns a new instance of Plugin.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/hairballs/plugin.rb', line 32 def initialize(name, **attributes) @name = name @irb_configuration = nil attributes.each do |k, v| define_singleton_method(k) do instance_variable_get("@#{k}".to_sym) end define_singleton_method("#{k}=") do |new_value| instance_variable_set("@#{k}".to_sym, new_value) end instance_variable_set("@#{k}".to_sym, v) end end |
Instance Attribute Details
#name ⇒ Symbol (readonly)
26 27 28 |
# File 'lib/hairballs/plugin.rb', line 26 def name @name end |
Instance Method Details
#load!(**attributes) ⇒ Object
Loads the plugin using the attributes
values. The keys in attributes
must match attributes that were given when the Plugin was defined. Attribute values given here will override the defaults that were given when the Plugin was defined.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/hairballs/plugin.rb', line 60 def load!(**attributes) attributes.each do |k, v| send("#{k}=".to_sym, v) end if @libraries vputs "[pl:#{@name}] Requiring libs..." require_libraries vputs "[pl:#{@name}] Done requiring libs." end return unless @on_load fail PluginLoadFailure, name unless @on_load.kind_of?(Proc) @on_load.call end |
#on_load(&block) ⇒ Object
Everything in the block
given here will be the last code to get evaluated when #load! is called. This is where you define code that makes up your plugin.
52 53 54 |
# File 'lib/hairballs/plugin.rb', line 52 def on_load(&block) @on_load = block end |