• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdesdk API Reference
  • KDE Home
  • Contact Us
 

umbrello/umbrello

  • sources
  • kde-4.12
  • kdesdk
  • umbrello
  • umbrello
  • codegenerators
  • ruby
rubycodeoperation.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * This program is free software; you can redistribute it and/or modify *
3  * it under the terms of the GNU General Public License as published by *
4  * the Free Software Foundation; either version 2 of the License, or *
5  * (at your option) any later version. *
6  * *
7  * copyright (C) 2005 *
8  * Richard Dale <Richard_Dale@tipitina.demon.co.uk> *
9  * copyright (C) 2006-2013 *
10  * Umbrello UML Modeller Authors <umbrello-devel@kde.org> *
11  ***************************************************************************/
12 
13 // own header
14 #include "rubycodeoperation.h"
15 
16 // local includes
17 #include "rubyclassifiercodedocument.h"
18 #include "rubycodedocumentation.h"
19 #include "rubycodegenerator.h"
20 #include "uml.h"
21 
22 // qt includes
23 #include <QRegExp>
24 
25 RubyCodeOperation::RubyCodeOperation (RubyClassifierCodeDocument * doc, UMLOperation *parent, const QString & body, const QString & comment)
26  : CodeOperation (doc, parent, body, comment)
27 {
28  // lets not go with the default comment and instead use
29  // full-blown ruby documentation object instead
30  setComment(new RubyCodeDocumentation(doc));
31 
32  // these things never change..
33  setOverallIndentationLevel(1);
34 }
35 
36 RubyCodeOperation::~RubyCodeOperation()
37 {
38 }
39 
40 // we basically want to update the doc and start text of this method
41 void RubyCodeOperation::updateMethodDeclaration()
42 {
43  CodeDocument * doc = getParentDocument();
44  RubyClassifierCodeDocument * rubydoc = dynamic_cast<RubyClassifierCodeDocument*>(doc);
45  UMLClassifier *c = rubydoc->getParentClassifier();
46  UMLOperation * o = getParentOperation();
47  bool isInterface = rubydoc->getParentClassifier()->isInterface();
48  QString endLine = getNewLineEndingChars();
49 
50  // now, the starting text.
51  //:UNUSED: QString strVis = o->visibility().toString();
52  // no return type for constructors
53  QString fixedReturn = RubyCodeGenerator::cppToRubyType(o->getTypeName());
54  QString returnType = o->isConstructorOperation() ? QString("") : (fixedReturn + QString(" "));
55  QString methodName = o->name();
56 
57  QString RubyClassName = rubydoc->getRubyClassName(c->name());
58 
59  // Skip destructors, and operator methods which
60  // can't be defined in ruby
61  if (methodName.startsWith('~')
62  || QRegExp("operator\\s*(=|--|\\+\\+|!=)$").exactMatch(methodName))
63  {
64  getComment()->setText("");
65  return;
66  }
67 
68  if (RubyClassName == methodName) {
69  methodName = "initialize";
70  }
71 
72  methodName.remove(QRegExp("operator\\s*"));
73  methodName = methodName.mid(0, 1).toLower() + methodName.mid(1);
74 
75  QString paramStr = QString("");
76  QStringList commentedParams;
77 
78  // assemble parameters
79  UMLAttributeList list = getParentOperation()->getParmList();
80  int nrofParam = list.count();
81  int paramNum = 0;
82  foreach (UMLAttribute* parm, list) {
83  QString paramName = RubyCodeGenerator::cppToRubyName(parm->name());
84  paramStr += paramName;
85  if (! parm->getInitialValue().isEmpty()) {
86  paramStr += QString(" = ") + RubyCodeGenerator::cppToRubyType(parm->getInitialValue());
87  }
88  paramNum++;
89 
90  if (paramNum != nrofParam)
91  paramStr += ", ";
92  }
93 
94  QString startText;
95  if (isInterface) {
96  // Assume 'isInterface' means a module in Ruby, so
97  // generate module methods
98  startText = "def "+ RubyClassName + '.' + methodName + '(' + paramStr +')';
99  } else {
100  startText = "def "+ methodName + '(' + paramStr +')';
101  }
102 
103  startText += "";
104  setEndMethodText("end");
105 
106  setStartMethodText(startText);
107 
108  // Lastly, for text content generation, we fix the comment on the
109  // operation, IF the codeop is autogenerated & currently empty
110  QString comment = o->doc();
111 
112  if (comment.isEmpty()) {
113  if (contentType() == CodeBlock::AutoGenerated) {
114  UMLAttributeList parameters = o->getParmList();
115  foreach (UMLAttribute* currentAtt, parameters) {
116  comment += endLine + "* _" + currentAtt->name() + "_ ";
117  comment += (' ' + currentAtt->doc().replace(QRegExp("[\\n\\r]+[\\t ]*"), endLine + " "));
118  }
119  // add a returns statement too
120  if(!returnType.isEmpty() && !QRegExp("^void\\s*$").exactMatch(returnType))
121  comment += endLine + "* _returns_ " + returnType + ' ';
122  getComment()->setText(comment);
123  }
124  } else {
125  comment.replace(QRegExp("[\\n\\r]+ *"), endLine);
126  comment.replace(QRegExp("[\\n\\r]+\\t*"), endLine);
127 
128  comment.replace(" m_", " ");
129  comment.replace(QRegExp("\\s[npb](?=[A-Z])"), " ");
130  QRegExp re_params("@param (\\w)(\\w*)");
131  int pos = re_params.indexIn(comment);
132  while (pos != -1) {
133  comment.replace(re_params.cap(0),
134  QString("@param _") + re_params.cap(1).toLower() + re_params.cap(2) + '_');
135  commentedParams.append(re_params.cap(1).toLower() + re_params.cap(2));
136 
137  pos += re_params.matchedLength() + 3;
138  pos = re_params.indexIn(comment, pos);
139  }
140 
141  UMLAttributeList parameters = o->getParmList();
142  foreach (UMLAttribute* currentAtt, parameters) {
143  // Only write an individual @param entry if one hasn't been found already
144  // in the main doc comment
145  if (commentedParams.contains(RubyCodeGenerator::cppToRubyName(currentAtt->name())) == 0) {
146  comment += (endLine + "@param _" + RubyCodeGenerator::cppToRubyName(currentAtt->name()) + '_');
147  if (currentAtt->doc().isEmpty()) {
148  comment += (' ' + RubyCodeGenerator::cppToRubyType(currentAtt->getTypeName()));
149  } else {
150  comment += (' ' + currentAtt->doc().replace(QRegExp("[\\n\\r]+[\\t ]*"), endLine + " "));
151  }
152  }
153  }
154 
155  comment.remove("@ref ");
156  comment.replace("@param", "*");
157  comment.replace("@return", "* _returns_");
158 
159  // All lines after the first one starting with '*' in the doc comment
160  // must be indented correctly. If they aren't a list
161  // item starting with '*', then indent the text with
162  // two spaces, ' ', to line up with the list item.
163  pos = comment.indexOf(endLine + '*');
164  if (pos != -1) {
165  pos += endLine.length() + 1;
166  pos = comment.indexOf(endLine, pos);
167  }
168 
169  while (pos > 0) {
170  pos += endLine.length();
171  if (comment[pos] != '*') {
172  comment.insert(pos, " ");
173  pos += 2;
174  }
175 
176  pos = comment.indexOf(endLine, pos);
177  }
178 
179  QString typeStr = RubyCodeGenerator::cppToRubyType(o->getTypeName());
180  if (!typeStr.isEmpty()
181  && !QRegExp("^void\\s*$").exactMatch(typeStr)
182  && comment.contains("_returns_") == 0)
183  {
184  comment += endLine + "* _returns_ " + typeStr;
185  }
186 
187  getComment()->setText(comment);
188  }
189 
190  // In Java, for interfaces..we DON'T write out non-public
191  // method declarations. And for Ruby modules?
192  if (isInterface) {
193  UMLOperation * o = getParentOperation();
194  if(o->visibility() != Uml::Visibility::Public)
195  setWriteOutText(false);
196  }
197 
198 }
199 
200 int RubyCodeOperation::lastEditableLine()
201 {
202  ClassifierCodeDocument * doc = dynamic_cast<ClassifierCodeDocument*>(getParentDocument());
203  if(doc->parentIsInterface())
204  return -1; // very last line is NOT editable as its a one-line declaration w/ no body in
205  // an interface.
206  return 0;
207 }
208 
209 #include "rubycodeoperation.moc"
RubyClassifierCodeDocument::getRubyClassName
QString getRubyClassName(const QString &name)
Definition: rubyclassifiercodedocument.cpp:87
CodeMethodBlock::setEndMethodText
void setEndMethodText(const QString &value)
Set the ending text that finishes this method after the body is printed.
Definition: codemethodblock.cpp:66
UMLClassifier
This class defines the non-graphical information required for a UML Classifier (ie a class or interfa...
Definition: classifier.h:39
CodeMethodBlock::setStartMethodText
void setStartMethodText(const QString &value)
Set the starting text that begins this method before the body is printed.
Definition: codemethodblock.cpp:58
CodeBlockWithComments::getComment
CodeComment * getComment() const
Get the Comment object.
Definition: codeblockwithcomments.cpp:46
RubyCodeOperation::updateMethodDeclaration
void updateMethodDeclaration()
This is the method called from within syncToparent().
Definition: rubycodeoperation.cpp:41
RubyCodeOperation::lastEditableLine
virtual int lastEditableLine()
Definition: rubycodeoperation.cpp:200
ClassifierCodeDocument
class ClassifierCodeDocument A CodeDocument which represents a UMLClassifier (e.g.
Definition: classifiercodedocument.h:33
UMLAttribute::getInitialValue
QString getInitialValue() const
Returns The initial value of the UMLAttribute.
Definition: attribute.cpp:98
UMLObject::visibility
Uml::Visibility::Enum visibility() const
Returns the visibility of the object.
Definition: umlobject.cpp:435
RubyCodeGenerator::cppToRubyName
static QString cppToRubyName(const QString &cppName)
Convert C++ names such as 'm_foobar' or pFoobar to just 'foobar' for ruby.
Definition: rubycodegenerator.cpp:134
TextBlock::setText
void setText(const QString &text)
Set the value of m_text The actual text of this code block.
Definition: textblock.cpp:80
CodeBlock::AutoGenerated
the content was generated by code generation itself
Definition: codeblock.h:28
RubyCodeDocumentation
class RubyCodeDocumentation A Ruby code comment.
Definition: rubycodedocumentation.h:29
RubyCodeOperation::~RubyCodeOperation
virtual ~RubyCodeOperation()
Empty Destructor.
Definition: rubycodeoperation.cpp:36
CodeBlockWithComments::setOverallIndentationLevel
void setOverallIndentationLevel(int level)
A utility method that causes the comment and body of the code block to have the same indentation leve...
Definition: codeblockwithcomments.cpp:163
UMLAttribute
This class is used to set up information for an attribute.
Definition: attribute.h:27
UMLAttributeList
This sub-class adds copyInto and clone to the QPtrList base class.
Definition: umlattributelist.h:26
RubyCodeOperation::RubyCodeOperation
RubyCodeOperation(RubyClassifierCodeDocument *doc, UMLOperation *op, const QString &body="", const QString &comment="")
Empty Constructor.
Definition: rubycodeoperation.cpp:25
CodeBlock::contentType
ContentType contentType() const
Get the value of m_contentType specifies whether the content (text) of this object was generated by t...
Definition: codeblock.cpp:54
ClassifierCodeDocument::getParentClassifier
UMLClassifier * getParentClassifier()
Get the value of m_parentclassifier.
Definition: classifiercodedocument.cpp:271
TextBlock::getNewLineEndingChars
static QString getNewLineEndingChars()
Get the new line chars which ends the line.
Definition: textblock.cpp:172
UMLClassifier::isInterface
bool isInterface() const
Returns true if this classifier represents an interface.
Definition: classifier.cpp:112
CodeOperation
Definition: codeoperation.h:23
RubyClassifierCodeDocument
class RubyClassifierCodeDocument A Ruby UMLClassifier Code Document.
Definition: rubyclassifiercodedocument.h:36
ClassifierCodeDocument::parentIsInterface
bool parentIsInterface()
Return if the parent classifier is an interface.
Definition: classifiercodedocument.cpp:413
TextBlock::setWriteOutText
void setWriteOutText(bool write)
Set the value of m_writeOutText Whether or not to include the text of this TextBlock into a file...
Definition: textblock.cpp:131
CodeDocument
A document containing the code for one file.
Definition: codedocument.h:32
rubycodedocumentation.h
rubycodeoperation.h
CodeMethodBlock::getParentDocument
CodeDocument * getParentDocument()
Get the parent code document.
Definition: codemethodblock.cpp:33
Uml::Visibility::Public
Definition: basictypes.h:57
CodeOperation::getParentOperation
UMLOperation * getParentOperation()
Add a Parameter object to the m_parameterVector List.
Definition: codeoperation.cpp:64
UMLOperation
This class represents an operation in the UML model.
Definition: operation.h:24
Import_Rose::methodName
void methodName(const QString &m)
Definition: import_rose.cpp:43
rubyclassifiercodedocument.h
UMLObject::name
QString name() const
Returns a copy of m_name.
Definition: umlobject.cpp:185
UMLOperation::isConstructorOperation
bool isConstructorOperation()
Returns whether this operation is a constructor.
Definition: operation.cpp:352
rubycodegenerator.h
CodeBlockWithComments::setComment
void setComment(CodeComment *object)
Set the Comment object.
Definition: codeblockwithcomments.cpp:38
UMLClassifierListItem::getTypeName
virtual QString getTypeName() const
Returns the type name of the UMLClassifierListItem.
Definition: classifierlistitem.cpp:110
RubyCodeGenerator::cppToRubyType
static QString cppToRubyType(const QString &cppType)
Convert a C++ type such as 'int' or 'QWidget' to ruby types Integer and Qt::Widget.
Definition: rubycodegenerator.cpp:110
uml.h
UMLObject::doc
QString doc() const
Returns the documentation for the object.
Definition: umlobject.cpp:404
UMLOperation::getParmList
UMLAttributeList getParmList() const
Returns a list of parameters.
Definition: operation.cpp:171
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:06:00 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

umbrello/umbrello

Skip menu "umbrello/umbrello"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • okteta
  • umbrello
  •   umbrello

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal