class PackerFiles::Utils::HashSerializer

The Hash serializer is a meta-class that allows tracking of objects that needs to be converted into Hash objects, w/o having to hand-write lot of code.

Public Class Methods

hash_attributes() click to toggle source

Return the attributes that are tracked so that it can be used for serialization.

# File lib/PackerFiles/Utils/HashSerializer.rb, line 59
def hash_attributes
  @attrs
end
hash_variable(name, type = String, method = nil, nil_emit=false) click to toggle source

Specify that a list of methods are tracked for serialization. Name is the attribute name, Type is the object type that it belongs to. Method is the name of the conversion function that returns the value of the object serialized for JSON.

# File lib/PackerFiles/Utils/HashSerializer.rb, line 21
def hash_variable(name, type = String, method = nil, nil_emit=false)
   @attrs ||= []
   @attrs +=  [Hash["name",     name,   "type",     type, 
                    "method",   method, "nil_emit", nil_emit]
              ]
   self.class_eval do
      
     # Read accessor
     define_method(name) do
       instance_variable_get("@#{name}")
     end

     # Write accessor
     define_method("#{name}=") do |value|
        if value.is_a? type
          instance_variable_set("@#{name}", value)
        else
          raise ArgumentError.new("Invalid Type")
        end
     end

     # Converter function. We need to first get the
     # variable and then call a function on it by binding
     # first. (See Module::instance_method example in Ruby man page)
     define_method("to_hash_value_#{name}") do
       var = instance_variable_get("@#{name}")
       if (method.nil? || var.nil?)
         var
       else
         self.method(method).call
       end
     end

   end
end

Public Instance Methods

merge_hs(other) click to toggle source

Merge a hash serializer object with the current object.

# File lib/PackerFiles/Utils/HashSerializer.rb, line 66
def merge_hs(other)

   # Do nothing if the other is not a Hash Serializer
   return unless other.kind_of?(HashSerializer)

   # Get the attributes set in other and if the same attribute
   # is not set in self, then replace it. Otherwise ignore
   other.class.hash_attributes.each do |attr|
      name         = attr['name']
      other_value  = other.method(name).call
      self_value   = self.method(name).call
      if !other_value.nil? && self_value.nil?
         self.method("#{name}=").call(other_value)
      end
   end
end
to_hash(*args) click to toggle source

Hash Converter which iterates through all the tracked objects and outputs the hash. This function is NOT a meta-class function but instead operates on the object level.

# File lib/PackerFiles/Utils/HashSerializer.rb, line 86
def to_hash(*args)
 
   # Return value
   hash = Hash.new

   # Get all classes in the class hierarchy until HashSerializer
   index = self.class.ancestors.index(HashSerializer)
   cl_list  = self.class.ancestors.slice(0, index)
   cl_list.each do |klass|
      next if !klass.respond_to?(:hash_attributes)
      klass.hash_attributes.each do |attr|
        name       = attr['name']
        nil_emit   = attr['nil_emit']
        value      = method("to_hash_value_#{name}").call
        hash[name] = value if (!value.nil? || (value.nil? && nil_emit))
      end
   end
   return hash
end