Module: ActiveFacts::API::Vocabulary

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

Overview

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

Instance Method Summary collapse

Instance Method Details

#__bind(concept_name) ⇒ Object

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



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

def __bind(concept_name)  #:nodoc:
  concept = const_get(concept_name)
  if (@delayed && @delayed.include?(concept_name))
    # $stderr.puts "#{concept_name} was delayed, binding now"
    d = @delayed[concept_name]
    d.each{|(a,b)|
        b.call(concept, *a)
      }
    @delayed.delete(concept_name)
  end
end

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

:nodoc:



42
43
44
45
46
# File 'lib/activefacts/api/vocabulary.rb', line 42

def __delay(concept_name, args, &block) #:nodoc:
  # puts "Arranging for delayed binding on #{concept_name.inspect}"
  @delayed ||= Hash.new { |h,k| h[k] = [] }
  @delayed[concept_name] << [args, block]
end

#add_concept(klass) ⇒ Object

:nodoc:



34
35
36
37
38
39
40
# File 'lib/activefacts/api/vocabulary.rb', line 34

def add_concept(klass)  #:nodoc:
  name = klass.basename
  __bind(name)
  # puts "Adding concept #{name} to #{self.name}"
  @concept ||= {}
  @concept[klass.basename] = klass
end

#concept(name = nil) ⇒ Object

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



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/activefacts/api/vocabulary.rb', line 19

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

  return name if name.is_a? Class

  # puts "Looking up concept #{name} in #{self.name}"
  camel = name.to_s.camelcase(true)
  if (c = @concept[camel])
    __bind(camel)
    return c
  end
  return (const_get(camel) rescue nil)
end

#verbaliseObject



61
62
63
64
65
66
67
68
# File 'lib/activefacts/api/vocabulary.rb', line 61

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