Class: Verifly::DependentCallbacks::CallbackGroup
- Inherits:
-
Object
- Object
- Verifly::DependentCallbacks::CallbackGroup
- 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
-
#index ⇒ {Symbol => Callback}
index for named callback lookup.
-
#list ⇒ [Callback]
all callbacks.
-
#name ⇒ Symbol
name of callback group.
Instance Method Summary collapse
-
#add_callback(callback) ⇒ Object
Adds callback to list and index, reset sequence.
-
#digest ⇒ Numeric
Digest change forces recompilation of callback group in service.
-
#initialize(name) { ... } ⇒ CallbackGroup
constructor
A new instance of CallbackGroup.
-
#merge(other) ⇒ Object
Merges with another group.
-
#sequence ⇒ [Callback]
Memoizes tsorted graph.
-
#to_dot(binding_) ⇒ Object
Renders graphviz dot-representation of callback group.
Constructor Details
#initialize(name) { ... } ⇒ CallbackGroup
Returns a new instance of CallbackGroup.
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
11 12 13 |
# File 'lib/verifly/dependent_callbacks/callback_group.rb', line 11 def index @index end |
#list ⇒ [Callback]
all callbacks
11 12 13 |
# File 'lib/verifly/dependent_callbacks/callback_group.rb', line 11 def list @list end |
#name ⇒ Symbol
name of callback group
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
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 |
#digest ⇒ Numeric
Digest change forces recompilation of callback group in service
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
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
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
107 108 109 110 111 112 |
# File 'lib/verifly/dependent_callbacks/callback_group.rb', line 107 def to_dot(binding_) template_path = File.("callback_group.dot.erb", __dir__) erb = ERB.new(File.read(template_path)) erb.filename = template_path erb.result(binding) end |