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
-
#__add_concept(klass) ⇒ Object
:nodoc:.
-
#__bind(concept_name) ⇒ Object
__bind raises an error if the named class doesn’t exist yet.
-
#__delay(concept_name, args, &block) ⇒ Object
:nodoc:.
-
#concept(name = nil) ⇒ Object
With a parameter, look up a concept class by name.
-
#constellation ⇒ Object
Create a new constellation over this vocabulary.
- #populate(&b) ⇒ Object
- #verbalise ⇒ Object
Instance Method Details
#__add_concept(klass) ⇒ Object
:nodoc:
52 53 54 55 56 57 58 |
# File 'lib/activefacts/api/vocabulary.rb', line 52 def __add_concept(klass) #:nodoc: name = klass.basename __bind(name) # puts "Adding concept #{name} to #{self.name}" @concept ||= {} @concept[klass.basename] = klass end |
#__bind(concept_name) ⇒ Object
__bind raises an error if the named class doesn’t exist yet.
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/activefacts/api/vocabulary.rb', line 67 def __bind(concept_name) #:nodoc: concept = const_get(concept_name) # puts "#{name}.__bind #{concept_name} -> #{concept.name}" if concept 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:
60 61 62 63 64 |
# File 'lib/activefacts/api/vocabulary.rb', line 60 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 |
#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("#{name}::#{camel}") rescue nil) end |
#constellation ⇒ Object
Create a new constellation over this vocabulary
35 36 37 |
# File 'lib/activefacts/api/vocabulary.rb', line 35 def constellation Constellation.new(self) end |
#populate(&b) ⇒ Object
39 40 41 |
# File 'lib/activefacts/api/vocabulary.rb', line 39 def populate &b constellation.populate &b end |
#verbalise ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/activefacts/api/vocabulary.rb', line 43 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 |