Module Sequel::GraphEach
In: lib/sequel/extensions/graph_each.rb

Methods

Public Instance methods

Fetch the rows, split them into component table parts, tranform and run the row_proc on each part (if applicable), and yield a hash of the parts.

[Source]

    # File lib/sequel/extensions/graph_each.rb, line 23
23:     def graph_each
24:       # Reject tables with nil datasets, as they are excluded from
25:       # the result set
26:       datasets = @opts[:graph][:table_aliases].to_a.reject{|ta,ds| ds.nil?}
27:       # Get just the list of table aliases into a local variable, for speed
28:       table_aliases = datasets.collect{|ta,ds| ta}
29:       # Get an array of arrays, one for each dataset, with
30:       # the necessary information about each dataset, for speed
31:       datasets = datasets.collect{|ta, ds| [ta, ds, ds.row_proc]}
32:       # Use the manually set graph aliases, if any, otherwise
33:       # use the ones automatically created by .graph
34:       column_aliases = @opts[:graph_aliases] || @opts[:graph][:column_aliases]
35:       fetch_rows(select_sql) do |r|
36:         graph = {}
37:         # Create the sub hashes, one per table
38:         table_aliases.each{|ta| graph[ta]={}}
39:         # Split the result set based on the column aliases
40:         # If there are columns in the result set that are
41:         # not in column_aliases, they are ignored
42:         column_aliases.each do |col_alias, tc|
43:           ta, column = tc
44:           graph[ta][column] = r[col_alias]
45:         end
46:         # For each dataset run the row_proc if applicable
47:         datasets.each do |ta,ds,rp|
48:           g = graph[ta]
49:           graph[ta] = if g.values.any?{|x| !x.nil?}
50:             rp ? rp.call(g) : g
51:           else
52:             nil
53:           end
54:         end
55: 
56:         yield graph
57:       end
58:       self
59:     end

[Validate]