I’m sure I’m just being an idiot here… my mind is a little foggy this
morning, but I’m having a hard time understanding how to accomplish
this. I’ve written a class (I’ll just put some snippets in for
understanding) in which I’d like to be able to use the following
behavior:
irb> columns = [[1, “Hostname”], [2, “Model”], [5, “OS Version”]]
irb> report = InventoryReport.new(“HARDWARE_QUERY”, columns)
irb> report.query.map {|a| a.first.downcase!; a}.save “/tmp/test.xls”
Where query() returns an array like [[“host1”, “Dell”, “Windows”],
[“host2”, “Hitachi”, “Windows”], …]
However, throwing a map (or select or whatever) in there (obviously)
ends up returning an Array. How would I handle this so that it works as
I would like? Do I need to move my save(), to_csv(), and to_xls() stuff
into the Array class?
Sorry if this is a dumb question.
class InventoryReport < Array
def initialize(query_name, fields)
@query_name = query_name
@fields = fields
super()
end
def query
cols = @fields.map {|a| a.first - 1}
%x{runquery #{@query_name}}.each_line do |l|
t = l.chomp.split
tmp = []
cols.each do |a|
tmp << t[a].strip
end
self << tmp
end
self
end
def save(filename)
case filename
when /.*\.csv/ then to_csv(filename)
when /.*\.xls/ then
to_csv(filename.gsub(/xls$/, "csv"))
to_xls(filename, filename.gsub(/xls$/, "csv")
end
end
def to_csv(...)
end
def to_xls(...)
end
end