Class: PackerFiles::Utils::HashSerializer
- Inherits:
-
Object
- Object
- PackerFiles::Utils::HashSerializer
- Defined in:
- lib/PackerFiles/Utils/HashSerializer.rb
Overview
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.
Direct Known Subclasses
Provision::Base, Generator, Virtual::KVM, Virtual::VMWare, Virtual::VirtualBox
Class Method Summary collapse
-
.hash_attributes ⇒ Object
Return the attributes that are tracked so that it can be used for serialization.
-
.hash_variable(name, type = String, method = nil, nil_emit = false) ⇒ Object
Specify that a list of methods are tracked for serialization.
Instance Method Summary collapse
-
#merge_hs(other) ⇒ Object
Merge a hash serializer object with the current object.
-
#to_hash(*args) ⇒ Object
Hash Converter which iterates through all the tracked objects and outputs the hash.
Class Method Details
.hash_attributes ⇒ Object
Return the attributes that are tracked so that it can be used for serialization.
59 60 61 |
# File 'lib/PackerFiles/Utils/HashSerializer.rb', line 59 def hash_attributes @attrs end |
.hash_variable(name, type = String, method = nil, nil_emit = false) ⇒ Object
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.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# 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 |
Instance Method Details
#merge_hs(other) ⇒ Object
Merge a hash serializer object with the current object.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# 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) ⇒ Object
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.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# 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 |