Module: Sequel::GraphEach

Defined in:
lib/sequel/extensions/graph_each.rb

Instance Method Summary collapse

Instance Method Details

#graph_eachObject

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.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/sequel/extensions/graph_each.rb', line 23

def graph_each
  # Reject tables with nil datasets, as they are excluded from
  # the result set
  datasets = @opts[:graph][:table_aliases].to_a.reject{|ta,ds| ds.nil?}
  # Get just the list of table aliases into a local variable, for speed
  table_aliases = datasets.collect{|ta,ds| ta}
  # Get an array of arrays, one for each dataset, with
  # the necessary information about each dataset, for speed
  datasets = datasets.collect{|ta, ds| [ta, ds, ds.row_proc]}
  # Use the manually set graph aliases, if any, otherwise
  # use the ones automatically created by .graph
  column_aliases = @opts[:graph_aliases] || @opts[:graph][:column_aliases]
  fetch_rows(select_sql) do |r|
    graph = {}
    # Create the sub hashes, one per table
    table_aliases.each{|ta| graph[ta]={}}
    # Split the result set based on the column aliases
    # If there are columns in the result set that are
    # not in column_aliases, they are ignored
    column_aliases.each do |col_alias, tc|
      ta, column = tc
      graph[ta][column] = r[col_alias]
    end
    # For each dataset run the row_proc if applicable
    datasets.each do |ta,ds,rp|
      g = graph[ta]
      graph[ta] = if g.values.any?{|x| !x.nil?}
        rp ? rp.call(g) : g
      else
        nil
      end
    end

    yield graph
  end
  self
end