From: Robert Klemme Date: 2007-01-10T17:00:11+09:00 Subject: Re: code organisation help request --------------000003000209050704030607 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit On 09.01.2007 22:19, Robert Klemme wrote: > I've attached a file to describe what I mean. You can distribute that > code to multiple files (you'll have to open and close the module once > per file). > > If you need to do some preprocessing common to all outputs, you might > want to add a PreProcessor class that converts a Config into something > else that stores the preprocessed state (and maybe also references the > original config). You can then pass that on to individual generators. Sorry, I made things more complex than necessary. The driver class in the first example was pretty useless. I've changed that to also be more consistent with the command pattern. I've also played a bit further to give you a better idea what I was thinking. Hope it helps. Kind regards robert --------------000003000209050704030607 Content-Type: text/plain; name="sample.rb" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="sample.rb" # module for namespace purposes only module FortranOutput class Config attr_accessor :name, :foo def self.load(file_name) config = new File.foreach(file_name) do |line| # parse content line by line end config end end # base class with all shared bits of code class OutputBase attr_accessor :context # alternatice approach: let the base class open # the file and invoke methods that must be # implemented in each subclass def generate File.open(file_name, "w") do |out| write_header out write_body out write_footer out end end # helper methods needed everywhere def write_comment(out, text) # do I remember Fortran formatting correct? # we could do other fancy things like line # wrapping, checking for legal characters etc. out.print "C ", text, "\n" end end # output for Struct (whatever that means) class StructOutput < OutputBase # sample, needed for generate in super class def file_name context.config.name + ".f" end def generate # just to do anything puts "generating: #{inspect}" end end # output for algorithms (whatever that means) class AlgorithmOutput < OutputBase def generate # just to do anything puts "generating: #{inspect}" end end # main driver of the generation class Generator # generators for individual parts # just add another class to generate # another part GENERATORS = [ StructOutput, AlgorithmOutput, ] attr_accessor :config def generate(config_file_name) self.config = Config.load config_file_name pre_process GENERATORS.each do |gen_class| gen = gen_class.new gen.context = self gen.generate end post_process end private def pre_process puts "global preprocessing" end def post_process puts "global postprocessing" end end end # silly main FortranOutput::Generator.new.generate __FILE__ --------------000003000209050704030607--