Class: Linen::Plugin
- Inherits:
-
Object
- Object
- Linen::Plugin
- Defined in:
- lib/linen/plugin.rb
Overview
Defines Linen::Plugin, which defines the structure of a plugin, as well as the DSL used to write a plugin.
Also includes some “meta-functions”, like help.
Authors
-
Ben Bleything <[email protected]>
Copyright
Copyright © 2007 Laika, Inc.
This code released under the terms of the BSD license.
Version
$Id: plugin.rb 274 2007-07-25 21:06:42Z bbleything $
Defined Under Namespace
Modules: CommandInfrastructure Classes: Argument, ArgumentError, PluginError, SimpleCommand, TwoPhaseCommand
Class Attribute Summary collapse
-
.commands ⇒ Object
readonly
Returns the value of attribute commands.
Class Method Summary collapse
- .argument(name, opts = {}) ⇒ Object
-
.arguments(*args) ⇒ Object
If args is empty, assume it was called like Plugin.arguments, so return the hash.
-
.cleanup(&block) ⇒ Object
multi-purpose method!.
- .command(name, hash = {}, &block) ⇒ Object
-
.description(input = nil) ⇒ Object
define the plugin’s description, or fetch it if nothing passed.
-
.fail(input = nil) ⇒ Object
give validation a way to fail without explicitly raising an exception in the lambda.
- .help ⇒ Object
- .inherited(plugin) ⇒ Object
- .short_name(short_name = nil) ⇒ Object
Class Attribute Details
.commands ⇒ Object (readonly)
Returns the value of attribute commands.
25 26 27 |
# File 'lib/linen/plugin.rb', line 25 def commands @commands end |
Class Method Details
.argument(name, opts = {}) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/linen/plugin.rb', line 61 def self::argument( name, opts = {} ) @defined_arguments ||= IndifferentHash.new if opts[:error_message].nil? and opts['error_message'].nil? opts[ :error_message ] = "The value for #{name} is invalid." end @defined_arguments[ name ] = Argument.new( self, name, opts ) end |
.arguments(*args) ⇒ Object
If args is empty, assume it was called like Plugin.arguments, so return the hash.
74 75 76 77 78 79 80 81 82 |
# File 'lib/linen/plugin.rb', line 74 def self::arguments( *args ) @defined_arguments ||= IndifferentHash.new return @defined_arguments if args.empty? args.each do |arg| argument arg end end |
.cleanup(&block) ⇒ Object
multi-purpose method!
if passed a block, set this plugin’s cleanup proc to the block. if called without a block, execute the proc.
this is meant to allow plugins to clean up after themselves.
104 105 106 107 108 109 110 111 |
# File 'lib/linen/plugin.rb', line 104 def self::cleanup( &block ) if block_given? @cleanup_proc = block else # if we didn't define a proc, don't try to call it @cleanup_proc.call if @cleanup_proc end end |
.command(name, hash = {}, &block) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/linen/plugin.rb', line 85 def self::command( name, hash = {}, &block ) @commands ||= IndifferentHash.new if hash[ :lookup_first ] command = TwoPhaseCommand.new( self, name, &block ) else command = SimpleCommand.new( self, name, &block ) end @commands[ name ] = command end |
.description(input = nil) ⇒ Object
define the plugin’s description, or fetch it if nothing passed
115 116 117 118 119 |
# File 'lib/linen/plugin.rb', line 115 def self::description( input = nil ) return @description unless input @description = input end |
.fail(input = nil) ⇒ Object
give validation a way to fail without explicitly raising an exception in the lambda
124 125 126 127 128 |
# File 'lib/linen/plugin.rb', line 124 def self::fail( input = nil ) input ||= "Validation failed." raise Linen::Plugin::ArgumentError, input end |
.help ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/linen/plugin.rb', line 40 def self::help output = [] desc = description || "No help for #{short_name}" output << desc output << nil #blank line output << "Available commands:" output << nil #blank line commands.each do |name, command| output << "- #{name}" end output << nil #blank line output << "For detailed help on a command, enter \"help #{short_name} <command>\"".wrap return output.join( "\n" ) end |
.inherited(plugin) ⇒ Object
29 30 31 |
# File 'lib/linen/plugin.rb', line 29 def self::inherited( plugin ) Linen::PluginRegistry.instance.register plugin end |
.short_name(short_name = nil) ⇒ Object
34 35 36 37 |
# File 'lib/linen/plugin.rb', line 34 def self::short_name( short_name = nil ) @short_name = short_name if short_name return @short_name || self.to_s.downcase.gsub( /plugin$/, '' ) end |