Module: Etna::CommandExecutor
- Included in:
- EtnaApp::Administrate, EtnaApp::Administrate::Models, EtnaApp::Administrate::Models::Attributes, EtnaApp::Administrate::Project, EtnaApp::Config, EtnaApp::CreateTemplate
- Defined in:
- lib/etna/command.rb
Overview
Include this module into class that dispatches to child CommandExecutors | Commands that must exist as inner classes. Note that non-root CommandExecutors must accept and set their @parent just like commands.
Instance Attribute Summary collapse
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Class Method Summary collapse
Instance Method Summary collapse
- #all_subcommands ⇒ Object
- #dispatch_to_subcommand(cmd = 'help', *args, **kwds) ⇒ Object
- #find_command(*args, **kwds) ⇒ Object
- #help ⇒ Object
- #initialize(parent = nil) ⇒ Object
- #subcommands ⇒ Object
Instance Attribute Details
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
153 154 155 |
# File 'lib/etna/command.rb', line 153 def parent @parent end |
Class Method Details
.included(cls) ⇒ Object
160 161 162 163 164 165 166 167 168 169 |
# File 'lib/etna/command.rb', line 160 def self.included(cls) cls.include(CommandOrExecutor) cls.const_set(:Help, Class.new(Etna::Command) do usage 'List this help' def execute self.parent.help end end) unless cls.const_defined?(:Help) end |
Instance Method Details
#all_subcommands ⇒ Object
203 204 205 |
# File 'lib/etna/command.rb', line 203 def all_subcommands subcommands.values + (subcommands.values.map { |s| s.respond_to?(:all_subcommands) ? s.all_subcommands : [] }.flatten) end |
#dispatch_to_subcommand(cmd = 'help', *args, **kwds) ⇒ Object
183 184 185 186 187 188 189 190 |
# File 'lib/etna/command.rb', line 183 def dispatch_to_subcommand(cmd = 'help', *args, **kwds) unless subcommands.include?(cmd) cmd = 'help' args = [] end subcommands[cmd].find_command(*args, **kwds) end |
#find_command(*args, **kwds) ⇒ Object
178 179 180 181 |
# File 'lib/etna/command.rb', line 178 def find_command(*args, **kwds) flags, args = parse_flags(*args) dispatch_to_subcommand(*args, **(kwds.update(flags))) end |
#help ⇒ Object
171 172 173 174 175 176 |
# File 'lib/etna/command.rb', line 171 def help puts "usage: #{program_name} #{desc}" subcommands.each do |name, cmd| puts cmd.usage end end |
#initialize(parent = nil) ⇒ Object
155 156 157 158 |
# File 'lib/etna/command.rb', line 155 def initialize(parent = nil) super() @parent = parent end |
#subcommands ⇒ Object
192 193 194 195 196 197 198 199 200 201 |
# File 'lib/etna/command.rb', line 192 def subcommands @subcommands ||= self.class.constants.sort.reduce({}) do |acc, n| acc.tap do c = self.class.const_get(n) next unless c.instance_methods.include?(:find_command) v = c.new(self) acc[v.command_name] = v end end end |