Module: ActiveFacts::Generators::DataVaultTraits::ObjectType

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

Instance Method Summary collapse

Instance Method Details

#dv_add_surrogate(type_name = 'Auto Counter', suffix = 'ID') ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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