Class: Connect::ObjectsTable
- Inherits:
-
Object
- Object
- Connect::ObjectsTable
- Defined in:
- lib/connect/objects_table.rb
Overview
Implements a table to keep all object definitions is. The table is index by name
Class Method Summary collapse
-
.entry(type, name, values) ⇒ Object
Autoload a ruby class based on the type of the object.
Instance Method Summary collapse
-
#add(type, name, values, xref = nil) ⇒ Object
Add an object to the objects table.
-
#definitions(type, name) ⇒ Object
return the array of defintions for the specfied object.
-
#dump ⇒ Object
return the content of the objects table in human readable format.
-
#entries(type, name = /.*/) ⇒ Object
Give all entries in the objects table based on the specfied type and name.
-
#initialize ⇒ ObjectsTable
constructor
A new instance of ObjectsTable.
-
#lookup(type, name) ⇒ ObjectDefinition
Lookup an object with a specfied type and name in the object table.
-
#references(type, name) ⇒ Object
return the array of references for the specfied parameter.
- #register_reference(type, name, xref) ⇒ Object
Constructor Details
#initialize ⇒ ObjectsTable
Returns a new instance of ObjectsTable.
12 13 14 |
# File 'lib/connect/objects_table.rb', line 12 def initialize @objects_table = {} end |
Class Method Details
.entry(type, name, values) ⇒ Object
Autoload a ruby class based on the type of the object. If it doesn’t exists, just instantiate a ObjectDefinition. This mechanism allows extension of the connect classes with user written classes.
#param values [Hash] a [Hash] containing all values
110 111 112 113 114 115 |
# File 'lib/connect/objects_table.rb', line 110 def self.entry(type, name, values) type_name = type.to_s.capitalize klass_name = "Connect::Objects::#{type_name}" klass = Puppet::Pops::Types::ClassLoader.provide_from_string(klass_name) klass ? klass.new(type, name, values) : ObjectDefinition.new(type, name, values) end |
Instance Method Details
#add(type, name, values, xref = nil) ⇒ Object
Add an object to the objects table.
58 59 60 61 |
# File 'lib/connect/objects_table.rb', line 58 def add(type, name, values, xref = nil) object = register_reference(type, name, xref) add_values_to_existing!(object, values) end |
#definitions(type, name) ⇒ Object
return the array of defintions for the specfied object
33 34 35 36 37 |
# File 'lib/connect/objects_table.rb', line 33 def definitions(type, name) key = key(type,name) entry = @objects_table.fetch(key) { fail "internal error. Object #{type} #{name} not found" } entry.xref.collect {|e| e.class == Connect::Xdef ? [e.file_name, e.lineno] : nil}.compact end |
#dump ⇒ Object
return the content of the objects table in human readable format
86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/connect/objects_table.rb', line 86 def dump output = '' @objects_table.keys.sort.each do |key| object_entry = @objects_table[key] name = object_entry.__name__ type = object_entry.__type__ # # use the inspect to make ruby 1.8.7 and 1.9.3 compatible # object = lookup(type, name).to_hash.inspect output << "#{type}(#{name}) = #{object}\n" end output end |
#entries(type, name = /.*/) ⇒ Object
Give all entries in the objects table based on the specfied type and name. As name, you can specify a regular expression.
22 23 24 25 |
# File 'lib/connect/objects_table.rb', line 22 def entries( type, name = /.*/) all_keys = @objects_table.keys.select{| k| Regexp.new(key(type,name)).match(k) }.sort all_keys.collect {|key| identity(key)} end |
#lookup(type, name) ⇒ ObjectDefinition
Lookup an object with a specfied type and name in the object table
76 77 78 |
# File 'lib/connect/objects_table.rb', line 76 def lookup(type, name) from_table(type, name) end |
#references(type, name) ⇒ Object
return the array of references for the specfied parameter
45 46 47 48 49 |
# File 'lib/connect/objects_table.rb', line 45 def references(type, name) key = key(type,name) entry = @objects_table.fetch(key) { fail "internal error. Object #{type} #{name} not found" } entry.xref.collect {|e| e.class == Connect::Xref ? [e.file_name, e.lineno] : nil}.compact end |
#register_reference(type, name, xref) ⇒ Object
63 64 65 66 67 |
# File 'lib/connect/objects_table.rb', line 63 def register_reference(type,name, xref) object = from_table(type, name) object.add_reference(xref) object end |