Class: SanUltari::CommandWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/sanultari/command_wrapper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, clazz, params = nil, options = nil) ⇒ CommandWrapper

Returns a new instance of CommandWrapper.



6
7
8
9
10
11
12
13
14
15
16
17
18
# 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

Instance Attribute Details

#clazzObject

Returns the value of attribute clazz.



4
5
6
# File 'lib/sanultari/command_wrapper.rb', line 4

def clazz
  @clazz
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/sanultari/command_wrapper.rb', line 4

def options
  @options
end

#paramsObject

Returns the value of attribute params.



4
5
6
# File 'lib/sanultari/command_wrapper.rb', line 4

def params
  @params
end

Instance Method Details

#add_param(param_name, options) ⇒ Object



20
21
22
23
24
25
26
# 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

#freeze!Object



40
41
42
# File 'lib/sanultari/command_wrapper.rb', line 40

def freeze!
  @freeze = true
end

#freeze?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/sanultari/command_wrapper.rb', line 36

def freeze?
  @freeze
end

#run(args = nil, options = nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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