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 commands.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/command-set/dsl.rb', line 85 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 elsif name_or_command_class.nil? command = @command_list[nil] command.instance_eval(&block) return else if 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.
141 142 143 |
# File 'lib/command-set/dsl.rb', line 141 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.clone @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 67 68 69 70 71 72 73 74 75 |
# File 'lib/command-set/dsl.rb', line 48 def require_commands(module_name, file = nil, path = [], *commands) require file rescue nil if Module === module_name mod = module_name else module_path = module_name.to_s.split("::") mod = Object module_path.each do |part| mod = mod.const_get(part) end end set = mod.define_commands unless CommandSet === set raise RuntimeError,"#{set.inspect} isn't a CommandSet" end 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
135 136 137 |
# File 'lib/command-set/dsl.rb', line 135 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
117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/command-set/dsl.rb', line 117 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 |