class PackerFiles::Utils::TypeAccessor

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

Public Class Methods

proc_from(&block) click to toggle source

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

# 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) click to toggle source

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.

# 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_accessors() click to toggle source

Return the Hash that is used to track registered type accessors

# File lib/PackerFiles/Utils/TypeAccessor.rb, line 51
def type_accessors
  @types
end