• 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
rubycodeaccessormethod.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 "rubycodeaccessormethod.h"
15 
16 // local includes
17 #include "attribute.h"
18 #include "codegenerator.h"
19 #include "classifiercodedocument.h"
20 #include "codegen_utils.h"
21 #include "debug_utils.h"
22 #include "rubyclassifiercodedocument.h"
23 #include "rubycodegenerationpolicy.h"
24 #include "rubycodegenerator.h"
25 #include "rubycodeclassfield.h"
26 #include "rubycodedocumentation.h"
27 #include "umlobject.h"
28 #include "umlrole.h"
29 #include "uml.h"
30 
31 // qt includes
32 #include <QRegExp>
33 
37 RubyCodeAccessorMethod::RubyCodeAccessorMethod(CodeClassField * field, CodeAccessorMethod::AccessorType type)
38  : CodeAccessorMethod(field)
39 {
40  setType(type);
41 
42  // lets use full-blown comment
43  RubyClassifierCodeDocument *rccd = dynamic_cast<RubyClassifierCodeDocument*>(field->getParentDocument());
44  setComment(new RubyCodeDocumentation(rccd));
45 }
46 
50 RubyCodeAccessorMethod::~RubyCodeAccessorMethod()
51 {
52 }
53 
58 void RubyCodeAccessorMethod::setAttributesOnNode(QDomDocument& doc, QDomElement& blockElement)
59 {
60  // set super-class attributes
61  CodeAccessorMethod::setAttributesOnNode(doc, blockElement);
62 
63  // set local attributes now
64 }
65 
70 void RubyCodeAccessorMethod::setAttributesFromNode(QDomElement& root)
71 {
72  // set attributes from superclass method the XMI
73  CodeAccessorMethod::setAttributesFromNode(root);
74 
75  // load local stuff
76 }
77 
78 void RubyCodeAccessorMethod::updateContent()
79 {
80  CodeClassField * parentField = getParentClassField();
81  RubyCodeClassField * rubyfield = dynamic_cast<RubyCodeClassField*>(parentField);
82  QString fieldName = rubyfield->getFieldName();
83  QString endLine = UMLApp::app()->commonPolicy()->getNewLineEndingChars();
84 
85  QString text = "";
86  switch(getType()) {
87  case CodeAccessorMethod::ADD:
88  {
89  int maxOccurs = rubyfield->maximumListOccurances();
90  QString fieldType = rubyfield->getTypeName();
91  QString indent = getIndentation();
92  if(maxOccurs > 0)
93  text += "if "+fieldName+".size() < "+ QString::number(maxOccurs)+' '+endLine+indent;
94  text += fieldName+".push(value)";
95  if(maxOccurs > 0)
96  {
97  text += endLine+"else"+endLine;
98  text += indent + "puts(\"ERROR: Cannot add" + fieldType + " to " + fieldName
99  + ", minimum number of items reached.\")" + endLine + "end" + endLine;
100  }
101  break;
102  }
103  case CodeAccessorMethod::GET:
104 // text = "return "+fieldName;
105  break;
106  case CodeAccessorMethod::LIST:
107  text = "return "+fieldName;
108  break;
109  case CodeAccessorMethod::REMOVE:
110  {
111  int minOccurs = rubyfield->minimumListOccurances();
112  QString fieldType = rubyfield->getTypeName();
113  QString indent = getIndentation();
114 
115  if(minOccurs > 0)
116  text += "if "+fieldName+".size() >= "+ QString::number(minOccurs)+endLine+indent;
117  text += fieldName+".delete(value)";
118  if(minOccurs > 0)
119  {
120  text += endLine+"else"+endLine;
121  text += indent + "puts(\"ERROR: Cant remove"+fieldType+" from "+fieldName+", minimum number of items reached.\")"+endLine+"end"+endLine;
122  }
123  break;
124  }
125  case CodeAccessorMethod::SET:
126 // text = fieldName+" = value";
127  break;
128  default:
129  // do nothing
130  break;
131  }
132 
133  setText(text);
134 }
135 
136 void RubyCodeAccessorMethod::updateMethodDeclaration()
137 {
138  RubyCodeClassField * rubyfield = dynamic_cast<RubyCodeClassField*>(getParentClassField());
139 
140  // gather defs
141  CodeGenerationPolicy *p = UMLApp::app()->commonPolicy();
142  Uml::Visibility::Enum scopePolicy = p->getAttributeAccessorScope();
143  QString strVis = Uml::Visibility::toString(rubyfield->getVisibility());
144  QString fieldName = RubyCodeGenerator::cppToRubyName(rubyfield->getFieldName());
145  QString fieldType = RubyCodeGenerator::cppToRubyType(rubyfield->getTypeName());
146  QString objectType = rubyfield->getListObjectType();
147  if(objectType.isEmpty())
148  objectType = fieldName;
149  QString endLine = p->getNewLineEndingChars();
150 
151  QString description = getParentObject()->doc();
152  description.remove(QRegExp("m_[npb](?=[A-Z])"));
153  description.remove("m_");
154  description.replace(QRegExp("[\\n\\r]+[\\t ]*"), endLine);
155 
156  // set scope of this accessor appropriately..if its an attribute,
157  // we need to be more sophisticated
158  if(rubyfield->parentIsAttribute())
159  switch (scopePolicy) {
160  case Uml::Visibility::Public:
161  case Uml::Visibility::Private:
162  case Uml::Visibility::Protected:
163  strVis = Uml::Visibility::toString(scopePolicy);
164  break;
165  default:
166  case Uml::Visibility::FromParent:
167  // do nothing..already have taken parent value
168  break;
169  }
170 
171  // some variables we will need to populate
172  QString headerText = "";
173  QString methodReturnType = "";
174  QString methodName = "";
175  QString methodParams = "";
176 
177  switch(getType()) {
178  case CodeAccessorMethod::ADD:
179  methodName = "add" + Codegen_Utils::capitalizeFirstLetter(fieldType);
180  methodReturnType = "";
181  methodParams = objectType+" value ";
182  headerText = "Add an object of type "+objectType+" to the Array "+fieldName+endLine+description+endLine+"@return nil";
183  setStartMethodText("def "+ methodName + '(' + methodParams + ')');
184  setEndMethodText("end");
185  break;
186  case CodeAccessorMethod::GET:
187  headerText = "Get the value of " + fieldName + endLine + description;
188  setStartMethodText(QString("attr_reader :") + fieldName);
189  setEndMethodText("");
190  break;
191  case CodeAccessorMethod::LIST:
192  methodName = "get" + Codegen_Utils::capitalizeFirstLetter(fieldType)+"List";
193  methodReturnType = "";
194  headerText = "Get the list of "+fieldName+endLine+description+endLine+"_returns_ List of "+fieldName;
195  setStartMethodText("def "+ methodName + '(' + methodParams + ')');
196  setEndMethodText("end");
197  break;
198  case CodeAccessorMethod::REMOVE:
199  methodName = "remove" + Codegen_Utils::capitalizeFirstLetter(fieldType);
200  methodReturnType = "";
201  methodParams = objectType+" value ";
202  headerText = "Remove an object of type "+objectType+" from the List "+fieldName+endLine+description;
203  setStartMethodText("def "+ methodName + '(' + methodParams + ')');
204  setEndMethodText("end");
205  break;
206  case CodeAccessorMethod::SET:
207  headerText = "Set the value of " + fieldName + endLine + description;
208  setStartMethodText(QString("attr_writer :") + fieldName);
209  setEndMethodText("");
210  break;
211  default:
212  // do nothing..no idea what this is
213  uWarning() << "Warning: can not generate RubyCodeAccessorMethod for type: " << getType();
214  break;
215  }
216 
217  // set header once.
218  if (getComment()->getText().isEmpty())
219  getComment()->setText(headerText);
220 }
221 
225 void RubyCodeAccessorMethod::update()
226 {
227  updateMethodDeclaration();
228  updateContent();
229 }
230 
231 #include "rubycodeaccessormethod.moc"
CodeAccessorMethod::setType
void setType(AccessorType type)
Set the type of accessor method this is.
Definition: codeaccessormethod.cpp:72
umlobject.h
CodeClassField
class CodeClassField a special type of parameter.
Definition: codeclassfield.h:29
OwnedCodeBlock::getParentObject
UMLObject * getParentObject()
Get the value of m_parentObject.
Definition: ownedcodeblock.cpp:64
CodeMethodBlock::setEndMethodText
void setEndMethodText(const QString &value)
Set the ending text that finishes this method after the body is printed.
Definition: codemethodblock.cpp:66
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
CodeAccessorMethod::REMOVE
Definition: codeaccessormethod.h:32
Uml::Visibility::Enum
Enum
Definition: basictypes.h:56
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
umlrole.h
CodeGenerationPolicy::getNewLineEndingChars
QString getNewLineEndingChars() const
Utility function to get the actual characters.
Definition: codegenerationpolicy.cpp:248
RubyCodeDocumentation
class RubyCodeDocumentation A Ruby code comment.
Definition: rubycodedocumentation.h:29
rubycodegenerationpolicy.h
RubyCodeClassField::getTypeName
QString getTypeName()
Get the value of m_dialog.
Definition: rubycodeclassfield.cpp:89
CodeAccessorMethod::getType
AccessorType getType()
Utility method to get the value of the parent object of the parent classifield.
Definition: codeaccessormethod.cpp:64
RubyCodeAccessorMethod::setAttributesOnNode
virtual void setAttributesOnNode(QDomDocument &doc, QDomElement &blockElement)
Set attributes of the node that represents this class in the XMI document.
Definition: rubycodeaccessormethod.cpp:58
CodeAccessorMethod::getParentClassField
CodeClassField * getParentClassField()
Get the value of m_parentclassfield.
Definition: codeaccessormethod.cpp:40
CodeAccessorMethod::AccessorType
AccessorType
Definition: codeaccessormethod.h:32
UMLApp::app
static UMLApp * app()
Get the last created instance of this class.
Definition: uml.cpp:206
CodeGenerationPolicy
class CodeGenerationPolicy This class describes the code generation policy for this project...
Definition: codegenerationpolicy.h:29
uWarning
#define uWarning()
Definition: debug_utils.h:97
RubyCodeClassField::getFieldName
QString getFieldName()
Definition: rubycodeclassfield.cpp:43
debug_utils.h
CodeParameter::getParentDocument
ClassifierCodeDocument * getParentDocument()
Get the parent Code Document.
Definition: codeparameter.cpp:133
description
static const char description[]
Definition: docgenerators/main.cpp:37
CodeClassField::minimumListOccurances
int minimumListOccurances()
Find the minimum number of things that can occur in an association If mistakenly called on attribute ...
Definition: codeclassfield.cpp:325
Uml::Visibility::Private
Definition: basictypes.h:58
codegenerator.h
Codegen_Utils::capitalizeFirstLetter
QString capitalizeFirstLetter(const QString &string)
Return the input string with the first letter capitalized.
Definition: codegen_utils.cpp:421
CodeAccessorMethod::SET
Definition: codeaccessormethod.h:32
attribute.h
CodeClassField::parentIsAttribute
bool parentIsAttribute() const
Get the value of m_isAbstract.
Definition: codeclassfield.cpp:126
RubyClassifierCodeDocument
class RubyClassifierCodeDocument A Ruby UMLClassifier Code Document.
Definition: rubyclassifiercodedocument.h:36
CodeAccessorMethod::LIST
Definition: codeaccessormethod.h:32
CodeAccessorMethod::setAttributesOnNode
virtual void setAttributesOnNode(QDomDocument &doc, QDomElement &blockElement)
Set attributes of the node that represents this class in the XMI document.
Definition: codeaccessormethod.cpp:123
CodeClassField::maximumListOccurances
int maximumListOccurances()
Find the maximum number of things that can occur in an association If mistakenly called on attribute ...
Definition: codeclassfield.cpp:349
rubycodeaccessormethod.h
rubycodedocumentation.h
Uml::Visibility::toString
QString toString(Enum item, bool mnemonic)
Convert Visibility item into QString representation.
Definition: basictypes.cpp:99
CodeAccessorMethod
Definition: codeaccessormethod.h:20
CodeAccessorMethod::ADD
Definition: codeaccessormethod.h:32
UMLApp::commonPolicy
CodeGenerationPolicy * commonPolicy() const
Returns the default code generation policy.
Definition: uml.cpp:2132
Uml::Visibility::Public
Definition: basictypes.h:57
RubyCodeAccessorMethod::updateContent
virtual void updateContent()
This is the method called from within syncToparent() to update the body of the method.
Definition: rubycodeaccessormethod.cpp:78
CodeAccessorMethod::setAttributesFromNode
virtual void setAttributesFromNode(QDomElement &element)
Set the class attributes of this object from the passed element node.
Definition: codeaccessormethod.cpp:137
CodeGenerationPolicy::getAttributeAccessorScope
Uml::Visibility::Enum getAttributeAccessorScope()
Get the value of m_attributeAccessorScope.
Definition: codegenerationpolicy.cpp:384
rubycodeclassfield.h
Import_Rose::methodName
void methodName(const QString &m)
Definition: import_rose.cpp:43
rubyclassifiercodedocument.h
CodeParameter::getVisibility
Uml::Visibility::Enum getVisibility() const
Utility method to get the value of parent object scope.
Definition: codeparameter.cpp:89
RubyCodeAccessorMethod::RubyCodeAccessorMethod
RubyCodeAccessorMethod(CodeClassField *field, CodeAccessorMethod::AccessorType type)
Constructor.
Definition: rubycodeaccessormethod.cpp:37
CodeAccessorMethod::GET
Definition: codeaccessormethod.h:32
RubyCodeAccessorMethod::setAttributesFromNode
virtual void setAttributesFromNode(QDomElement &element)
Set the class attributes of this object from the passed element node.
Definition: rubycodeaccessormethod.cpp:70
RubyCodeAccessorMethod::update
void update()
Must be called before this object is usable.
Definition: rubycodeaccessormethod.cpp:225
RubyCodeAccessorMethod::~RubyCodeAccessorMethod
virtual ~RubyCodeAccessorMethod()
Empty Destructor.
Definition: rubycodeaccessormethod.cpp:50
classifiercodedocument.h
rubycodegenerator.h
Uml::Visibility::FromParent
Definition: basictypes.h:61
CodeClassField::getListObjectType
QString getListObjectType()
Definition: codeclassfield.cpp:112
CodeBlockWithComments::setComment
void setComment(CodeComment *object)
Set the Comment object.
Definition: codeblockwithcomments.cpp:38
codegen_utils.h
TextBlock::getText
QString getText() const
Get the value of m_text The actual text of this code block.
Definition: textblock.cpp:99
Uml::Visibility::Protected
Definition: basictypes.h:59
RubyCodeAccessorMethod::updateMethodDeclaration
virtual void updateMethodDeclaration()
This is the method called from within syncToparent().
Definition: rubycodeaccessormethod.cpp:136
TextBlock::getIndentation
static QString getIndentation()
Get how much a single "level" of indentation will actually indent.
Definition: textblock.cpp:182
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
RubyCodeClassField
Definition: rubycodeclassfield.h:22
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