Method: ConfigToolkit::Writer#create_containing_object_hash

Defined in:
lib/configtoolkit/writer.rb

#create_containing_object_hash(config_hash, containing_object_name) ⇒ Object

Description:

This is a concrete method that may be useful to Writers. It returns a Hash containing (possibly nested) entries for containing_object_name and, in the last object name’s Hash, config_hash. Basically, it returns a Hash that reflects config_hash and containing_object_name.

Some Writers may want to display containing objects in a special way, in which case the arguments to the write method (the configuration Hash and the containing object name) allow them to do so. For Writers that do not want any special handling for containing objects, however, this method can convert that information into a single Hash representation in which the configuration parameters are nested within Hashes for the containing objects (this matches the format returned by readers, as they do not know about containing objects).

This should not be re-implemented by writers.

Parameters:

config_hash

A configuration represented as a Hash

containing_object_name

The configuration’s containing object name

Returns:

A Hash in which config_hash is nested within Hashes for the containing objects in containing_object_name



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/configtoolkit/writer.rb', line 51

def create_containing_object_hash(config_hash, containing_object_name)
  if(containing_object_name.empty?())
    return config_hash
  else
    use_symbol_parameter_names = require_symbol_parameter_names?()

    containing_object_hash = {}
    object_hash = containing_object_hash
    
    object_names = containing_object_name.split(".")
    object_names.each_with_index do |object_name, index|
      #
      # The hash for the last object in containing_object_name must
      # be config_hash, so it gets treated specially.
      #
      if(index < (object_names.size() - 1))
        contained_hash = {}
      else
        contained_hash = config_hash
      end

      if(use_symbol_parameter_names)
        object_hash[object_names[index].to_sym()] = contained_hash
      else
        object_hash[object_names[index]] = contained_hash
      end
      
      object_hash = contained_hash
    end

    return containing_object_hash
  end
end