Class: Commands::Command

Inherits:
Object show all
Defined in:
lib/commands.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, description, arg, commands) ⇒ Command

Returns a new instance of Command.



101
102
103
104
105
106
107
# File 'lib/commands.rb', line 101

def initialize(name, description, arg, commands)
  @name = name
  @description = description
  @arg = arg
  @commands = commands
  @logger = commands.logger
end

Instance Attribute Details

#argObject

Returns the value of attribute arg.



99
100
101
# File 'lib/commands.rb', line 99

def arg
  @arg
end

#commandsObject

Returns the value of attribute commands.



99
100
101
# File 'lib/commands.rb', line 99

def commands
  @commands
end

#descriptionObject

Returns the value of attribute description.



99
100
101
# File 'lib/commands.rb', line 99

def description
  @description
end

#loggerObject

Returns the value of attribute logger.



99
100
101
# File 'lib/commands.rb', line 99

def logger
  @logger
end

#nameObject

Returns the value of attribute name.



99
100
101
# File 'lib/commands.rb', line 99

def name
  @name
end

Instance Method Details

#enact(client) ⇒ Object

action the command



114
115
# File 'lib/commands.rb', line 114

def enact(client)
end

#get_field(field_symbol, default_value = nil) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/commands.rb', line 128

def get_field(field_symbol, default_value=nil)
  value = nil
  if respond_to?(field_symbol) then
    value = self.send(field_symbol)
  end
  if value == nil then
    value = @commands.global_options[field_symbol]
  end
  default_field_symbol = ("default_" + field_symbol.to_s).to_sym
  if value == nil && respond_to?(default_field_symbol) then
    value = self.send(default_field_symbol)
  end
  if value == nil then
    value = default_value
  end
  return value
end

#has_value(obj, *args) ⇒ Object



159
160
161
162
163
164
# File 'lib/commands.rb', line 159

def has_value(obj, *args)
  while obj != nil && args.size > 1 do
    obj = obj[args.shift]
  end
  return obj == args[0]
end

#have(field_symbol) ⇒ Object



154
155
156
157
# File 'lib/commands.rb', line 154

def have(field_symbol)
  value = get_field(field_symbol)
  return value != nil 
end

#option(argument_name, argument_symbol, value) ⇒ Object



117
118
119
120
121
122
123
124
125
126
# File 'lib/commands.rb', line 117

def option(argument_name, argument_symbol, value)
  var = self.send(argument_symbol)
  if var == nil then
    self.send((argument_symbol.to_s + "=").to_sym, value)
  elsif var.is_a?(Array) then
    var << value
  else
    raise RuntimeError, "Repeating #{argument_name} is not allowed, previous value was #{var.inspect}"
  end
end

#require(field_symbol, error_msg) ⇒ Object



146
147
148
149
150
151
152
# File 'lib/commands.rb', line 146

def require(field_symbol, error_msg)
  value = get_field(field_symbol)
  if value == nil then
    raise RuntimeError, error_msg
  end
  return value
end

#require_single_jobflowObject



173
174
175
176
177
178
179
180
181
# File 'lib/commands.rb', line 173

def require_single_jobflow
  jobflow_ids = get_field(:jobflow)
  if jobflow_ids.size == 0 then
    raise RuntimeError, "A jobflow is required to use option #{name}"
  elsif jobflow_ids.size > 1 then
    raise RuntimeError, "The option #{name} can only act on a single jobflow"
  end
  return jobflow_ids.first
end

#resolve(obj, *args) ⇒ Object



166
167
168
169
170
171
# File 'lib/commands.rb', line 166

def resolve(obj, *args)
  while obj != nil && args.size > 0 do
    obj = obj[args.shift]
  end
  return obj
end

#validateObject

test any constraints that the command has



110
111
# File 'lib/commands.rb', line 110

def validate
end