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:



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

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.



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

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:



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

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

#constellationObject

Create a new constellation over this vocabulary



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

def constellation
  Constellation.new(self)
end

#delayedObject



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

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
45
# 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 const_defined?(camel)
	    begin
 const_get(camel)
	    rescue NameError
 nil
	    end
	  else
	    nil
	  end
  end
end

#populate(&b) ⇒ Object



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

def populate &b
  constellation.populate &b
end

#verbaliseObject



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

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.roles.verbalise
	  end.
	  join("\n\t")
end