Class: Rabal::Plugin::Foundation
- Inherits:
-
Object
- Object
- Rabal::Plugin::Foundation
- Defined in:
- lib/rabal/plugin/foundation.rb
Overview
Base plugin that all other plugins will inherit from, but not directly. New plugins are declared with:
class NewPlugin < Rabal::Plugin::Base "/foo/bar"
...
end
This process uses GemPlugin under the covers, it has just been wrapped to provide a different Base
class for everything to inherit from.
Instance Attribute Summary collapse
-
#tree ⇒ Object
readonly
the PluginTree that the plugin creates.
Class Method Summary collapse
-
.inherited(by_class) ⇒ Object
Called when another class inherits from Foundation.
-
.parameter(pname, description, block = nil) ⇒ Object
part of the mini DSL for describing a Plugin.
-
.parameters ⇒ Object
get the parameters back.
- .use_always(d = true) ⇒ Object
- .use_always? ⇒ Boolean
- .use_name ⇒ Object
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Foundation
constructor
A Plugin is instantiated and the options passed to it are the results of the command line options for this plugin.
-
#my_gem_name ⇒ Object
What gem a plugin belongs to.
-
#my_main_tree_name ⇒ Object
The main resource for this Plugin.
-
#prompt_for_param(param, desc, validation_proc = nil) ⇒ Object
prompt the user for the value that we want from them.
-
#resource_by_name(resource_name) ⇒ Object
Access a resource utilized by the gem.
-
#validate_parameters ⇒ Object
validate the parameters of the plugin in the simplest way possible.
Constructor Details
#initialize(options = {}) ⇒ Foundation
A Plugin is instantiated and the options passed to it are the results of the command line options for this plugin.
The default action for any Plugin is to create a PluginTree from its options utilizing the register_path
to determine a location within the gem’s resources. The resources is used to instantiate a PluginTree and that is set to tree
and by default, this tree will be ‘mounted’ at the root of some other tree.
78 79 80 81 82 83 84 |
# File 'lib/rabal/plugin/foundation.rb', line 78 def initialize( = {}) @parameters = OpenStruct.new() validate_parameters main_tree_name = my_main_tree_name @tree = PluginTree.new(,resource_by_name(main_tree_name), File.basename(main_tree_name)) end |
Instance Attribute Details
#tree ⇒ Object (readonly)
the PluginTree that the plugin creates.
22 23 24 |
# File 'lib/rabal/plugin/foundation.rb', line 22 def tree @tree end |
Class Method Details
.inherited(by_class) ⇒ Object
Called when another class inherits from Foundation. when that happens that class is registered in the GemPlugin::Manager
29 30 31 32 33 34 |
# File 'lib/rabal/plugin/foundation.rb', line 29 def inherited(by_class) register_key = "/" + by_class.to_s.downcase by_class.register_path register_as GemPlugin::Manager.instance.register(register_as,register_key,by_class) register_as = nil end |
.parameter(pname, description, block = nil) ⇒ Object
part of the mini DSL for describing a Plugin
41 42 43 44 45 46 |
# File 'lib/rabal/plugin/foundation.rb', line 41 def parameter(pname,description,block = nil) @parameters ||= {} @parameters[pname] = {:name => pname, :desc => description, :proc => block} end |
.parameters ⇒ Object
get the parameters back
49 50 51 |
# File 'lib/rabal/plugin/foundation.rb', line 49 def parameters @parameters ||= {} end |
.use_always(d = true) ⇒ Object
57 58 59 |
# File 'lib/rabal/plugin/foundation.rb', line 57 def use_always(d = true) @use_always = d end |
.use_always? ⇒ Boolean
53 54 55 |
# File 'lib/rabal/plugin/foundation.rb', line 53 def use_always? @use_always end |
.use_name ⇒ Object
64 65 66 |
# File 'lib/rabal/plugin/foundation.rb', line 64 def use_name name.split("::").last.dashify end |
Instance Method Details
#my_gem_name ⇒ Object
What gem a plugin belongs to. This is necessary to access the resources of the gem the plugin may use. Overload this in a child plugin to return what you want. By default it uses the first part of the path the gem is registered under.
That is when the plugin is defined
class MyPlugin < Rabal::Plugin::Base "/my-plugin/something"
...
end
‘my-plugin’ is defined as being the default gem name.
126 127 128 |
# File 'lib/rabal/plugin/foundation.rb', line 126 def my_gem_name self.class.register_path.split("/").find{|p| p.length > 0} end |
#my_main_tree_name ⇒ Object
The main resource for this Plugin. This is generally a file or a directory that the plugin will use as a template and create a PluginTree from.
133 134 135 136 137 138 |
# File 'lib/rabal/plugin/foundation.rb', line 133 def my_main_tree_name tree_name = self.class.register_path.split("/").find_all {|p| p.length > 0} tree_name.shift tree_name.unshift "trees" tree_name.join("/") end |
#prompt_for_param(param, desc, validation_proc = nil) ⇒ Object
prompt the user for the value that we want from them
106 107 108 109 110 111 |
# File 'lib/rabal/plugin/foundation.rb', line 106 def prompt_for_param(param,desc,validation_proc = nil) ask("#{desc} ? ") do |q| q.readline = true q.validate = validation_proc end end |
#resource_by_name(resource_name) ⇒ Object
Access a resource utilized by the gem. The name is the path to a file or directory under the ‘resources’ directory in the gem this plugin is a part of.
143 144 145 |
# File 'lib/rabal/plugin/foundation.rb', line 143 def resource_by_name(resource_name) Rabal.application.plugin_resource(my_gem_name,resource_name) end |
#validate_parameters ⇒ Object
validate the parameters of the plugin in the simplest way possible. Make sure that each listend parameters is not null. This assumes that @parameters has a method for each parameter name
96 97 98 99 100 101 102 103 |
# File 'lib/rabal/plugin/foundation.rb', line 96 def validate_parameters self.class.parameters.each do |name,param_info| if not @parameters.respond_to?(name) or @parameters.send(name).nil? then value = prompt_for_param(param_info[:name],param_info[:desc],param_info[:proc]) @parameters.send("#{name}=",value) end end end |