module Filigree::Commands::ClassMethods

Class Methods #

Attributes

command_list[RW]

@return [Array<Command>]

commands[RW]

@return [Hash<String, Hash>]

Public Class Methods

extended(klass) click to toggle source

Callbacks #

# File lib/filigree/commands.rb, line 215
def self.extended(klass)
        klass.install_icvars
end

Public Instance Methods

add_command(command_obj) click to toggle source

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
command(str, &block) click to toggle source

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
config(&block) click to toggle source

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
get_namespace(tokens, root: @commands) click to toggle source

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
help(str) click to toggle source

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_icvars() click to toggle source

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
param(name, description) click to toggle source

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
reify_namespace(tokens, root: @commands) click to toggle source

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