Module: ActiveFacts::Generators::ScalaTraits::ValueType

Defined in:
lib/activefacts/generators/traits/scala.rb

Constant Summary collapse

DataTypeMap =
{
  "Signed Integer" => "Int",
  "Unsigned Integer" => "Int",
  "Real" => "Double",
  "Char" => "String",
  # REVISIT: More will be needed here.
}
LengthTypes =

REVISIT: More will be needed here.

[ "String", "Decimal" ]
ScaleTypes =
[ "Decimal" ]

Instance Method Summary collapse

Instance Method Details

#scala_definition(super_type_name, facets) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/activefacts/generators/traits/scala.rb', line 201

def scala_definition(super_type_name, facets)
  vt_name = name.words.titlecase
  if d = DataTypeMap[super_type_name]
    super_type_name = d
  end
  super_type_title = super_type_name.words.titlecase
  super_type_camel = super_type_name.words.camelcase

  sometimes_optional =  all_role.detect do |r|
    r.fact_type.all_role.size == 2 && (c = (r.fact_type.all_role.to_a-[r])[0]) and !c.is_mandatory
  end

  "  case class #{vt_name}(value: #{super_type_title})(implicit val constellation: Constellation) extends FBMModel.ValueTypeValue[#{super_type_title}] {\n" +
  "    val objectType = metaModel.#{vt_name.words.camelcase}\n" +
#	  REVISIT: scala_type_params +
#	  REVISIT: scala_value_restriction + # puts "    restrict #{value_constraint.all_allowed_range_sorted.map{|ar| ar.to_s}*", "}\n" if value_constraint
#	  REVISIT: scala_units +  # puts "    \# REVISIT: #{vt_name} is in units of #{unit.name}\n" if unit
  absorbed_roles.map do |role|
           role.scala_role_definition
         end.
         compact*"\n" +
  "  }\n" +

  # Add implicit casts for the underlying data type:
  "  implicit def #{super_type_camel}2#{vt_name}(value: #{super_type_title})(implicit constellation: Constellation): #{vt_name} = #{vt_name}(value)\n" +
  if sometimes_optional
    "  implicit def #{super_type_camel}2#{vt_name}Option(value: #{super_type_title})(implicit constellation: Constellation): Option[#{vt_name}] = Some(#{vt_name}(value))\n"
  else
    ""
  end +
  "\n"
end

#scala_metamodel(super_type_name, facets) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
# File 'lib/activefacts/generators/traits/scala.rb', line 234

def scala_metamodel(super_type_name, facets)
  vt_name = name.words.titlecase
  super_type_title = super_type_name.words.titlecase
  # REVISIT: Remove facets that do not apply to the Scala data types
  params = [
    LengthTypes.include?(super_type_name) ? facets[:length] : nil,
    ScaleTypes.include?(super_type_name) ? facets[:scale] : nil
  ].compact * ", "

  "  val #{name.words.camelcase} = assertEntity(FBMModel.ValueType(FBMModel.DomainObjectTypeName(\"#{vt_name}\"), FBMModel.#{super_type_title}Type(#{params}), Nil))\n"
end