Sha256: 4056743e6093f9943f6dbbd70fba6a0aa3f1073374f8522a5c1a1a2bf99d67dc

Contents?: true

Size: 1.93 KB

Versions: 2

Compression:

Stored size: 1.93 KB

Contents

require 'typecollection'
require 'builder'

class XMLGenerator
	include TypeCollection::Base

	class UnimplementedMethod < StandardError; end

	def self.process_elements(elements)
		raise XMLGenerator::UnimplementedMethod.new("#{self.name} doesn't implement process_elements!")
	end

	def self.get_writer_instance; FileWriter.get_associated_type(self).new(); end

	def self.generate_feed(elements)
		writer = self.get_writer_instance()
		writer.write_contents(self.process_elements(elements))
	end

	def self.generate_all_feeds(elements)
		XMLGenerator.all_types.each do |klass|
			klass.generate_feed(elements)
		end
		true
	end
end

class FileWriter
	include TypeCollection::Base

	class UnimplementedMethod < StandardError; end

	def self.file_name; "#{self.inferred_type}.xml"; end

	def write_contents(string_contents)
		raise FileWriter::UnimplementedMethod.new("#{self.name} doesn't implement write_contents!")
	end
end

class FirstXMLGenerator < XMLGenerator

	def self.process_elements(elements)
		xml = Builder::XmlMarkup.new()
		xml.body do
			xml.name "First" # => Alternatively, self.inferred_type
			xml.elements do
				elements.each do |element|
					xml.element "One way to handle #{element.to_s}"
			  end
			end
		end
		xml.target!
	end
end

class FirstFileWriter < FileWriter
	def write_contents(string_contents)
		File.open(self.class.file_name, 'w'){|f| f.write(string_contents) }
	end
end

class SecondXMLGenerator < XMLGenerator
	def self.process_elements(elements)
		xml = Builder::XmlMarkup.new()
		xml.body do
			xml.name self.inferred_type
			xml.elements do
				elements.each do |element|
				  xml.element "Another Way to Handle #{element.to_s}"
				end
			end
		end
		xml.target!
	end
end

class SecondFileWriter < FileWriter
	def write_contents(string_contents)
		File.open(self.class.file_name, 'a'){|f| f.write(string_contents) }
	end
end

# Example of how you might perform this example
#
# XMLGenerator.generate_all_feeds(["one", 2, "three"])

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
typecollection-0.0.11 examples/xml_generator.rb
typecollection-0.0.10 examples/xml_generator.rb