Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > My First Almost Serious Ruby Program - Critique it

Reply
Thread Tools

My First Almost Serious Ruby Program - Critique it

 
 
voipfc
Guest
Posts: n/a
 
      10-11-2006

This is a follow up on what I this posting

http://groups.google.co.uk/group/rub...0309f2a864ed85

It is based on info from here http://freshmeat.net/articles/view/447/
and
http://www.oreillynet.com/onjava/blo...list_with.html

It is a templating program that takes a source directory, reads the
files there and outputs them to a destination directory, adjusting both
values and file paths alike.


Main Program
==============
# templatefiller.rb

require 'find'

class Template

# Construct the template object with the template and the
# replacement values. "values" can be a hash, a function,
# or a method.


def initialize(replaceStr, values, fileReplaceStr, fileValues)
@replaceStrs = {}
if values.kind_of?( Hash )
@replaceStrs[ replaceStr ] = values.method( :fetch )
else
@replaceStrs[ replaceStr ] = values.clone()
end

@fileReplaceStrs = {}
if fileValues.kind_of?( Hash )
@fileReplaceStrs[ fileReplaceStr ] = fileValues.method( :fetch )
else
@fileReplaceStrs[ fileReplaceStr ] = fileValues.clone()
end

end


def run(inStream)
outStream = inStream.clone()
@replaceStrs.keys.each { |fileReplaceStr|
outStream.gsub!( /#{fileReplaceStr}(.*?)#{fileReplaceStr}/ ) {
@replaceStrs[ fileReplaceStr ].call( $1 ).to_s
}
}
outStream
end

def runFile(inStream)
outStream = inStream.clone()
@fileReplaceStrs.keys.each { |fileReplaceStr|
outStream.gsub!( /#{fileReplaceStr}(.*?)#{fileReplaceStr}/ ) {
@fileReplaceStrs[fileReplaceStr ].call( $1 ).to_s
}
}
outStream
end

def to_s()
run();
end
end


if File.exists?(ARGV[0])
inputFile = ARGV[0];
else
puts "File #{ARGV[0]} does not exist.\n"
exit
end

ini_hash = {}
ini_hash[""] = section_hash = {} # default unnamed section

data = File.read(inputFile)

data.each do |line|
if /^\[.+\]$/ =~ line # section headers are surrounded by brackets
ini_hash[line.chomp.gsub(/[\[\]]/, '')] = section_hash = {}
else
key,value = line.chomp.split("=",2)
if value
section_hash[key] = value
#puts " #{key} is #{value} "
else
#puts "key is #{key} nil? How about value? #{value} and
section_hash[#{key}] = " if key.nil?
end
end
end

# example of walking through the whole thing
ini_hash.each do |k, v|
unless k.empty?
# v.each {|sk, sv| puts " #{sk}=#{sv}"}
v.each do |sk, sv|
unless sk.nil?
#
end
end
end
end

$matchesTemplate = Template.new(ini_hash["Directories"]["escape"],
ini_hash["TemplateValues"], ini_hash["Directories"]["outEscape"],
ini_hash["PathTemplates"])

inDir = ini_hash["Directories"]["input_directory"]
outDir = ini_hash["Directories"]["output_directory"]
puts "Input Directory is - #{inDir}"
puts "Output Directory is - #{outDir}"
dirs = [inDir]
excludes = ["CVS","classes","images","lib","tlds"]
for dir in dirs
Find.find(dir) do |path|
if FileTest.directory?(path)
if excludes.include?(File.basename(path))
Find.prune # Don't look any further into this directory.
else
next
end
else
if FileTest.file?(path)
if excludes.include?(File.basename(path)) == true
next
end
data = File.read(path)
outFileName = outDir + path[inDir.length, path.length -
inDir.length]
zFileName = $matchesTemplate.runFile(outFileName)
f = File:pen( zFileName, "w" )
f << $matchesTemplate.run(data)
f.close()
end
end
end
end

==============

sample template settings.
#template.ini

[Directories]
input_directory=.\templateSource
output_directory=.\templateDestination
escape=:::
outEscape=___
[TemplateValues]
aaaa=Mary
bbbb=Had
cccc=a
dddd=Little
eeee=Lamb
[PathTemplates]
template=workdone


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Critique of first python code Zack Python 9 02-17-2008 04:07 PM
Request critique of first program cs C Programming 14 09-04-2007 01:01 AM
Request critique of first program cs C Programming 3 09-03-2007 05:17 AM
Eclipse vs. Netbeans. Netbeans is winning me over almost at first glance. Thomas G. Marshall Java 2 10-16-2006 02:54 AM
Re: a serious question for a non-serious cause... henry DVD Video 1 11-08-2003 04:38 PM



Advertisments