class ActiveFacts::Generators::Scala

Generate Scala module containing classes for an ActiveFacts vocabulary. Invoke as

afgen --scala[=options] <file>.cql

Options are comma or space separated:

Public Class Methods

new(vocabulary, *options) click to toggle source
Calls superclass method
# File lib/activefacts/generators/scala.rb, line 23
def initialize(vocabulary, *options)
  super
  @constraints_used = {}
  @fact_types_dumped = {}
end

Private Instance Methods

all_identifying_roles(o) click to toggle source
# File lib/activefacts/generators/scala.rb, line 87
def all_identifying_roles(o)
  pis = []
  # This places the subtype identifying roles before the supertype's. Reverse the list to change this.
  id_roles = []
  o.supertypes_transitive.each do |supertype|
    pi = supertype.preferred_identifier
    next if pis.include?(pi)   # Seen this identifier already?
    pis << pi
    identifying_role_refs = pi.role_sequence.all_role_ref_in_order
    identifying_role_refs.each do |id_role_ref|
      # Have we seen this role in another identifier?
      next if id_roles.detect{|idr| idr == id_role_ref.role }
      id_roles << id_role_ref.role
    end
  end
  [id_roles, pis]
end
data_type_dump(o) click to toggle source
# File lib/activefacts/generators/scala.rb, line 59
def data_type_dump(o)
end
entity_model(o, title_name) click to toggle source
# File lib/activefacts/generators/scala.rb, line 113
def entity_model(o, title_name)
  @metamodel << o.scala_metamodel(title_name)
end
entity_object(o, title_name, id_names, id_types) click to toggle source
# File lib/activefacts/generators/scala.rb, line 105
def entity_object(o, title_name, id_names, id_types)
  puts o.scala_object(title_name, id_names, id_types)
end
entity_trait(o, title_name, primary_supertype, pis) click to toggle source
# File lib/activefacts/generators/scala.rb, line 109
def entity_trait(o, title_name, primary_supertype, pis)
  puts o.scala_trait(title_name, primary_supertype, pis)
end
fact_type_dump(fact_type, name) click to toggle source

Dump one fact type.

# File lib/activefacts/generators/scala.rb, line 156
def fact_type_dump(fact_type, name)
  @fact_types_dumped[fact_type] = true
  return objectified_fact_type_dump(fact_type.entity_type) if fact_type.entity_type

  puts fact_type.scala_definition

  @metamodel << fact_type.scala_metamodel
end
fact_type_name(fact_type) click to toggle source
# File lib/activefacts/generators/scala.rb, line 44
def fact_type_name(fact_type)
  fact_type.default_reading.words
end
id_role_names(o, id_roles) click to toggle source
# File lib/activefacts/generators/scala.rb, line 68
def id_role_names o, id_roles
  id_roles.map do |role|
    # Ignore identification through a supertype
    next if role.fact_type.kind_of?(ActiveFacts::Metamodel::TypeInheritance)
    role.preferred_role_name(o).words.camelcase
  end.compact
end
id_role_types(id_roles) click to toggle source
# File lib/activefacts/generators/scala.rb, line 76
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
identified_by_roles_and_facts(entity_type, identifying_role_refs, identifying_facts) click to toggle source
# File lib/activefacts/generators/scala.rb, line 143
def identified_by_roles_and_facts(entity_type, identifying_role_refs, identifying_facts)
  identifying_role_refs.map do |role_ref|
      [ role_ref.role.scala_preferred_role_name(entity_type),
        entity_type.name.words.titlecase
      ]
    end
end
non_subtype_dump(o, pi) click to toggle source
# File lib/activefacts/generators/scala.rb, line 117
def non_subtype_dump(o, pi)
  subtype_dump(o, nil, pi)
end
objectified_fact_type_dump(o) click to toggle source
# File lib/activefacts/generators/scala.rb, line 165
def objectified_fact_type_dump o
  puts o.scala_objectification
  @metamodel << o.scala_objectification_metamodel
end
role_dump(role) click to toggle source
# File lib/activefacts/generators/scala.rb, line 175
def role_dump(role)
  puts role.scala_role_definition
end
set_option(option) click to toggle source
Calls superclass method
# File lib/activefacts/generators/scala.rb, line 31
def set_option(option)
  @mapping = false
  case option
  when 'help', '?'
    $stderr.puts "Usage:\t\tafgen --scala[=option,option] input_file.cql\n"+
        "\t\tmeta\t\tModify the mapping to suit a metamodel"
    exit 0
  when /^meta/
    @is_metamodel = true
  else super
  end
end
skip_fact_type(f) click to toggle source
# File lib/activefacts/generators/scala.rb, line 151
def skip_fact_type(f)
  f.is_a?(ActiveFacts::Metamodel::TypeInheritance)
end
subtype_dump(o, supertypes, pi = nil) click to toggle source
# File lib/activefacts/generators/scala.rb, line 121
def subtype_dump(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(o, title_name, id_names, id_types)

  entity_trait(o, title_name, primary_supertype, pis)

  entity_model(o, title_name)

  @constraints_used[pi] = true if pi
end
unary_dump(role, role_name) click to toggle source
# File lib/activefacts/generators/scala.rb, line 170
def unary_dump(role, role_name)
  scala_role_name = role_name.words.camelcase
  puts "    val #{scala_role_name}: Boolean"
end
value_type_dump(o, super_type_name, facets) click to toggle source
# File lib/activefacts/generators/scala.rb, line 62
def value_type_dump(o, super_type_name, facets)
  puts o.scala_definition(super_type_name, facets)

  @metamodel << o.scala_metamodel(super_type_name, facets)
end
vocabulary_end() click to toggle source
# File lib/activefacts/generators/scala.rb, line 54
def vocabulary_end
  puts @vocabulary.scala_finale
  puts "#{@metamodel}\n}\n"
end
vocabulary_start() click to toggle source
# File lib/activefacts/generators/scala.rb, line 48
def vocabulary_start
  puts @vocabulary.scala_prelude

  @metamodel = @vocabulary.scala_prelude_metamodel
end