Class: BlastRadius::ImpactCalculator

Inherits:
Object
  • Object
show all
Defined in:
lib/blast_radius/impact_calculator.rb

Instance Method Summary collapse

Constructor Details

#initialize(configuration = nil) ⇒ ImpactCalculator

Returns a new instance of ImpactCalculator.



5
6
7
# File 'lib/blast_radius/impact_calculator.rb', line 5

def initialize(configuration = nil)
  @configuration = configuration || BlastRadius.configuration
end

Instance Method Details

#calculate(record, options = {}) ⇒ Hash

Calculate the impact of deleting a specific record

Parameters:

  • record (ActiveRecord::Base)

    record to analyze

  • options (Hash) (defaults to: {})

    options

    • :max_depth [Integer] maximum depth (default: from configuration)
    • :include_nullify [Boolean] include nullify associations (default: from configuration)
    • :include_restrict [Boolean] include restrict associations (default: from configuration)

Returns:

  • (Hash)

    hash of model names to record counts



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/blast_radius/impact_calculator.rb', line 17

def calculate(record, options = {})
  tree_builder = DependencyTree.new(@configuration)
  tree = tree_builder.build(record.class, options)

  impact = Hash.new(0)
  # Start with root node's children
  tree.children.each do |child|
    calculate_node_impact(child, record, impact)
  end
  impact
end

#dry_run(record, options = {}) ⇒ Hash

Perform a dry run to see what would be deleted

Parameters:

  • record (ActiveRecord::Base)

    record to analyze

  • options (Hash) (defaults to: {})

    options (same as #calculate)

Returns:

  • (Hash)

    detailed impact information with counts and sample IDs



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/blast_radius/impact_calculator.rb', line 34

def dry_run(record, options = {})
  tree_builder = DependencyTree.new(@configuration)
  tree = tree_builder.build(record.class, options)

  impact = {}
  # Start with root node's children
  tree.children.each do |child|
    calculate_detailed_impact(child, record, impact)
  end
  impact
end