Module: Command::DSL::CommandSetDefinition
- Included in:
- CommandSet
- Defined in:
- lib/command-set/dsl.rb
Overview
These are the commands available within the CommandSet::define_commands block.
Instance Method Summary collapse
-
#command(name_or_command_class, name_or_nil = nil, &block) ⇒ Object
Defines a command.
-
#define_commands(&block) ⇒ Object
This is the method that makes DSL::CommandSetDefinition available.
-
#include_commands(set, *commands) ⇒ Object
Allows other command sets to be composited into this one.
-
#require_commands(module_name, file = nil, path = [], *commands) ⇒ Object
If you’ve got a file dedicated to a set of commands, (and you really should) you can use require_commands to require it, call
define_commandson a specific Module, pick out a specific subcommand (by passing a path), and then including specific commands from it. -
#root_command(&block) ⇒ Object
Occasionally it makes sense for a subcommand to do something when invoked on it’s own.
-
#sub_command(name, &block) ⇒ Object
Defines a nested CommandSet.
Instance Method Details
#command(name_or_command_class, name_or_nil = nil, &block) ⇒ Object
Defines a command. Either:
-
pass a name and a block, which will create the command on
the fly - within the block, use methods from
Command::DSL::CommandDefinition.
-
pass a Command subclass - which will be added to the set based on it’s name.
-
pass a Command subclass, a name, and a block. The new command will
be a subclass of the class you passed in, which is great for a
series of related commands.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/command-set/dsl.rb', line 76 def command(name_or_command_class, name_or_nil=nil, &block) @subject_template = nil if Class === name_or_command_class && Command > name_or_command_class if block.nil? command = name_or_command_class.dup command.parent = self name = command.name else name = name_or_nil.to_s command = name_or_command_class.setup(self, name, &block) end else if name_or_command_class.nil? name = nil elsif String === name_or_command_class or Symbol === name_or_command_class name = name_or_command_class.to_s else raise RuntimeError, "#{name_or_command_class} is neither a Command class nor a name!" end command = Command.setup(self, name, &block) end @command_list[name] = command end |
#define_commands(&block) ⇒ Object
This is the method that makes DSL::CommandSetDefinition available. It’s just a wrapper on instance_eval, honestly.
130 131 132 |
# File 'lib/command-set/dsl.rb', line 130 def define_commands(&block) instance_eval(&block) end |
#include_commands(set, *commands) ⇒ Object
Allows other command sets to be composited into this one. And optional list of command names will cherry-pick the commands of the other set, otherwise they’re all folded in, with preference given to the new commands.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/command-set/dsl.rb', line 22 def include_commands(set, *commands) new_commands = set.command_list commands.map!{|c| c.to_s} unless commands.empty? new_commands.delete_if do |name,command| not commands.include? name end end new_commands.each_pair do|name, command| if(CommandSet === @command_list[name]) next unless CommandSet === command @command_list[name].include_commands(command) else @command_list[name] = command.dup @command_list[name].parent=self end end end |
#require_commands(module_name, file = nil, path = [], *commands) ⇒ Object
If you’ve got a file dedicated to a set of commands, (and you really should) you can use require_commands to require it, call define_commands on a specific Module, pick out a specific subcommand (by passing a path), and then including specific commands from it.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/command-set/dsl.rb', line 48 def require_commands(module_name, file = nil, path = [], *commands) require file rescue nil module_path = module_name.to_s.split("::") mod = Object module_path.each do |part| mod = mod.const_get(part) end set = mod.define_commands set = set.find_command(*path) if CommandSet === set include_commands(set, *commands) elsif Class === set and Command > set command(set) else raise RuntimeError,"#{set.inspect} isn't a CommandSet or a Command" end end |
#root_command(&block) ⇒ Object
Occasionally it makes sense for a subcommand to do something when invoked on it’s own. Define that command using root_command as you would a normal command
124 125 126 |
# File 'lib/command-set/dsl.rb', line 124 def root_command(&block) command(nil, &block) end |
#sub_command(name, &block) ⇒ Object
Defines a nested CommandSet. Commands within the nested set will be referenced by preceding them with the name of the set. DSL::CommandSetDefinition will be available within the block to be used on the subcommand
106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/command-set/dsl.rb', line 106 def sub_command(name, &block) @subject_template = nil name = name.to_s if (@command_list.has_key? name) && (CommandSet === @command_list[name]) command = @command_list[name] else command = CommandSet.new(self, name) @command_list[name] = command end command.define_commands(&block) #paths_update(command, name) end |