Class: ConfigToolkit::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/configtoolkit/writer.rb

Overview

This is an “abstract class” that documents the Writer interface. I put “abstract class” in quotes because Ruby of course neither supports nor requires abstract classes (that is, one can create a new writer class without extending Writer). It solely serves to document the interface.

Writers write configuration (represented by Hashes of parameter names to parameter values with the parameter names being Strings or Symbols and the parameter values being scalars, Hashes, or Arrays) to some underlying data sink (the write method).

Writers must implement require_symbol_parameter_names? in order to advertise whether they want parameter names as Strings or Symbols in the Hash passed to the write method.

Instance Method Summary collapse

Instance Method Details

#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

#require_symbol_parameter_names?Boolean

This must be implemented by Writers.

Returns:

Returns true if and only if the Writer requires Symbol parameter names (rather than String parameter names) in the Hash passed to write.

Returns:

Raises:

  • (NotImplementedError)


93
94
95
# File 'lib/configtoolkit/writer.rb', line 93

def require_symbol_parameter_names?
  raise NotImplementedError, "abstract method called"
end

#write(config_hash, containing_object_name) ⇒ Object

Description:

This method writes the configuration in config_hash, with containing object name containing_object_name, to the underlying data sink.

This must be implemented by Writers.

Parameters:

config_hash

A configuration represented as a Hash

containing_object_name

The configuration’s containing object name

Raises:

  • (NotImplementedError)


111
112
113
# File 'lib/configtoolkit/writer.rb', line 111

def write(config_hash, containing_object_name)
  raise NotImplementedError, "abstract method called"
end