module Filigree::Commands::ClassMethods
Class Methods #
Attributes
@return [Array<Command>]
@return [Hash<String, Hash>]
Public Class Methods
Callbacks #
# File lib/filigree/commands.rb, line 215 def self.extended(klass) klass.install_icvars end
Public Instance Methods
Add a command to the necessary internal data structures.
@param [Command] command_obj Command
to add
@return [void]
# File lib/filigree/commands.rb, line 99 def add_command(command_obj) @command_list << command_obj namespace = reify_namespace(command_obj.name.split.map(&:to_sym)) namespace[:nil] = command_obj end
Add a new command to the class. All command code is executed in the context of the Commands
object.
@param [String] str Name of the command @param [Proc] block Code to be executed when the command is run
@return [void]
# File lib/filigree/commands.rb, line 112 def command(str, &block) add_command Command.new(str, @help_string, @param_docs, @config, block) @help_string = '' @param_docs = Array.new @config = nil end
This will generate an anonymous {Configuration} class for this command. After a string resolves to the next command defined the remainder of the command line will be passed to an instance of this Configuration
class. Any remaining text is then provided to the command as usual.
The variables defined in the configuration class are available in the command's block.
@param [Proc] block Body of the {Configuration} class
@return [void]
# File lib/filigree/commands.rb, line 132 def config(&block) @config = Class.new { include Filigree::Configuration } @config.instance_exec(&block) end
Given a root namespace, find the namespace indicated by the provided tokens.
@param [Array<String>] tokens String tokens specifying the namespace @param [Hash<Symbol, Hash>] root Root namespace
@return [Array<(Hash<Symbol, Hash>, Array
<String>)>]
The requested namespace and the remainder of the tokens.
# File lib/filigree/commands.rb, line 166 def get_namespace(tokens, root: @commands) if tokens.empty? [root, tokens] else curr_token = tokens.first.to_sym if ns = root[curr_token] tokens.shift get_namespace(tokens, root: ns) else [root, tokens] end end end
Attaches the provided help string to the command that is defined next.
@param [String] str Help string for the next command
@return [void]
# File lib/filigree/commands.rb, line 143 def help(str) @help_string = str end
Install the instance class variables in the including class.
@return [void]
# File lib/filigree/commands.rb, line 150 def install_icvars @commands = Hash.new @command_list = Array.new @config = nil @help_string = '' @param_docs = Array.new end
Add a description for a command's parameter.
@param [String] name Name of the parameter @param [String] description Description of the parameter.
@return [void]
# File lib/filigree/commands.rb, line 187 def param(name, description) @param_docs << [name, description] end
Find or create the namespace specified by tokens.
@param [Array<String>] tokens Tokens specifying the namespace. @param [Hash<Symbol, Hash>] root Root namespace
@return [Array<(Hash<Symbol, Hash>, Array
<String>)>]
The requested namespace and the remainder of the tokens.
# File lib/filigree/commands.rb, line 198 def reify_namespace(tokens, root: @commands) if tokens.empty? root else curr_token = tokens.shift ns = root[curr_token] ns = root[curr_token] = Hash.new if ns.nil? reify_namespace(tokens, root: ns) end end