Quickly,

# First, I'd want a naked struct that will be populated by the data I
actually have.
# To convert your @levels array into this structure, I threw this together:
def naked_tree levels = [], i = 0
 o = {}
 levels[i].each do |key|
   o[key] = naked_tree level, i + 1
 end unless i >= levels.size
 o = nil if i >= levels.size
 o
end
# Kind of weird, but okay...

# Then you can just merge your stuff in recursively.
def merge_hashes unpopulated, populated
 o = {}
 unpopulated.each do |key, value|
   o[key] = merge_hashes(value, (populated.nil? ? nil : populated[key]))
 end if unpopulated.kind_of?(Hash)
 o = populated unless unpopulated.kind_of?(Hash)
 o
end

merge_hashes(naked_tree(@levels), orig_hash)

On 10/18/06, David Turnbull <[EMAIL PROTECTED]> wrote:

Sounds like you're trying to build a class to vend a pivot table based on
what it's fed.  I'd suggest instead making your existing data models look
like a pivot table.  Once that is working, you should see all the patterns
you need to refactor it into something reusable like an acts_as_pivot_table.
 For the sparse data, ignore it.  Build a wrapper to make it look full if
you need.

-david



On Oct 18, 2006, at 1:30 PM, Bryan Donovan wrote:
Hi,
I'm working on a class that I'll use in some rails applications that are
used to display data in cross-tabulation format (or as it's called in Excel,
a "pivot table").  Right now I'm taking an ActiveRecord object, specifying
what fields should go where in the crosstab, and then generating a big
nested hash (which I call a "tree", but not sure if that's the right
terminology).  This isn't too difficult if I always know how many levels the
tree will have, etc., but I want this to be flexible enough to handle any
reasonable number of levels.  I've also generated crosstab datasets with SQL
directly, but it gets a bit hard to follow and in my current situation, the
query runs slowly.  This is a bit difficult to explain, so I've put up a web
page with an example hash and the desired output here:

http://bryandonovan.com/pastes/crosstab.html



_______________________________________________
PDXRuby mailing list
[email protected]
IRC: #pdx.rb on irc.freenode.net
http://lists.pdxruby.org/mailman/listinfo/pdxruby


_______________________________________________
PDXRuby mailing list
[email protected]
IRC: #pdx.rb on irc.freenode.net
http://lists.pdxruby.org/mailman/listinfo/pdxruby

Reply via email to