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>
79 80 81 |
# File 'lib/filigree/commands.rb', line 79 def command_list @command_list end |
#commands ⇒ 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.
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.
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.
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.
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.
130 131 132 |
# File 'lib/filigree/commands.rb', line 130 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.
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.
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.
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 |