Class: Rabal::Plugin::Foundation

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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(options = {})
    @parameters = OpenStruct.new(options)
    validate_parameters
    main_tree_name = my_main_tree_name
    @tree = PluginTree.new(options,resource_by_name(main_tree_name),
                           File.basename(main_tree_name))
end

Instance Attribute Details

#treeObject (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

.parametersObject

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

Returns:

  • (Boolean)


54
55
56
# File 'lib/rabal/plugin/foundation.rb', line 54

def use_always?
    @use_always
end

.use_nameObject



65
66
67
# File 'lib/rabal/plugin/foundation.rb', line 65

def use_name
    name.split("::").last.dashify
end

Instance Method Details

#my_gem_nameObject

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_nameObject

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_parametersObject

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