We don't really have a standard way of doing things. It always
depends on the situation,
but I'll see what I can do to help here.
Looks mostly okay to me, I do roughly the same thing. We really
should create an abstraction on this, perhaps in ruport-util, because
it's not pretty.
One note:
> :rank => '',
> :hd => '',
> :reg => '',
> :a5d => '',
These should not be necessary. Nil fields should work, and if they
don't, it's a bug.
> (2) What is the proper way to add two columns together to produce a
> row total column?
Hmm... well, if subtable allowed filters and transforms, it might be
something like:
seglist = @segtbl.sub_table(["a","b","total"]) do |t|
t.transform { |r| r.total = r.a + r.b }
end
This isn't the case right now, but that looks kind of neat. It'd mean
that you'd need to explicitly use t.filter { ... } for filtering your
rows though...
What do folks thinks?
> And (3), after all the calculations are done, how can one tell the
> formatter to format the values in a given column. For example, I have
> this method:
>
> # Augment the Integer class with a method to format
> # a number with commas.
> class Integer
> def to_fmt(num)
> num.to_s.strip.gsub(/(.)(?=.{3}+$)/, %q(\1,))
> end
> end
Doesn't this read?
2.to_fmt(2)
Why not just def to_fmt
and then self.to_s
allowing
2.to_fmt
> When is the proper time and method to implement it such that the
> format of numbers is produced with commas.
If performance is a concern, you really need to wire this somewhere in
a custom renderer on a row by row basis.
However, if you can afford to pass through the data twice, there are a
couple choices.
If you know the column names specifically which need formatting, you
can do something like this:
table.replace_column("foo") { |r| r.foo.to_fmt }
You can of course iterate over an array of column names there, but ugh, slow.
Also slow, but only does one extra pass, is this:
with_commas = lambda do |r|
r.each_with_index do |e,i|
if Integer === e
r[i] = e.to_fmt
end
end
end
Table(table.column_names, table.data, :transforms => with_commas)
Hope that helps.
-greg
> And this brings us to one more topic, related to this and that of
> another discussion. Above the columns are renamed (for to_text
> purposes) to create prettier column headings. (I'm really not too
> keen on renaming columns for this purpose. Heading text should be
> relatable to a column without affecting column names.)
If you do this in your renderer, it only effects the column names on
render, not your original data. If it effects your original data,
we're in trouble. :)
This issue has come up before. I don't think of column names as being
independent of 'heading names', but many people do.
Perhaps it'd be helpful if we made the table renderer set
options.column_headings to data.column_names by default, but allow
this to be overridden as needed.
We can then use options.column_headings in the formatters instead of
data.column_names.
What do you think?
-greg
Have you tried this? I haven't tested it myself, but can't see
anything here that wouldn't work currently as it is. Maybe I'm
missing something?
Mike