Module: Filigree::Commands::ClassMethods

Defined in:
lib/filigree/commands.rb

Overview

Class Methods #

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#command_listArray<Command>

Returns:



79
80
81
# File 'lib/filigree/commands.rb', line 79

def command_list
  @command_list
end

#commandsHash<String, Hash>

Returns:

  • (Hash<String, Hash>)


76
77
78
# File 'lib/filigree/commands.rb', line 76

def commands
  @commands
end

Class Method Details

.extended(klass) ⇒ Object

Callbacks #



202
203
204
# File 'lib/filigree/commands.rb', line 202

def self.extended(klass)
	klass.install_icvars
end

Instance Method Details

#add_command(command_obj) ⇒ void

This method returns an undefined value.

Add a command to the necessary internal data structures.

Parameters:

  • command_obj (Command)

    Command to add



86
87
88
89
90
# File 'lib/filigree/commands.rb', line 86

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) ⇒ void

This method returns an undefined value.

Add a new command to the class. All command code is executed in the context of the Commands object.

Parameters:

  • str (String)

    Name of the command

  • block (Proc)

    Code to be executed when the command is run



99
100
101
102
103
104
105
# File 'lib/filigree/commands.rb', line 99

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) ⇒ void

This method returns an undefined value.

This will generate an anonymous Filigree::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.

Parameters:



119
120
121
122
# File 'lib/filigree/commands.rb', line 119

def config(&block)
	@config = Class.new { include Filigree::Configuration }
	@config.instance_exec(&block)
end

#get_namespace(tokens, root: @commands) ⇒ Array<(Hash<Symbol, Hash>, Array<String>)>

Given a root namespace, find the namespace indicated by the provided tokens.

Parameters:

  • tokens (Array<String>)

    String tokens specifying the namespace

  • root (Hash<Symbol, Hash>) (defaults to: @commands)

    Root namespace

Returns:

  • (Array<(Hash<Symbol, Hash>, Array<String>)>)

    The requested namespace and the remainder of the tokens.



153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/filigree/commands.rb', line 153

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) ⇒ void

This method returns an undefined value.

Attaches the provided help string to the command that is defined next.

Parameters:

  • str (String)

    Help string for the next command



130
131
132
# File 'lib/filigree/commands.rb', line 130

def help(str)
	@help_string = str
end

#install_icvarsvoid

This method returns an undefined value.

Install the instance class variables in the including class.



137
138
139
140
141
142
143
# File 'lib/filigree/commands.rb', line 137

def install_icvars
	@commands     = Hash.new
	@command_list = Array.new
	@config       = nil
	@help_string  = ''
	@param_docs   = Array.new
end

#param(name, description) ⇒ void

This method returns an undefined value.

Add a description for a command’s parameter.

Parameters:

  • name (String)

    Name of the parameter

  • description (String)

    Description of the parameter.



174
175
176
# File 'lib/filigree/commands.rb', line 174

def param(name, description)
	@param_docs << [name, description]
end

#reify_namespace(tokens, root: @commands) ⇒ Array<(Hash<Symbol, Hash>, Array<String>)>

Find or create the namespace specified by tokens.

Parameters:

  • tokens (Array<String>)

    Tokens specifying the namespace.

  • root (Hash<Symbol, Hash>) (defaults to: @commands)

    Root namespace

Returns:

  • (Array<(Hash<Symbol, Hash>, Array<String>)>)

    The requested namespace and the remainder of the tokens.



185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/filigree/commands.rb', line 185

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