Class: Verifly::DependentCallbacks::CallbackGroup

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

Overview

Handles callbacks with same ‘group’ option, allowing to do sequential invokation of them

Defined Under Namespace

Classes: TSortService

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) { ... } ⇒ CallbackGroup

Returns a new instance of CallbackGroup.

Parameters:

  • name (Symbol)

    name of callback group

Yields:

  • self if block given



65
66
67
68
69
70
71
# File 'lib/verifly/dependent_callbacks/callback_group.rb', line 65

def initialize(name)
  self.name = name
  self.index = {}
  self.list = []

  yield(self) if block_given?
end

Instance Attribute Details

#index{Symbol => Callback}

index for named callback lookup

Returns:

  • ({Symbol => Callback})

    the current value of index



11
12
13
# File 'lib/verifly/dependent_callbacks/callback_group.rb', line 11

def index
  @index
end

#list[Callback]

all callbacks

Returns:

  • ([Callback])

    the current value of list



11
12
13
# File 'lib/verifly/dependent_callbacks/callback_group.rb', line 11

def list
  @list
end

#nameSymbol

name of callback group

Returns:

  • (Symbol)

    the current value of name



11
12
13
# File 'lib/verifly/dependent_callbacks/callback_group.rb', line 11

def name
  @name
end

Instance Method Details

#add_callback(callback) ⇒ Object

Adds callback to list and index, reset sequence

Parameters:



75
76
77
78
79
80
# File 'lib/verifly/dependent_callbacks/callback_group.rb', line 75

def add_callback(callback)
  list << callback
  index[callback.name] = callback if callback.name

  @sequence = nil
end

#digestNumeric

Digest change forces recompilation of callback group in service

Returns:

  • (Numeric)


101
102
103
# File 'lib/verifly/dependent_callbacks/callback_group.rb', line 101

def digest
  [name, list].hash
end

#merge(other) ⇒ Object

Merges with another group

Parameters:

Raises:

  • if group names differ



85
86
87
88
89
90
91
# File 'lib/verifly/dependent_callbacks/callback_group.rb', line 85

def merge(other)
  raise "Only groups with one name could be merged" unless name == other.name

  [*list, *other.list].each_with_object(CallbackGroup.new(name)) do |callback, group|
    group.add_callback(callback)
  end
end

#sequence[Callback]

Memoizes tsorted graph

Returns:



95
96
97
# File 'lib/verifly/dependent_callbacks/callback_group.rb', line 95

def sequence
  @sequence ||= TSortService.call(self)
end

#to_dot(binding_) ⇒ Object

Renders graphviz dot-representation of callback group

Returns:

  • graphviz dot



107
108
109
110
111
112
# File 'lib/verifly/dependent_callbacks/callback_group.rb', line 107

def to_dot(binding_)
  template_path = File.expand_path("callback_group.dot.erb", __dir__)
  erb = ERB.new(File.read(template_path))
  erb.filename = template_path
  erb.result(binding)
end