Class: PackerFiles::Utils::TypeAccessor

Inherits:
Object
  • Object
show all
Defined in:
lib/PackerFiles/Utils/TypeAccessor.rb

Overview

The Type accessor class is used as a meta-class for any class.

Direct Known Subclasses

Builder, Virtual::Hypervisors

Class Method Summary collapse

Class Method Details

.proc_from(&block) ⇒ Object

Given a block, convert it into a proc. if no block is given then it is a nil object that is returned



57
58
59
60
61
62
63
# File 'lib/PackerFiles/Utils/TypeAccessor.rb', line 57

def proc_from(&block)
  if block_given?
     Proc.new
  else
     nil
  end
end

.type_accessor(type, name = type.name.gsub(/.*::/, ''), optional = false) ⇒ Object

Custom accessor to create accessors from type name. It is also Possible to set the name of the accessor. By default it is the name of the type.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/PackerFiles/Utils/TypeAccessor.rb', line 27

def type_accessor(type, name     = type.name.gsub(/.*::/, ''), 
		 optional = false)

   # Save the type and name in a array hash, which will be used later.
   @types ||= Hash.new
   @types[name] = [type, optional]

   # Doing a class_eval creates a new method of "name"
   class_eval do
   
     # Read/Write accessor which accepts blocks for initialization.
     define_method("#{name}") do |arg = nil, &block|
if (self.class.proc_from(&block).nil?)
   instance_variable_get("@#{name}")
else
   obj = type.new(&block)
   instance_variable_set("@#{name}", obj)
end
     end # define_method
   end # class_eval

end

.type_accessorsObject

Return the Hash that is used to track registered type accessors



51
52
53
# File 'lib/PackerFiles/Utils/TypeAccessor.rb', line 51

def type_accessors
  @types
end