Class: ConfigToolkit::YAMLWriter
- Defined in:
- lib/configtoolkit/yamlwriter.rb
Overview
This class implements the Writer interface for YAML configuration files. It basically is a wrapper over Ruby’s YAML library.
YAML natively supports Integers, Floats, Strings, and Booleans. Instances of other classes are written by Ruby to YAML configuration files by recursively writing each of the instance’s variables and noting the associated Ruby class in the YAML file. This may not be desired behavior for a configuration file, especially if the file is going to be shared with a non-Ruby program. On the other hand, if only Ruby is using the configuration the file, this behavior may well be very desirable (since arbitrarily complex objects can be supported directly by the YAML format). Thus, the YAMLWriter offers two options:
-
Only standard YAML classes will be written. Values of classes not supported natively by YAML will be converted to Strings (with
to_s
). -
Non-standard YAML classes will be written (Ruby object->YAML serialization).
See YAML.txt for more details.
Defined Under Namespace
Classes: Visitor
Instance Method Summary collapse
-
#initialize(stream, only_output_standard_yaml_classes) ⇒ YAMLWriter
constructor
Description: This constructs a YAMLWriter instance for
stream
, wherestream
either is a file name (a String) or an IO object. -
#require_symbol_parameter_names? ⇒ Boolean
Returns: Returns
false
, since the YAMLWriter requires String parameter names in the Hash passed to write. -
#write(config_hash, containing_object_name) ⇒ Object
Description: This method writes
config_hash
to the underlying stream.
Methods inherited from Writer
#create_containing_object_hash
Constructor Details
#initialize(stream, only_output_standard_yaml_classes) ⇒ YAMLWriter
Description:
This constructs a YAMLWriter instance for stream
, where stream
either is a file name (a String) or an IO object. If only_output_standard_yaml_classes
, then the writer only will output standard YAML classes; all classes not native to YAML will be converted into Strings with to_s
before being written. If not only_output_standard_yaml_classes
, however, then Ruby serialized classes will be written to the configuration file.
Parameters:
- stream
-
A file name (String) or an IO object. If
stream
is a file name, then the YAMLWriter will open the associated file. - only_output_standard_yaml_classes
-
Whether or not the writer should output only standard YAML classes.
52 53 54 55 56 57 58 59 60 |
# File 'lib/configtoolkit/yamlwriter.rb', line 52 def initialize(stream, only_output_standard_yaml_classes) if(stream.class == String) @stream = File.open(stream, "w") else @stream = stream end @only_output_standard_yaml_classes = only_output_standard_yaml_classes end |
Instance Method Details
#require_symbol_parameter_names? ⇒ Boolean
Returns:
Returns false
, since the YAMLWriter requires String parameter names in the Hash passed to write.
153 154 155 |
# File 'lib/configtoolkit/yamlwriter.rb', line 153 def require_symbol_parameter_names? return false end |
#write(config_hash, containing_object_name) ⇒ Object
Description:
This method writes config_hash
to the underlying stream.
Parameters:
- config_hash
-
The configuration hash to write
- containing_object_name
-
The configuration’s containing object name
167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/configtoolkit/yamlwriter.rb', line 167 def write(config_hash, containing_object_name) if(@only_output_standard_yaml_classes) visitor = Visitor.new() visitor.visit(config_hash) output_hash = visitor.config_hash else output_hash = config_hash end containing_object_hash = create_containing_object_hash(output_hash, containing_object_name) YAML::dump(containing_object_hash, @stream) @stream.flush() end |