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:



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

def command_list
  @command_list
end

#commandsHash<String, Hash>

Returns:



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

def commands
  @commands
end

Class Method Details

.extended(klass) ⇒ Object

Callbacks #



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

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



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

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



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

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:



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

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:



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

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



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

def help(str)
	@help_string = str
end

#install_icvarsvoid

This method returns an undefined value.

Install the instance class variables in the including class.



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

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.



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

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:



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

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