class SanUltari::CommandWrapper
Attributes
clazz[R]
options[R]
params[R]
Public Class Methods
new(name, clazz, params = nil, options = nil)
click to toggle source
# File lib/sanultari/command_wrapper.rb, line 6 def initialize name, clazz, params = nil, options = nil @name = name @clazz = clazz @params = params @params ||= [] @options = options @options ||= {} @freeze = false @required_param_count = 0 # TODO : deprecate @args = [] end
Public Instance Methods
add_param(param_name, options)
click to toggle source
# File lib/sanultari/command_wrapper.rb, line 20 def add_param param_name, options param = SanUltari::CommandParameter.new(param_name, options) if param.require? && !param.default @required_param_count += 1 end @params.push param end
clazz=(value)
click to toggle source
# File lib/sanultari/command_wrapper.rb, line 32 def clazz= value @clazz = value if !@freeze end
freeze!()
click to toggle source
# File lib/sanultari/command_wrapper.rb, line 40 def freeze! @freeze = true end
freeze?()
click to toggle source
# File lib/sanultari/command_wrapper.rb, line 36 def freeze? @freeze end
params=(value)
click to toggle source
# File lib/sanultari/command_wrapper.rb, line 28 def params= value @clazz = value if !@freeze end
run(args = nil, options = nil)
click to toggle source
# File lib/sanultari/command_wrapper.rb, line 44 def run args = nil, options = nil args ||= [] options ||= [] unless args.length >= @required_param_count # TODO: standard output change puts "this command has #{@required_param_count} parameters at least" return end runner = @clazz.new # TODO options parsing options, param_configs = set_values runner, args, options set_defaults runner, param_configs if runner.public_method(@name).parameters.length > 0 runner.public_send @name, *@args else runner.public_send @name end end
Private Instance Methods
handle_args(param_config, value)
click to toggle source
# File lib/sanultari/command_wrapper.rb, line 70 def handle_args param_config, value if param_config.order < 0 @args.push value else if @args[order] == nil @args[order] = value else puts "this command is mis-configured. some arguments have same order." end end end
set_defaults(object, param_configs)
click to toggle source
# File lib/sanultari/command_wrapper.rb, line 82 def set_defaults object, param_configs param_configs.each do |param_config| value = param_config.default if param_config.require? && !value puts "required parameter(#{param_config.name}) is missing" return end set_value(object, param_config.name, value) unless value == nil param_configs.shift end end
set_value(object, name, value)
click to toggle source
# File lib/sanultari/command_wrapper.rb, line 66 def set_value object, name, value object.public_send "#{name}=".to_sym, value end
set_values(object, args, options)
click to toggle source
# File lib/sanultari/command_wrapper.rb, line 95 def set_values object, args, options param_configs = @params.clone args.each do |arg| if param_configs.empty? index = args.index arg options += args[index..-1] break end current_param_config = param_configs[0] if arg.start_with? '-' options.push arg next end # set params if current_param_config.type == :parameter handle_args current_param_config, arg else unless object.respond_to? "#{current_param_config.name}=".to_sym next end set_value object, current_param_config.name, arg end param_configs.shift end return options, param_configs end