umbrello API Documentation

rubycodeaccessormethod.cpp

00001 /***************************************************************************
00002                           rubycodeaccessormethod.cpp
00003                           Derived from the Java code generator by thomas
00004 
00005     begin                : Thur Jul 21 2005
00006     author               : Richard Dale
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  *   copyright (C) 2006-2007                                               *
00017  *   Umbrello UML Modeller Authors <uml-devel@uml.sf.net>                  *
00018  ***************************************************************************/
00019 
00020 // own header
00021 #include "rubycodeaccessormethod.h"
00022 
00023 // qt/kde includes
00024 #include <qregexp.h>
00025 #include <kdebug.h>
00026 
00027 // local includes
00028 #include "../attribute.h"
00029 #include "../codegenerator.h"
00030 #include "../classifiercodedocument.h"
00031 #include "../umlobject.h"
00032 #include "../umlrole.h"
00033 #include "../uml.h"
00034 
00035 #include "rubyclassifiercodedocument.h"
00036 #include "rubycodegenerationpolicy.h"
00037 #include "rubycodegenerator.h"
00038 #include "rubycodeclassfield.h"
00039 #include "rubycodedocumentation.h"
00040 
00041 // Constructors/Destructors
00042 //
00043 
00044 RubyCodeAccessorMethod::RubyCodeAccessorMethod ( CodeClassField * field, CodeAccessorMethod::AccessorType type)
00045         : CodeAccessorMethod ( field )
00046 {
00047     setType(type);
00048 
00049     // lets use full-blown comment
00050     RubyClassifierCodeDocument *rccd = dynamic_cast<RubyClassifierCodeDocument*>(field->getParentDocument());
00051     setComment(new RubyCodeDocumentation(rccd));
00052 }
00053 
00054 RubyCodeAccessorMethod::~RubyCodeAccessorMethod ( ) { }
00055 
00056 // Other methods
00057 //
00058 
00059 void RubyCodeAccessorMethod::setAttributesOnNode ( QDomDocument & doc, QDomElement & blockElement)
00060 {
00061 
00062     // set super-class attributes
00063     CodeAccessorMethod::setAttributesOnNode(doc, blockElement);
00064 
00065     // set local attributes now
00066 }
00067 
00068 void RubyCodeAccessorMethod::setAttributesFromNode( QDomElement & root)
00069 {
00070 
00071     // set attributes from superclass method the XMI
00072     CodeAccessorMethod::setAttributesFromNode(root);
00073 
00074     // load local stuff
00075 
00076 }
00077 
00078 void RubyCodeAccessorMethod::updateContent( )
00079 {
00080 
00081     CodeClassField * parentField = getParentClassField();
00082     RubyCodeClassField * rubyfield = dynamic_cast<RubyCodeClassField*>(parentField);
00083     QString fieldName = rubyfield->getFieldName();
00084     QString endLine = UMLApp::app()->getCommonPolicy()->getNewLineEndingChars();
00085 
00086     QString text = "";
00087     switch(getType()) {
00088     case CodeAccessorMethod::ADD:
00089         {
00090             int maxOccurs = rubyfield->maximumListOccurances();
00091             QString fieldType = rubyfield->getTypeName();
00092             QString indent = getIndentation();
00093             if(maxOccurs > 0)
00094                 text += "if "+fieldName+".size() < "+ QString::number(maxOccurs)+' '+endLine+indent;
00095             text += fieldName+".push(value)";
00096             if(maxOccurs > 0)
00097             {
00098                 text += endLine+"else"+endLine;
00099                 text += indent + "puts(\"ERROR: Can't add"+fieldType+" to "+fieldName+", minimum number of items reached.\")"+endLine+"end"+endLine;
00100             }
00101             break;
00102         }
00103     case CodeAccessorMethod::GET:
00104 //        text = "return "+fieldName;
00105         break;
00106     case CodeAccessorMethod::LIST:
00107         text = "return "+fieldName;
00108         break;
00109     case CodeAccessorMethod::REMOVE:
00110         {
00111             int minOccurs = rubyfield->minimumListOccurances();
00112             RubyClassifierCodeDocument * rubydoc = dynamic_cast<RubyClassifierCodeDocument*>(rubyfield->getParentDocument());
00113             QString fieldType = rubyfield->getTypeName();
00114             QString indent = getIndentation();
00115 
00116             if(minOccurs > 0)
00117                 text += "if "+fieldName+".size() >= "+ QString::number(minOccurs)+endLine+indent;
00118             text += fieldName+".delete(value)";
00119             if(minOccurs > 0)
00120             {
00121                 text += endLine+"else"+endLine;
00122                 text += indent + "puts(\"ERROR: Cant remove"+fieldType+" from "+fieldName+", minimum number of items reached.\")"+endLine+"end"+endLine;
00123             }
00124             break;
00125         }
00126     case CodeAccessorMethod::SET:
00127 //        text = fieldName+" = value";
00128         break;
00129     default:
00130         // do nothing
00131         break;
00132     }
00133 
00134     setText(text);
00135 
00136 }
00137 
00138 void RubyCodeAccessorMethod::updateMethodDeclaration()
00139 {
00140 
00141     RubyCodeClassField * rubyfield = dynamic_cast<RubyCodeClassField*>(getParentClassField());
00142     RubyClassifierCodeDocument * rubydoc = dynamic_cast<RubyClassifierCodeDocument*>(rubyfield->getParentDocument());
00143 
00144     // gather defs
00145     CodeGenerationPolicy *p = UMLApp::app()->getCommonPolicy();
00146     CodeGenerationPolicy::ScopePolicy scopePolicy = p->getAttributeAccessorScope();
00147     QString strVis = rubydoc->scopeToRubyDecl(rubyfield->getVisibility());
00148     QString fieldName = RubyCodeGenerator::cppToRubyName(rubyfield->getFieldName());
00149     QString fieldType = RubyCodeGenerator::cppToRubyType(rubyfield->getTypeName());
00150     QString objectType = rubyfield->getListObjectType();
00151     if(objectType.isEmpty())
00152         objectType = fieldName;
00153     QString endLine = p->getNewLineEndingChars();
00154 
00155     QString description = getParentObject()->getDoc();
00156     description.replace(QRegExp("m_[npb](?=[A-Z])"), "");
00157     description.replace("m_", "");
00158     description.replace(QRegExp("[\\n\\r]+[\\t ]*"), endLine);
00159 
00160     // set scope of this accessor appropriately..if its an attribute,
00161     // we need to be more sophisticated
00162     if(rubyfield->parentIsAttribute())
00163         switch (scopePolicy) {
00164         case CodeGenerationPolicy::Public:
00165         case CodeGenerationPolicy::Private:
00166         case CodeGenerationPolicy::Protected:
00167             strVis = rubydoc->scopeToRubyDecl((Uml::Visibility::Value) scopePolicy);
00168             break;
00169         default:
00170         case CodeGenerationPolicy::FromParent:
00171             // do nothing..already have taken parent value
00172             break;
00173         }
00174 
00175     // some variables we will need to populate
00176     QString headerText = "";
00177     QString methodReturnType = "";
00178     QString methodName = "";
00179     QString methodParams = "";
00180 
00181     switch(getType()) {
00182     case CodeAccessorMethod::ADD:
00183         methodName = "add"+rubydoc->capitalizeFirstLetter(fieldType);
00184         methodReturnType = "";
00185         methodParams = objectType+" value ";
00186         headerText = "Add an object of type "+objectType+" to the Array "+fieldName+endLine+description+endLine+"@return nil";
00187         setStartMethodText("def "+ methodName + '(' + methodParams + ')');
00188         setEndMethodText("end");
00189         break;
00190     case CodeAccessorMethod::GET:
00191         headerText = "Get the value of " + fieldName + endLine + description;
00192         setStartMethodText(QString("attr_reader :") + fieldName);
00193         setEndMethodText("");
00194         break;
00195     case CodeAccessorMethod::LIST:
00196         methodName = "get"+rubydoc->capitalizeFirstLetter(fieldType)+"List";
00197         methodReturnType = "";
00198         headerText = "Get the list of "+fieldName+endLine+description+endLine+"_returns_ List of "+fieldName;
00199         setStartMethodText("def "+ methodName + '(' + methodParams + ')');
00200         setEndMethodText("end");
00201         break;
00202     case CodeAccessorMethod::REMOVE:
00203         methodName = "remove"+rubydoc->capitalizeFirstLetter(fieldType);
00204         methodReturnType = "";
00205         methodParams = objectType+" value ";
00206         headerText = "Remove an object of type "+objectType+" from the List "+fieldName+endLine+description;
00207         setStartMethodText("def "+ methodName + '(' + methodParams + ')');
00208         setEndMethodText("end");
00209         break;
00210     case CodeAccessorMethod::SET:
00211         headerText = "Set the value of " + fieldName + endLine + description;
00212         setStartMethodText(QString("attr_writer :") + fieldName);
00213         setEndMethodText("");
00214         break;
00215     default:
00216         // do nothing..no idea what this is
00217         kWarning()<<"Warning: can't generate RubyCodeAccessorMethod for type: "<<getType()<<endl;
00218         break;
00219     }
00220 
00221     // set header once.
00222     if (getComment()->getText().isEmpty())
00223         getComment()->setText(headerText);
00224 
00225 }
00226 
00227 void RubyCodeAccessorMethod::update()
00228 {
00229     updateMethodDeclaration();
00230     updateContent();
00231 }
00232 
00233 #include "rubycodeaccessormethod.moc"
KDE Logo
This file is part of the documentation for umbrello Version 3.1.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Jun 26 08:07:59 2007 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003