Module: SciYAG::Backends::Descriptions::DescriptionExtend

Included in:
Filter
Defined in:
lib/SciYAG/Backends/descriptions.rb

Overview

This module should be used with extend to provide the class with descriptions functionnalities. You also need to include DescriptionInclude. Please not that all the instance methods defined here will become class methods there.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extend_object(t) ⇒ Object

Redefined so that we can add the description lists only in the base ancestor.



410
411
412
413
414
415
416
417
# File 'lib/SciYAG/Backends/descriptions.rb', line 410

def DescriptionExtend.extend_object(t)
  # First call super, to make sure the methods are here !
  super
  if t.base_ancestor?
    t.set_description_list_base
    t.set_description_hash_base
  end
end

Instance Method Details

#base_ancestor?Boolean

Tells if the current object is a base ancestor ?

Returns:

  • (Boolean)


340
341
342
343
344
345
346
# File 'lib/SciYAG/Backends/descriptions.rb', line 340

def base_ancestor?
  if superclass.respond_to?(:lookup_description_extend_ancestor)
    return false
  else
    return true
  end
end

#descriptionObject

Returns the description of the class.



318
319
320
# File 'lib/SciYAG/Backends/descriptions.rb', line 318

def description
  return @description
end

#description_hashObject

Returns the hash of descriptions associated with the base class



397
398
399
# File 'lib/SciYAG/Backends/descriptions.rb', line 397

def description_hash
  return lookup_description_extend_ancestor.description_hash_base
end

#description_hash_baseObject

Valid only in the base class



381
382
383
384
385
386
387
388
# File 'lib/SciYAG/Backends/descriptions.rb', line 381

def description_hash_base
  if base_ancestor?
    return @description_hash
  else
    raise "This function should only be called from the " +
      "base ancestor"
  end
end

#description_listObject

Returns the list of descriptions associated with the base class



376
377
378
# File 'lib/SciYAG/Backends/descriptions.rb', line 376

def description_list
  return lookup_description_extend_ancestor.description_list_base
end

#description_list_baseObject

Valid only in the base class



359
360
361
362
363
364
365
366
# File 'lib/SciYAG/Backends/descriptions.rb', line 359

def description_list_base
  if base_ancestor?
    return @description_list
  else
    raise "This function should only be called from the " +
      "base ancestor"
  end
end

#inherit(*names) ⇒ Object

The parameters the class should inherit from its direct parents (which could have in turn inherited them…).



438
439
440
441
442
443
444
445
446
447
448
449
450
451
# File 'lib/SciYAG/Backends/descriptions.rb', line 438

def inherit(*names)
  if self.superclass.respond_to?(:description)
    parents_params = self.superclass.description.param_hash
    for n in names
      if parents_params.key?(n)
        description.add_param(parents_params[n])
      else
        warn "Param #{n} not found"
      end
    end
  else
    warn "The parent class has no description"
  end
end

#init_param(name, long_name, type, desc) ⇒ Object

Creates a parameter which has to be used for instancitation



459
460
461
462
# File 'lib/SciYAG/Backends/descriptions.rb', line 459

def init_param(name, long_name, type, desc)
  p = param_noaccess(nil, nil, name, long_name, type, desc)
  init_params(p)
end

#init_params(*params) ⇒ Object

Adds the params to the list of init params. Better use init_param



454
455
456
# File 'lib/SciYAG/Backends/descriptions.rb', line 454

def init_params(*params)
  description.init_params(*params)
end

#lookup_description_extend_ancestorObject

Retrieves the most ancient ancestor that has included DescriptionExtend.



350
351
352
353
354
355
356
# File 'lib/SciYAG/Backends/descriptions.rb', line 350

def lookup_description_extend_ancestor
  t = self
  while not t.base_ancestor?
    t = t.superclass
  end
  return t
end

#param(symbol, name, long_name, type = String, desc = "") ⇒ Object

This shortcut creates an accessor for the given symbol and registers it at the same time. Use if after describe. Something like

param :size, "size", "Size", Integer , "The size of the backend"

should be perfectly fine.



426
427
428
429
430
431
432
433
434
# File 'lib/SciYAG/Backends/descriptions.rb', line 426

def param(symbol, name, long_name, type = String, 
          desc = "")
  # We use the accessor = symbol.
  param_noaccess((symbol.to_s + "=").to_sym, 
                 symbol, name, long_name, 
                 type, desc)
  # Creates an accessor
  attr_accessor symbol
end

#param_noaccess(writer, reader, name, long_name, type = String, desc = "") ⇒ Object

Like param, but doesn’t define an accessor.



329
330
331
332
333
334
335
336
337
# File 'lib/SciYAG/Backends/descriptions.rb', line 329

def param_noaccess(writer, reader, name, long_name, type = String, 
                   desc = "")
  raise "Use describe first" if description.nil? 
  param = Descriptions::Parameter.new(name, writer, reader,
                                      long_name, 
                                      type, desc)
  description.add_param(param)
  return param
end

#register_description(desc) ⇒ Object

Registers a description in the base ancestor.



403
404
405
406
# File 'lib/SciYAG/Backends/descriptions.rb', line 403

def register_description(desc)
  description_list << desc
  description_hash[desc.name] = desc
end

#set_description(desc) ⇒ Object

Sets the description of the class. Classes should provide an easier interface for that.



324
325
326
# File 'lib/SciYAG/Backends/descriptions.rb', line 324

def set_description(desc)
  @description = desc
end

#set_description_hash_baseObject

Valid only in the base class



391
392
393
# File 'lib/SciYAG/Backends/descriptions.rb', line 391

def set_description_hash_base()
  @description_hash = {}
end

#set_description_list_baseObject

Valid only in the base class



369
370
371
# File 'lib/SciYAG/Backends/descriptions.rb', line 369

def set_description_list_base()
  @description_list = []
end