Module: Filigree::Commands::ClassMethods
- Defined in:
- lib/filigree/commands.rb
Overview
Class Methods #
Instance Attribute Summary collapse
Class Method Summary collapse
-
.extended(klass) ⇒ Object
Callbacks #.
Instance Method Summary collapse
-
#add_command(command_obj) ⇒ void
Add a command to the necessary internal data structures.
-
#command(str, &block) ⇒ void
Add a new command to the class.
-
#config(&block) ⇒ void
This will generate an anonymous Filigree::Configuration class for this command.
-
#get_namespace(tokens, root: @commands) ⇒ Array<(Hash<Symbol, Hash>, Array<String>)>
Given a root namespace, find the namespace indicated by the provided tokens.
-
#help(str) ⇒ void
Attaches the provided help string to the command that is defined next.
-
#install_icvars ⇒ void
Install the instance class variables in the including class.
-
#param(name, description) ⇒ void
Add a description for a command’s parameter.
-
#reify_namespace(tokens, root: @commands) ⇒ Array<(Hash<Symbol, Hash>, Array<String>)>
Find or create the namespace specified by tokens.
Instance Attribute Details
#command_list ⇒ Array<Command>
92 93 94 |
# File 'lib/filigree/commands.rb', line 92 def command_list @command_list end |
#commands ⇒ 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.
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.
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.
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.
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.
143 144 145 |
# File 'lib/filigree/commands.rb', line 143 def help(str) @help_string = str end |
#install_icvars ⇒ void
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.
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.
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 |