class MDT::Helpers::Command
A helper class to be used when implementing commands
Public Class Methods
apply_command_modifiers(command, modifiers)
click to toggle source
Finds and applies command modifiers to a command using passed modifiers configuration. Arguments:
-
command
- a command or expression to prepend command modifiers to -
modifiers
- an array of modifier configurations - each configuration is a Hash that includes the modifier type and modifier options
Returns:
-
Modified command
# File lib/mdt/helpers/command.rb 13 def self.apply_command_modifiers(command, modifiers) 14 modifiers.each do |modifier_config| 15 unless modifier_config.has_key?('type') 16 puts 'WARNING: Skipping command modifier because of missing type...' 17 next 18 end 19 modifier_key = modifier_config['type'].split('.').first 20 modifier_value = modifier_config['type'].split('.').last 21 modifier_options = modifier_config['options'] 22 modifier = MDT::CommandModifiers::Base.descendants.select { |cm| cm.key == modifier_key }.first 23 if modifier == nil 24 puts "WARNING: Could not find a command modifier set with key #{modifier_key}. Check its correctness or install needed MDT modules." 25 next 26 end 27 unless modifier.subkeys.include?(modifier_value) 28 puts "WARNING: Command modifier set with key #{modifier_key} does not have a command modifier with key #{modifier_value}." 29 next 30 end 31 modifier = modifier.new 32 command = modifier.prepend(modifier_value, command, modifier_options) 33 end 34 command 35 end