Class: SciYAG::Backends::Descriptions::Description

Inherits:
Object
  • Object
show all
Defined in:
lib/SciYAG/Backends/descriptions.rb

Overview

The base class for all descriptions. A description describes a “plugin” class. It has the following attributes:

  • the basic name, code-like, which is used mainly for internal purposes;

  • the long name, which has to be translated

  • the description itself, some small text describing the nature of the plugin

  • a list of parameters the plugin can take, along with their description. These are Parameter .

Direct Known Subclasses

FilterDescription

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cls, name, long_name, description = "") ⇒ Description

Initializes a Description



185
186
187
188
189
190
191
192
193
# File 'lib/SciYAG/Backends/descriptions.rb', line 185

def initialize(cls, name, long_name, description = "")
  @class = cls
  @name = name
  @long_name = long_name
  @description = description
  @param_list = []
  @param_hash = {}
  @init_param_list = []
end

Instance Attribute Details

#classObject

the class to instantiate.



162
163
164
# File 'lib/SciYAG/Backends/descriptions.rb', line 162

def class
  @class
end

#descriptionObject

(text) description !



168
169
170
# File 'lib/SciYAG/Backends/descriptions.rb', line 168

def description
  @description
end

#init_param_listObject

The list of parameters that have to be fed into the initialize function.



181
182
183
# File 'lib/SciYAG/Backends/descriptions.rb', line 181

def init_param_list
  @init_param_list
end

#long_nameObject

Long name, the one for public display



171
172
173
# File 'lib/SciYAG/Backends/descriptions.rb', line 171

def long_name
  @long_name
end

#nameObject

The name of the backend (short, code-like)



165
166
167
# File 'lib/SciYAG/Backends/descriptions.rb', line 165

def name
  @name
end

#param_hashObject (readonly)

The hash holding parameters. Useful mainly for subclasses



174
175
176
# File 'lib/SciYAG/Backends/descriptions.rb', line 174

def param_hash
  @param_hash
end

#param_listObject (readonly)

The parameter list



177
178
179
# File 'lib/SciYAG/Backends/descriptions.rb', line 177

def param_list
  @param_list
end

Instance Method Details

#add_param(param) ⇒ Object



211
212
213
214
215
216
217
218
# File 'lib/SciYAG/Backends/descriptions.rb', line 211

def add_param(param)
  @param_list << param

  # Three different cross-linkings.
  @param_hash[param.reader_symbol] = param
  @param_hash[param.writer_symbol] = param
  @param_hash[param.name] = param
end

#fill_parser(instance, parser, uniquify = true) ⇒ Object

Fills an OptionParser with all the parameters the Backend should be able to take.



235
236
237
238
# File 'lib/SciYAG/Backends/descriptions.rb', line 235

def fill_parser(instance, parser, uniquify = true)
  parser_banner(instance, parser)
  parser_options(instance, parser, uniquify)
end

#init_params(*args) ⇒ Object

Pushes the names of the params onto @init_list_param_list. Arguments have to be strings.



228
229
230
# File 'lib/SciYAG/Backends/descriptions.rb', line 228

def init_params(*args)
  @init_param_list += args
end

#instantiate(*args) ⇒ Object

Creates an instance of the class, forwards parameters to the initialize method.



197
198
199
# File 'lib/SciYAG/Backends/descriptions.rb', line 197

def instantiate(*args)
  return @class.new(*args)
end

#param_set(i, p) ⇒ Object



220
221
222
223
224
# File 'lib/SciYAG/Backends/descriptions.rb', line 220

def param_set(i, p)
  param = p
  instance = i
  return proc { |x| param.set(instance,x) }
end

#parser_banner(instance, parser) ⇒ Object

The parsers’s banner.



256
257
258
# File 'lib/SciYAG/Backends/descriptions.rb', line 256

def parser_banner(instance, parser) 
  # nothing by default
end

#parser_instantiate_option(parser, receiver, result, prefix = "") ⇒ Object

Creates a parser entry in parser that creates a new instance of the description’s class and feed it to the result method of the receiver. You can specify an optionnal prefix for the option’s name.



264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/SciYAG/Backends/descriptions.rb', line 264

def parser_instantiate_option(parser, receiver, result, prefix = "")
  op_name = "--#{prefix}#{@name}"
  for arg in @init_param_list
    op_name += " " + arg.name.upcase
  end
  # cool !
  parser.on(op_name, @description) do |*a|
    b = prepare_instantiate_arglist(*a)
    instance = instantiate(*b)
    receiver.send(result, instance)
  end
end

#parser_options(instance, parser, uniquify = true) ⇒ Object

Fills a parser with options (and only that)



241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/SciYAG/Backends/descriptions.rb', line 241

def parser_options(instance, parser, uniquify = true)
  raise "The instance is not of the right class" unless
    instance.is_a? @class
  for param in @param_list
    if uniquify
      param_name = "--#{@name}-#{param.name} #{param.name.upcase}"
    else
      param_name = "--#{param.name} #{param.name.upcase}"
    end
    parser.on(param_name, param.description,
              param_set(instance, param))
  end
end

#prepare_instantiate_arglist(*args) ⇒ Object

Prepares the argument list for instantiate based on init_param_list and the text arguments given:



203
204
205
206
207
208
209
# File 'lib/SciYAG/Backends/descriptions.rb', line 203

def prepare_instantiate_arglist(*args)
  target = []
  for pars in @init_param_list
    target << pars.value(args.shift)
  end
  return target
end