Class: RGen::ECore::ECoreToRuby

Inherits:
Object
  • Object
show all
Defined in:
lib/rgen/ecore/ecore_to_ruby.rb

Overview

ECoreToRuby can turn ECore models into their Ruby metamodel representations

Defined Under Namespace

Classes: FeatureWrapper

Instance Method Summary collapse

Constructor Details

#initializeECoreToRuby

Returns a new instance of ECoreToRuby.



10
11
12
13
14
# File 'lib/rgen/ecore/ecore_to_ruby.rb', line 10

def initialize
  @modules = {}
  @classifiers = {}
  @features_added = {}
end

Instance Method Details

#add_features(eclass) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/rgen/ecore/ecore_to_ruby.rb', line 204

def add_features(eclass)
  return false if @features_added[eclass]
  c = @classifiers[eclass]
  eclass.eStructuralFeatures.each do |f|
    w1 = FeatureWrapper.new(f, @classifiers) 
    w2 = FeatureWrapper.new(f.eOpposite, @classifiers) if f.is_a?(RGen::ECore::EReference) && f.eOpposite
    c.module_eval do
      if w1.many?
        _build_many_methods(w1, w2)
      else
        _build_one_methods(w1, w2)
      end
    end
  end
  @features_added[eclass] = true
  eclass.eSuperTypes.each do |t|
    add_features(t)
  end
  true
end

#create_module(epackage, under = Module.new) ⇒ Object

Create a Ruby module representing epackage. This includes all nested modules/packages, classes and enums.

If a parent module is provided with the “under” parameter, the new module will be nested under the parent module.

If the parent module has a non-temporary name, (more precisely: a non-temporary classpath) i.e. if it is reachable via a path of constant names from the root, then the nested modules and classes will also have non-temporary names. In particular, this means that they will keep their names even if they are assigned to new constants.

If no parent module is provided or the parent module has a temporary name by itself, then the nested modules and classes will also have temporary names. This means that their name will stay ‘volatile’ until they are assigned to constants reachable from the root and the Module#name method is called for the first time.

While the second approach is more flexible, it can come with a major performance impact. The reason is that Ruby searches the space of all known non-temporary classes/modules every time the name of a class/module with a temporary name is queried.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rgen/ecore/ecore_to_ruby.rb', line 40

def create_module(epackage, under=Module.new)
  temp = under.to_s.start_with?("#")
  mod = create_module_internal(epackage, under, temp)

  epackage.eAllClassifiers.each do |c| 
    if c.is_a?(RGen::ECore::EClass)
      create_class(c, temp)
    elsif c.is_a?(RGen::ECore::EEnum)
      create_enum(c)
    end
  end

  mod
end