Class: ComputedModel::DepGraph::Sorted Private

Inherits:
Object
  • Object
show all
Defined in:
lib/computed_model/dep_graph.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

A preprocessed graph with topologically sorted order.

Generated by #tsort.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(original, nodes_in_order) ⇒ Sorted

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Sorted.

Parameters:



198
199
200
201
# File 'lib/computed_model/dep_graph.rb', line 198

def initialize(original, nodes_in_order)
  @original = original
  @nodes_in_order = nodes_in_order
end

Instance Attribute Details

#nodes_in_orderArray<ComputedModel::DepGraph::Node> (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



194
195
196
# File 'lib/computed_model/dep_graph.rb', line 194

def nodes_in_order
  @nodes_in_order
end

#originalComputedModel::DepGraph (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



192
193
194
# File 'lib/computed_model/dep_graph.rb', line 192

def original
  @original
end

Instance Method Details

#plan(deps) ⇒ ComputedModel::Plan

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Computes the plan for the given requirements.

Examples:

Plain dependencies

sorted.plan([:field1, :field2])

Dependencies with subfields

sorted.plan([:field1, field2: { optional_field: {} }])

Parameters:

  • deps (Array)

    the list of required nodes. Each dependency can optionally include subfields hashes.

Returns:



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/computed_model/dep_graph.rb', line 213

def plan(deps)
  normalized = ComputedModel.normalize_dependencies(deps)
  subfields_hash = {}
  uses = Set[]
  plan_nodes = []

  normalized.each do |name, subfields|
    raise "No dependency info for ##{name}" unless @original[name]

    uses.add(name)
    (subfields_hash[name] ||= []).unshift(*subfields)
  end
  @nodes_in_order.each do |node|
    uses.add(node.name) if node.type == :primary
    next unless uses.include?(node.name)

    node_subfields = ComputedModel::NormalizableArray.new(subfields_hash[node.name] || [])
    deps = Set[]
    node.edges.each_value do |edge|
      specval = edge.evaluate(node_subfields)
      if specval.any?
        deps.add(edge.name)
        uses.add(edge.name)
        (subfields_hash[edge.name] ||= []).unshift(*specval)
      end
    end
    plan_nodes.push(ComputedModel::Plan::Node.new(node.name, deps, node_subfields))
  end
  ComputedModel::Plan.new(plan_nodes.reverse, normalized.keys.to_set)
end