Module: ActiveFacts::API::Vocabulary

Defined in:
lib/activefacts/api/vocabulary.rb,
lib/activefacts/api/object_type.rb

Overview

Vocabulary is a mixin that adds methods to any Module which has any ObjectType classes (ValueType or EntityType). A Vocabulary knows all the ObjectType classes including forward-referenced ones, and can resolve the forward references when the class is finally defined. Construction of a Constellation requires a Vocabulary as argument.

Instance Method Summary collapse

Instance Method Details

#__add_object_type(klass) ⇒ Object

:nodoc:



65
66
67
68
69
70
# File 'lib/activefacts/api/vocabulary.rb', line 65

def __add_object_type(klass)  #:nodoc:
  name = klass.basename
  __bind(name)
  @object_type ||= {}
  @object_type[klass.basename] = klass
end

#__bind(object_type_name) ⇒ Object

__bind raises an error if the named class doesn’t exist yet.



77
78
79
80
81
82
83
84
85
86
# File 'lib/activefacts/api/vocabulary.rb', line 77

def __bind(object_type_name)  #:nodoc:
  object_type = const_get(object_type_name)
  if (delayed.include?(object_type_name))
    d = delayed[object_type_name]
    d.each{|(a,b)|
        b.call(object_type, *a)
      }
    delayed.delete(object_type_name)
  end
end

#__delay(object_type_name, args, &block) ⇒ Object

:nodoc:



72
73
74
# File 'lib/activefacts/api/vocabulary.rb', line 72

def __delay(object_type_name, args, &block) #:nodoc:
  delayed[object_type_name] << [args, block]
end

#constellationObject

Create a new constellation over this vocabulary



47
48
49
# File 'lib/activefacts/api/vocabulary.rb', line 47

def constellation
  Constellation.new(self)
end

#delayedObject



88
89
90
91
# File 'lib/activefacts/api/vocabulary.rb', line 88

def delayed
  @delayed ||= Hash.new { |h,k| h[k] = [] }
  @delayed
end

#object_type(name = nil) ⇒ Object

With a parameter, look up an object type by name. Without, return the hash (keyed by the class’ basename) of all object_types in this vocabulary



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
# File 'lib/activefacts/api/vocabulary.rb', line 19

def object_type(name = nil)
  @object_type ||= {}
  return @object_type unless name

  if name.is_a? Class
    unless name.respond_to?(:vocabulary) and name.vocabulary == self
      raise CrossVocabularyRoleException.new(name, self)
    end
    return name
  end

  camel = name.to_s.camelcase
  if (c = @object_type[camel])
    __bind(camel)
    c
  else
    if constants.include?(camel.to_sym)
      if klass = const_get(camel) and !klass.respond_to?(:vocabulary)
        raise CrossVocabularyRoleException.new(name, self)
      end
      klass
    else
      nil
    end
  end
end

#populate(&b) ⇒ Object



51
52
53
# File 'lib/activefacts/api/vocabulary.rb', line 51

def populate &b
  constellation.populate &b
end

#verbaliseObject



55
56
57
58
59
60
61
62
63
# File 'lib/activefacts/api/vocabulary.rb', line 55

def verbalise
  "Vocabulary #{name}:\n\t" +
    @object_type.keys.sort.map do |object_type|
      c = @object_type[object_type]
      __bind(c.basename)
      c.verbalise + "\n\t\t// Roles played: " + c.all_role.verbalise
    end.
    join("\n\t")
end