Let Your UML Shine with Ruby

Ruby Code Generator

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 readonlyno static] attributes uses attr_accessor
    • [readonlyno static] attributes uses attr_reader
    • [readonlystatic] attributes are generated as ‘CONSTANTS’
    • [no readonlystatic] attributes are generated as ‘@@attribute’
    • if attribute cardinality is [x..*] -> the attribute is init as a list (can be changed)
  • initialize method is generated following these rules:
    • if no initialize is found, but there is attribute -> an initialize is generated
    • if many initialize is found -> initialize with the greatest number of arguments is generated
    • if initialize parameter name x matches an attribute name -> @x = x is generated
    • if the class inherits from another one which owns an initialize -> a super() call is generated with the good arguments number
  • 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 the root.rb file: {your_model_name}.rb
  • indentation is two spaces (no tabs)

You can find this generator in the Generators tab:

GenMyModel UML Model Transformations

The generator in our sample generator repository

And here is a small example that will shows part of the generator feature:

Model Example

Small Model Example

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

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

module Shape2D
  def area

  def perimeter

class Square < Shape
  include Shape2D

  attr_accessor :z

  def initialize(x, y, z)
    super(x, y)
    @z = z

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!


Tweet about this on TwitterShare on FacebookShare on Google+Share on LinkedIn

About the author

vince Team

Music, cards and beer lover...

Also PhD in Computer Science specialized in software modeling, code generation and model driven development. Interest/work in code generation, reverse engineering techniques, interpretation and compilation.