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:



92
93
94
# File 'lib/filigree/commands.rb', line 92

def command_list
  @command_list
end

#commandsHash<String, Hash>

Returns:

  • (Hash<String, Hash>)


89
90
91
# File 'lib/filigree/commands.rb', line 89

def commands
  @commands
end

Class Method Details

.extended(klass) ⇒ Object

Callbacks #



215
216
217
# File 'lib/filigree/commands.rb', line 215

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



99
100
101
102
103
# 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) ⇒ 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



112
113
114
115
116
117
118
# 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) ⇒ 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:



132
133
134
135
# 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) ⇒ 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.



166
167
168
169
170
171
172
173
174
175
176
177
178
179
# 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) ⇒ 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



143
144
145
# File 'lib/filigree/commands.rb', line 143

def help(str)
	@help_string = str
end

#install_icvarsvoid

This method returns an undefined value.

Install the instance class variables in the including class.



150
151
152
153
154
155
156
# 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) ⇒ 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.



187
188
189
# File 'lib/filigree/commands.rb', line 187

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.



198
199
200
201
202
203
204
205
206
207
208
209
# 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