Class: Verifly::DependentCallbacks::CallbackGroup::TSortService

Inherits:
Object
  • Object
show all
Includes:
TSort
Defined in:
lib/verifly/dependent_callbacks/callback_group.rb

Overview

Implements topoplogy sorting of callbacks. As far as CallbackGroup is designed to store Callbacks, it is unable to link them into graph immediately . Service can do it, because if some callbacks are missing on compilation stage, there should be an error

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(callback_group) ⇒ TSortService

Returns a new instance of TSortService.

Parameters:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/verifly/dependent_callbacks/callback_group.rb', line 30

def initialize(callback_group)
  self.dependencies = Hash.new { |h, k| h[k] = Set[] }

  callback_group.list.each do |callback|
    dependencies[callback] ||= []

    callback.before.each do |key|
      dependencies[callback_group.index.fetch(key)] << callback
    end

    callback.after.each do |key|
      dependencies[callback] << callback_group.index.fetch(key)
    end
  end
end

Instance Attribute Details

#dependencies{ Callback => Callback }

dependency graph

Returns:



18
19
20
# File 'lib/verifly/dependent_callbacks/callback_group.rb', line 18

def dependencies
  @dependencies
end

Class Method Details

.call(callback_group) ⇒ [Callback]

Returns tsorted callbacks array (aka sequence).

Parameters:

Returns:

  • ([Callback])

    tsorted callbacks array (aka sequence)



25
26
27
# File 'lib/verifly/dependent_callbacks/callback_group.rb', line 25

def self.call(callback_group)
  new(callback_group).tsort
end