module ActiveFacts::Generators::DataVaultTraits::ObjectType

Public Instance Methods

dv_add_surrogate(type_name = 'Auto Counter', suffix = 'ID') click to toggle source
# File lib/activefacts/generators/traits/datavault.rb, line 15
def dv_add_surrogate type_name = 'Auto Counter', suffix = 'ID'
  # Find or assert the surrogate value type
  auto_counter = vocabulary.valid_value_type_name(type_name) ||
    constellation.ValueType(:vocabulary => vocabulary, :name => type_name, :concept => :new)

  # Create a subtype to identify this entity type:
  vt_name = self.name + ' '+suffix
  my_id = @vocabulary.valid_value_type_name(vt_name) ||
    constellation.ValueType(:vocabulary => vocabulary, :name => vt_name, :concept => :new, :supertype => auto_counter)

  # Create a fact type
  identifying_fact_type = constellation.FactType(:concept => :new)
  my_role = constellation.Role(:concept => :new, :fact_type => identifying_fact_type, :ordinal => 0, :object_type => self)
  self.injected_surrogate_role = my_role
  id_role = constellation.Role(:concept => :new, :fact_type => identifying_fact_type, :ordinal => 1, :object_type => my_id)

  # Create a reading (which needs a RoleSequence)
  reading = constellation.Reading(
    :fact_type => identifying_fact_type,
    :ordinal => 0,
    :role_sequence => [:new],
    :text => "{0} has {1}"
  )
  constellation.RoleRef(:role_sequence => reading.role_sequence, :ordinal => 0, :role => my_role)
  constellation.RoleRef(:role_sequence => reading.role_sequence, :ordinal => 1, :role => id_role)

  # Create two uniqueness constraints for the one-to-one. Each needs a RoleSequence (two RoleRefs)
  one_id = constellation.PresenceConstraint(
      :concept => :new,
      :vocabulary => vocabulary,
      :name => self.name+'HasOne'+suffix,
      :role_sequence => [:new],
      :is_mandatory => true,
      :min_frequency => 1,
      :max_frequency => 1,
      :is_preferred_identifier => false
    )
  @constellation.RoleRef(:role_sequence => one_id.role_sequence, :ordinal => 0, :role => my_role)

  one_me = constellation.PresenceConstraint(
      :concept => :new,
      :vocabulary => vocabulary,
      :name => self.name+suffix+'IsOfOne'+self.name,
      :role_sequence => [:new],
      :is_mandatory => false,
      :min_frequency => 0,
      :max_frequency => 1,
      :is_preferred_identifier => true
    )
  @constellation.RoleRef(:role_sequence => one_me.role_sequence, :ordinal => 0, :role => id_role)
end