The GenMyModel transformations repository grows again with the addition of a new code generator for UML. This time, Ruby is the shining star. The new generator tries to cover as much as possible of the semantic you add in your class diagram.
What this generator can produce? First thing, it deals with class diagrams and gives to you the Ruby representation of the system you’ve designed. Here is a list of our Ruby code generator features:
- one file per package is created
- inner packages are generated as modules (using modules as namespace)
- class
- inheritance
- interface (generated as module)
- interface realization (as
include
) - attribute/associations using these rules:
- [no readonly – no static] attributes uses
attr_accessor
- [readonly – no static] attributes uses
attr_reader
- [readonly – static] attributes are generated as ‘CONSTANTS’
- [no readonly – static] attributes are generated as ‘@@attribute’
- if attribute cardinality is
[x..*]
-> the attribute is init as alist
(can be changed)
- [no readonly – no static] attributes uses
initialize
method is generated following these rules:- if no
initialize
is found, but there is attribute -> aninitialize
is generated - if many
initialize
is found ->initialize
with the greatest number of arguments is generated - if
initialize
parameter namex
matches an attribute name ->@x = x
is generated - if the class inherits from another one which owns an
initialize
-> asuper()
call is generated with the good arguments number
- if no
- operations generation follows these rules:
- operation body returns nothing by default (can be changed)
- operations returning
Boolean
are postfixed by a?
if their name does not include it (can be changed)
- comments in an Asciidoc format (format can be changed)
require_relative
are generated for theroot.rb
file:{your_model_name}.rb
- indentation is two spaces (no tabs)
You can find this generator in the Generators
tab:
And here is a small example that will shows part of the generator feature:
This example defines two classes with a bunch of attributes. The MyModule
package contains a Config
class which owns two static attributes: one is read-only and the other is not. For the Shape
class, only the id
attribute is read-only. The two classes Shape
and Square
define initialize
operation with parameters.
Here is my_module.rb
(note the auto-snake_case translation the generator performed).
module MyModule
class Config
MY_KEY = ""
@@MY_OTHER_KEY = ""
# static accessors
def self.MY_OTHER_KEY
@@MY_OTHER_KEY
end
end
end
The package had been translated into a module
and the class had been placed into. The two attributes have been generated according to their semantic (i.e, static and/or read-only).
And here is the model.rb
generated file:
require_relative 'my_module'
class Shape
attr_accessor :x, :y, :config
attr_reader :id
def initialize(x, y)
@id = 0
@x = x
@y = y
@config = nil
end
end
module Shape2D
def area
end
def perimeter
end
end
class Square < Shape
include Shape2D
attr_accessor :z
def initialize(x, y, z)
super(x, y)
@z = z
end
end
You can see that the classes have been ordered by “ineritance”. The Shape
class defines an initialize
method, so it uses it and the binding between parameters and attribute have been automatically generated. We can observe the same thing for the Square
class. Moreover, the initialize
method has an explicit call to super()
and parameters are directly passed to it.
Obviously, you can copy this generator and modify it as you want (it is released under MIT License). The produced code is simple “vanilla” Ruby, but the generator can be modified to match some dedicated Ruby framework. We hope it will help you in your futur developments!
Love,