class Scenic::Adapters::Postgres::RefreshDependencies::DependencyParser
Attributes
raw_dependencies[R]
view_to_refresh[R]
Public Class Methods
new(raw_dependencies, view_to_refresh)
click to toggle source
# File lib/scenic/adapters/postgres/refresh_dependencies.rb, line 30 def initialize(raw_dependencies, view_to_refresh) @raw_dependencies = raw_dependencies @view_to_refresh = view_to_refresh end
Public Instance Methods
to_sorted_array()
click to toggle source
We’re given an array from the SQL query that looks kind of like this
- [“view_name”, “{‘dependency_1’, ‘dependency_2’}”]
-
We need to parse that into a more easy to understand data type so we can use the Tsort module from the Standard Library to topologically sort those out so we can refresh in the correct order, so we parse that raw data into a hash.
Then, once Tsort has worked it magic, we’re given a sorted 1-D array
- “dependency_1”, “dependency_2”, “view_name”
-
So we then need to slice off just the bit leading up to the view that we’re refreshing, so we find where in the topologically sorted array our given view is, and return all the dependencies up to that point.
# File lib/scenic/adapters/postgres/refresh_dependencies.rb, line 50 def to_sorted_array dependency_hash = parse_to_hash(raw_dependencies) sorted_arr = tsort(dependency_hash) idx = sorted_arr.find_index do |dep| if view_to_refresh.to_s.include?(".") dep == view_to_refresh.to_s else dep.ends_with?(".#{view_to_refresh}") end end if idx.present? sorted_arr[0...idx] else [] end end
Private Instance Methods
parse_to_hash(dependency_rows)
click to toggle source
# File lib/scenic/adapters/postgres/refresh_dependencies.rb, line 73 def parse_to_hash(dependency_rows) dependency_rows.each_with_object({}) do |row, hash| formatted_dependencies = row.last.tr("{}", "").split(",") formatted_dependencies.each do |dependency| hash[dependency] = [] unless hash[dependency] end hash[row.first] = formatted_dependencies end end
tsort(hash)
click to toggle source
# File lib/scenic/adapters/postgres/refresh_dependencies.rb, line 83 def tsort(hash) each_node = lambda { |&b| hash.each_key(&b) } each_child = lambda { |n, &b| hash[n].each(&b) } TSort.tsort(each_node, each_child) end