Class: Serializer::Manifest

Inherits:
Serializable show all
Includes:
HappyMapper
Defined in:
lib/serializer/manifest.rb

Overview

Note:

Copyright © 2012 by The Board of Trustees of the Leland Stanford Junior University. All rights reserved. See LICENSE for details.

Subclass of Serializable that adds methods for marshalling/unmarshalling data to a persistent XML file format.

Data Model

  • Serializable = utility methods to faciliate serialization to Hash, JSON, or YAML

    • Manifest = subclass adds methods for marshalling/unmarshalling data to XML file format

See Also:

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Serializable

#array_to_hash, deep_diff, #diff, #initialize, #key, #key_name, #summary, #to_hash, #to_json, #to_yaml, #variable_names, #variables

Constructor Details

This class inherits a constructor from Serializer::Serializable

Class Method Details

.read_xml_file(parent_dir, filename = nil) ⇒ Serializable

Returns Read the xml file and return the parsed XML.

Examples:

Parameters:

  • parent_dir (Pathname, String)

    The location of the directory in which the xml file is located

  • filename (String) (defaults to: nil)

    Optional filename if one wishes to override the default filename

Returns:

  • (Serializable)

    Read the xml file and return the parsed XML



50
51
52
# File 'lib/serializer/manifest.rb', line 50

def self.read_xml_file(parent_dir, filename=nil)
  self.parse(self.xml_pathname(parent_dir, filename).read)
end

.write_xml_file(xml_object, parent_dir, filename = nil) ⇒ void

This method returns an undefined value.

Returns Serializize the in-memory object to a xml file instance.

Parameters:

  • xml_object (Serializable)
  • parent_dir (Pathname, String)

    The location of the directory in which the xml file is located

  • filename (String) (defaults to: nil)

    Optional filename if one wishes to override the default filename



59
60
61
62
63
# File 'lib/serializer/manifest.rb', line 59

def self.write_xml_file(xml_object, parent_dir, filename=nil)
  parent_dir.mkpath
  self.xml_pathname(parent_dir, filename).open('w') { |f| f << xml_object.to_xml }
  nil
end

.xml_filename(filename = nil) ⇒ String

Returns the standard filename (derived from the class name) to be used for serializing an object

Parameters:

  • filename (String) (defaults to: nil)

    Optional filename if one wishes to override the default filename

Returns:

  • (String)

    Returns the standard filename (derived from the class name) to be used for serializing an object



20
21
22
23
24
25
26
27
# File 'lib/serializer/manifest.rb', line 20

def self.xml_filename(filename=nil)
  if filename
    filename
  else
    cname = self.name.split(/::/).last
    cname[0, 1].downcase + cname[1..-1] + '.xml'
  end
end

.xml_pathname(parent_dir, filename = nil) ⇒ Pathname

Returns The location of the xml file.

Parameters:

  • parent_dir (Pathname, String)

    The location of the directory in which the xml file is located

  • filename (String) (defaults to: nil)

    Optional filename if one wishes to override the default filename

Returns:

  • (Pathname)

    The location of the xml file



33
34
35
# File 'lib/serializer/manifest.rb', line 33

def self.xml_pathname(parent_dir, filename=nil)
  Pathname.new(parent_dir).join(self.xml_filename(filename))
end

.xml_pathname_exist?(parent_dir, filename = nil) ⇒ Boolean

Returns true if the xml file exists

Parameters:

  • parent_dir (Pathname, String)

    The location of the directory in which the xml file is located

  • filename (String) (defaults to: nil)

    Optional filename if one wishes to override the default filename

Returns:

  • (Boolean)

    Returns true if the xml file exists



41
42
43
# File 'lib/serializer/manifest.rb', line 41

def self.xml_pathname_exist?(parent_dir, filename=nil)
  self.xml_pathname(parent_dir, filename).exist?
end

Instance Method Details

#write_xml_file(parent_dir, filename = nil) ⇒ void

This method returns an undefined value.

Returns Serializize the in-memory object to a xml file instance.

Examples:

Parameters:

  • parent_dir (Pathname, String)

    The location of the directory in which the xml file is located

  • filename (String) (defaults to: nil)

    Optional filename if one wishes to override the default filename



70
71
72
# File 'lib/serializer/manifest.rb', line 70

def write_xml_file(parent_dir, filename=nil)
  self.class.write_xml_file(self, parent_dir, filename)
end