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:



59
60
61
62
63
64
# File 'lib/activefacts/api/vocabulary.rb', line 59

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.



71
72
73
74
75
76
77
78
79
80
# File 'lib/activefacts/api/vocabulary.rb', line 71

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:



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

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

#constellationObject

Create a new constellation over this vocabulary



41
42
43
# File 'lib/activefacts/api/vocabulary.rb', line 41

def constellation
  Constellation.new(self)
end

#delayedObject



82
83
84
85
# File 'lib/activefacts/api/vocabulary.rb', line 82

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
# 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
	  klass = const_get(camel) rescue nil	# NameError if undefined
	  klass && klass.modspace == self ? klass : nil
  end
end

#populate(&b) ⇒ Object



45
46
47
# File 'lib/activefacts/api/vocabulary.rb', line 45

def populate &b
  constellation.populate &b
end

#verbaliseObject



49
50
51
52
53
54
55
56
57
# File 'lib/activefacts/api/vocabulary.rb', line 49

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