Sha256: 2147d440b3d6a306e5a41c94b025a8db6fc3fcbac99793e8eacbe0473f1d1c24

Contents?: true

Size: 1.42 KB

Versions: 2

Compression:

Stored size: 1.42 KB

Contents

module RGen

module Serializer

# simple identifier calculation based on qualified names
# prerequisits:
# * containment relations must be bidirectionsl
# * local name stored in single attribute +@attribute_name+ for all classes
#
class QualifiedNameProvider

  def initialize(options={})
    @qualified_name_cache = {}
    @attribute_name = options[:attribute_name] || "name"
    @separator = options[:separator] || "/"
    @leading_separator = options.has_key?(:leading_separator) ? options[:leading_separator] : true 
  end

  def identifier(element)
    (element.is_a?(RGen::MetamodelBuilder::MMProxy) && element.targetIdentifier) || qualified_name(element)
  end

  def qualified_name(element)
    return @qualified_name_cache[element] if @qualified_name_cache[element]
    localIdent = ((element.respond_to?(@attribute_name) && element.getGeneric(@attribute_name)) || "").strip
    parentRef = element.class.ecore.eAllReferences.select{|r| r.eOpposite && r.eOpposite.containment}.first
    parent = parentRef && element.getGeneric(parentRef.name)
    if parent
      if localIdent.size > 0
        parentIdent = qualified_name(parent)
        result = parentIdent + @separator + localIdent
      else
        result = qualified_name(parent)
      end
    else
      result = (@leading_separator ? @separator : "") + localIdent
    end
    @qualified_name_cache[element] = result
  end
end

end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rgen-0.6.1 lib/rgen/serializer/qualified_name_provider.rb
rgen-0.6.0 lib/rgen/serializer/qualified_name_provider.rb