module ActiveFacts::Generators::ScalaTraits::EntityType

Public Instance Methods

id_role_names(id_roles) click to toggle source
# File lib/activefacts/generators/traits/scala.rb, line 335
def id_role_names id_roles
  id_roles.map do |role|
    # Ignore identification through a supertype
    next if role.fact_type.kind_of?(ActiveFacts::Metamodel::TypeInheritance)
    role.scala_preferred_role_name(self).words.camelcase
  end.compact
end
id_role_types(id_roles) click to toggle source
# File lib/activefacts/generators/traits/scala.rb, line 343
def id_role_types id_roles
  id_roles.map do |role|
    next if role.fact_type.kind_of?(ActiveFacts::Metamodel::TypeInheritance)
    if !role.fact_type.entity_type && role.fact_type.all_role.size == 1
      "Boolean"
    else
      role.object_type.name.words.titlecase
    end
  end.compact
end
scala_metamodel(title_name) click to toggle source
# File lib/activefacts/generators/traits/scala.rb, line 296
def scala_metamodel(title_name)
  pi = preferred_identifier
  # The following finds the closest non-inheritance identifier
  #while pi.role_sequence.all_role_ref.size == 1 and
  #    (role = pi.role_sequence.all_role_ref.single.role).fact_type.is_a?(ActiveFacts::Metamodel::TypeInheritance)
  #  pi = role.fact_type.supertype_role.object_type.preferred_identifier
  #end
  identifying_parameters =
    pi.role_sequence.all_role_ref_in_order.map{|rr| rr.role.object_type.name.words.camelcase }*', '

  supertypes_list =
    if supertypes.empty?
      'Nil'
    else
      "List(#{supertypes.map{|s| s.name.words.camelcase}*', '})"
    end
  "  val #{name.words.camelcase} = assertEntity(FBMModel.EntityType(FBMModel.DomainObjectTypeName(\"#{title_name}\"), #{supertypes_list}, Seq(#{identifying_parameters})))\n"
end
scala_object(title_name, id_names, id_types) click to toggle source
# File lib/activefacts/generators/traits/scala.rb, line 248
def scala_object(title_name, id_names, id_types)
  "  object #{title_name} {\n" +
  "    def apply(" +
    (id_names.zip(id_types).map do |(name, type_name)|
      "#{name}: #{type_name}"
    end * ', '
    ) +
  ")(implicit constellation: Constellation) = {\n" +

  # Define the constant storage for the identifying role values:
  id_names.map do |name|
    "      val _#{name} = #{name}"
  end*"\n" +
  "      val _constellation = constellation\n" +
  "      assertEntity(new #{title_name} {\n" +
    id_names.map do |name|
      "        val #{name} = _#{name}"
    end*"\n" +
  "        val constellation = _constellation\n" +
  "      })\n" +        # Ends new block and assertEntity
  "    }\n" +           # Ends apply()
  "  }\n" +             # Ends object{}
  "\n"
end
scala_objectification() click to toggle source
# File lib/activefacts/generators/traits/scala.rb, line 354
      def scala_objectification
        # REVISIT: This disregards any supertypes and their identifiers
#       primary_supertype = o && (o.identifying_supertype || o.supertypes[0])
#       secondary_supertypes = o.supertypes-[primary_supertype]

        pi = preferred_identifier
        id_roles = []
        identifying_role_refs = pi.role_sequence.all_role_ref_in_order
        identifying_role_refs.each do |id_role_ref|
          id_roles << id_role_ref.role
        end
        id_names = id_role_names id_roles
        id_types = id_role_types id_roles

        "  case class #{name.words.titlecase}(#{
          id_names.zip(id_types).map {|(n, t)|
            "#{n}: #{t}"
          }*', '
        }) extends FBMModel.ObjectifiedFact {\n" +
        "    // REVISIT: Here, we should use fact_roles_dump(fact_type)\n" +
        absorbed_roles.map do |role|
          role.scala_role_definition
        end.
        compact*"\n" +
        "  }"
      end
scala_objectification_metamodel() click to toggle source
# File lib/activefacts/generators/traits/scala.rb, line 381
def scala_objectification_metamodel
  identifying_parameters = preferred_identifier.role_sequence.all_role_ref_in_order.map{|rr| rr.role.object_type.name.words.camelcase }*', '
  "  val #{name.words.camelcase} = assertEntity(FBMModel.ObjectifiedType(FBMModel.DomainObjectTypeName(\"#{name.words.titlecase}\"), Nil, Seq(#{identifying_parameters})))\n"
end
scala_shared(o, supertypes, pi = nil) click to toggle source
# File lib/activefacts/generators/traits/scala.rb, line 315
def scala_shared(o, supertypes, pi = nil)
  if supertypes
    primary_supertype = o && (o.identifying_supertype || o.supertypes[0])
  end
  title_name = o.name.words.titlecase

  id_roles, pis = *all_identifying_roles(o)
  id_names = id_role_names(o, id_roles)
  id_types = id_role_types(id_roles)
  identification = pi ? identified_by(o, pi) : nil

  # REVISIT: We don't want an object for abstract classes,
  # i.e. where subtypes have a disjoint mandatory constraint
  entity_object(title_name, id_names, id_types)

  entity_trait(o, title_name, primary_supertype, pis)

  entity_model(o, title_name)
end
scala_trait(title_name, primary_supertype, pis) click to toggle source
# File lib/activefacts/generators/traits/scala.rb, line 273
def scala_trait(title_name, primary_supertype, pis)
  s = 'override ' unless supertypes.empty?

  "  trait #{title_name} extends #{primary_supertype ? primary_supertype.name.words.titlecase : 'FBMModel.Entity'} {\n" +
  "    #{s}val objectType = metaModel.#{name.words.camelcase}\n" +
  (fact_type ? "  // REVISIT: Here, we should use fact_roles_dump(fact_type)\n\n" : '') +
  absorbed_roles.map do |role|
    role.scala_role_definition
  end.
  compact*"\n" +
  "    #{s}val identifier: Seq[Seq[FBMModel.Identifier[_]]] = Seq(#{
    pis.map do |pi|
      'Seq(' +
        pi.role_sequence.all_role_ref_in_order.map do |id_role_ref|
          id_role_ref.role.object_type.name.words.camelcase
        end*', ' +
      ')'
    end*', '
  })\n" +
  "  }\n" +             # Ends trait{}
  "\n"
end