# description.rb : a system for # Copyright (C) 2006, 2009 Vincent Fourmond # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA require 'ctioga2/utils' require 'ctioga2/data/backends/parameter' require 'ctioga2/commands/commands' require 'ctioga2/commands/groups' module CTioga2 Version::register_svn_info('$Revision: 155 $', '$Date: 2010-06-21 21:41:32 +0200 (Mon, 21 Jun 2010) $') module Data module Backends # The Description class is a meta-information class that records several # informations about the class: # # * a basic name, code-like, which is used mainly for internal # purposes; # * a long name, more explanatory, in proper English (or any # other language. By the way, it should definitely be # translated in a production environment); # * a description itself, some small text describing the nature # of the class; # * a list of all the Parameters that are important for the class, # and enough to recreate the state of an object. # # This class is fairly general, and can be subclassed to fit specific # needs. An example is in the SciYAG/Backend system, where the description # of a backend is derived from Description. # # To make use of the Description system for a class, use the following: # # class SomeClass # extend MetaBuilder::DescriptionExtend # include MetaBuilder::DescriptionInclude # # describe 'someclass', 'Some nice class', < :integer}, # "The size !!" def param(writer, reader, name, long_name, type, desc = "") raise "Use describe first" if description.nil? param = nil param = Parameter.new(name, writer, reader, long_name, type, desc) description.add_param(param) return param end # The same as #param, but creates a attr_reader in addition def param_reader(writer, reader, name, long_name, type, desc = "") attr_reader reader return param(writer, reader, name, long_name, type, desc) end # The same as #param, except that _writer_ is made from # _symbol_ by appending a = at the end. An attr_writer is # created for the _symbol_. def param_writer(symbol, name, long_name, type, desc = "") writer = (symbol.to_s + '=').to_sym attr_writer symbol return param(writer, symbol, name, long_name, type, desc) end # The same as #param_writer, except that an attr_writer is # created for the _symbol_ instead of only a attr_writer. The # most useful of the four methods. Typical use: # # param_accessor :name, 'name', "Object name", {:type => :string}, # "The name of the object" def param_accessor(symbol, name, long_name, type, desc = "") writer = (symbol.to_s + '=').to_sym attr_accessor symbol return param(writer, symbol, name, long_name, type, desc) end # Creates a description attached to the current class. It # needs to be used before anything else. def describe(name, longname = name, desc = "") d = Description.new(self, name, longname, desc) set_description(d) end # Imports the given parameters directly from the parent class. # This function is quite naive and will not look further than # the direct #superclass. def inherit_parameters(*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 end end end end