This class seems to provide all but one element of the functionality
that I was seeking. I would very much appreciate a critique how it
varies from OOP ideals and how those weaknesses should be corrected. I
would also like a few pointers on how I would go about using Test::Unit
and Fixtures to test drive this class.
There appears to be something wrong with either my appreciation of how
Syslog is supposed to work, with its implementation, or with how I
understand class variables are preserved.
The problem is that given these three lines:
test1 = Mv2Dir.new("./").log
test2 = Mv2Dir.new("./").log
test1 = Mv2Dir.new("./").log
The third line that reassigns a new instance of Mv2Dir to test1 results
in @@logid being treated as nil in .log and the subsequent @@logid.info
calls fail. However, when I test for @@logid == nil and open syslog when
true then the third line fails with the error that syslog is already
opened. There seems to be a problem with Mv2Dir and Syslog when a
particular object name is reused. I cannot see what the problem is
though. Can anyone provide me with some ideas?
#------------------------------------------------------------------------------
class Mv2Dir
require 'fileutils'
require 'syslog'
attr_reader :path, :message
def get(sourcedir,globspec,ext=nil)
# given a valid source dir and file globbing spec get
# all the files in source dir that match and move them
# to @path, appending ext if given. (See: put)
sourcedir = File.expand_path(sourcedir.to_s)
if self.validpath?(sourcedir) then
Dir.glob(File.join(sourcedir.to_s,globspec.to_s)).each do |ff|
fo = File.join(@path,File.basename(ff)) + ext.to_s
@message = "Moving #{ff} to #{fo}"
FileUtils.mv(ff,fo)
self.log
end
end
return self
end
def initialize(path="./")
@path = File.expand_path(path.to_s)
@message = nil
@@logid = nil
self.validpath?(@path)
return self
end
def log(*message)
# Singleton check for open logging proces,
# open one if none exists. Log message to syslog.
# This has problems if an instance name is reused.
if !@@logid then @@logid = Syslog.open($0) end
if message.size > 0 then
message.each { |m| @@logid.info(m.to_s) }
else
@@logid.info(@message.to_s)
end
return self
end
def put(targetdir,globspec,ext=nil)
# given a valid target dir and file globbing spec get
# all the files in @path that match and move them to
# target dir appending ext if given. (See: get)
targetdir = File.expand_path(targetdir.to_s)
if self.validpath?(targetdir) then
Dir.glob(File.join(@path,globspec.to_s)).each do |ff|
fo = File.join(targetdir,File.basename(ff)) + ext.to_s
puts "Moving #{ff} to #{fo}"
@message = "Moving #{ff} to #{fo}"
FileUtils.mv(ff,fo)
self.log
end
end
return self
end
def readable?
if !File.readable?(@path) then
@message = "#{@path} is not readable."
FALSE
else
@message = "#{@path} is readable."
TRUE
end
end
def trim(filename,ext)
File.basename(filename.to_s, ext.to_s)
end
def validpath?(path)
if !File.exists?(path) then
@message = "#{path} does not exist on this system."
FALSE
elsif
!File.directory?(path) then
@message = "#{path} exists but is not a directory."
FALSE
else
@message = "#{path} is a valid directory."
TRUE
end
end
def writable?
if !File.writable?(@path) then
@message = "#{@path} is not writeable."
FALSE
else
@message = "#{@path} is writeable."
TRUE
end
end
def writeable?
self.writable?
end
end # End class Mv2Dir
#------------------------------------------------------------------------------
if __FILE__ == $0
test_in = Mv2Dir.new("~/ruby/data/source").log
test_out = Mv2Dir.new("~/ruby/data/target").log
test_glob = "QPCCCMR*"
test_in.put(test_out.path,test_glob,".csv").log
#or this will do the same thing.
#test_out.get(test_in.path,test_glob,".csv").log
--
Posted via http://www.ruby-forum.com/.