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.
79 80 81 82 83 84 85 |
# File 'lib/rabal/plugin/foundation.rb', line 79 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.
23 24 25 |
# File 'lib/rabal/plugin/foundation.rb', line 23 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
30 31 32 33 34 35 |
# File 'lib/rabal/plugin/foundation.rb', line 30 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
42 43 44 45 46 47 |
# File 'lib/rabal/plugin/foundation.rb', line 42 def parameter(pname,description,block = nil) @parameters ||= {} @parameters[pname] = {:name => pname, :desc => description, :proc => block} end |
.parameters ⇒ Object
get the parameters back
50 51 52 |
# File 'lib/rabal/plugin/foundation.rb', line 50 def parameters @parameters ||= {} end |
.use_always(d = true) ⇒ Object
58 59 60 |
# File 'lib/rabal/plugin/foundation.rb', line 58 def use_always(d = true) @use_always = d end |
.use_always? ⇒ Boolean
54 55 56 |
# File 'lib/rabal/plugin/foundation.rb', line 54 def use_always? @use_always end |
.use_name ⇒ Object
65 66 67 |
# File 'lib/rabal/plugin/foundation.rb', line 65 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.
127 128 129 |
# File 'lib/rabal/plugin/foundation.rb', line 127 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.
134 135 136 137 138 139 |
# File 'lib/rabal/plugin/foundation.rb', line 134 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
107 108 109 110 111 112 |
# File 'lib/rabal/plugin/foundation.rb', line 107 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.
144 145 146 |
# File 'lib/rabal/plugin/foundation.rb', line 144 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
97 98 99 100 101 102 103 104 |
# File 'lib/rabal/plugin/foundation.rb', line 97 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 |