-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Rustam Ibragimov
committed
Jan 13, 2018
1 parent
5325e06
commit 8ac8464
Showing
10 changed files
with
164 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# frozen_string_literal: true | ||
|
||
module EvilEvents::Core::Events::EventExtensions | ||
# @api private | ||
# @since 0.3.0 | ||
module Hookable | ||
class << self | ||
# @param base_class [Class] | ||
# | ||
# @since 0.3.0 | ||
def included(base_class) | ||
base_class.extend(ClassMethods) | ||
end | ||
end | ||
|
||
# @api private | ||
# @since 0.3.0 | ||
def __call_on_error_hooks__ | ||
self.class.__on_error_hooks__.each do |hook| | ||
hook.call(self) | ||
end | ||
end | ||
|
||
# @api private | ||
# @since 0.3.0 | ||
def __call_before_hooks__ | ||
self.class.__before_hooks__.each do |hook| | ||
hook.call(self) | ||
end | ||
end | ||
|
||
# @api private | ||
# @since 0.3.0 | ||
def __call_after_hooks__ | ||
self.class.__after_hooks__.each do |hook| | ||
hook.call(self) | ||
end | ||
end | ||
|
||
# @since 0.3.0 | ||
module ClassMethods | ||
# @param hook [#call] | ||
# | ||
# @api public | ||
# @since 0.3.0 | ||
def before_emit(hook) | ||
__before_hooks__ << BeforeHook.new(hook) | ||
end | ||
|
||
# @param hook [#call] | ||
# | ||
# @api public | ||
# @since 0.3.0 | ||
def after_emit(hook) | ||
__after_hooks__ << AfterHook.new(hook) | ||
end | ||
|
||
# @param hook [#call] | ||
# | ||
# @api public | ||
# @since 0.3.0 | ||
def on_error(hook) | ||
__on_error_hooks__ << OnErrorHook.new(hook) | ||
end | ||
|
||
# @return [Concurrent::Array<BeforeHook>] | ||
# | ||
# @api private | ||
# @since 0.3.0 | ||
def __before_hooks__ | ||
@__before_hooks__ ||= Concurrent::Array.new | ||
end | ||
|
||
# @return [Concurrent::Array<AfterHook>] | ||
# | ||
# @api private | ||
# @since 0.3.0 | ||
def __after_hooks__ | ||
@__after_hooks__ ||= Concurrent::Array.new | ||
end | ||
|
||
# @return [Concurrent::Array<OnErrorHook>] | ||
# | ||
# @api private | ||
# @since 0.3.0 | ||
def __on_error_hooks__ | ||
@__on_error_hooks__ ||= Concurrent::Array.new | ||
end | ||
end | ||
end | ||
end |
27 changes: 27 additions & 0 deletions
27
lib/evil_events/core/events/event_extensions/hookable/abstract_hook.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
module EvilEvents::Core::Events::EventExtensions::Hookable | ||
# @api private | ||
# @since 0.3.0 | ||
class AbstractHook | ||
# @return [#call] | ||
# | ||
# @since 0.3.0 | ||
attr_reader :callable | ||
|
||
# @param callable [#call] | ||
# | ||
# @since 0.3.0 | ||
def initialize(callable) | ||
@callable = callable | ||
end | ||
|
||
# @param event [EvilEvents::Core::Events::AbstractEvent] | ||
# @return void | ||
# | ||
# @since 0.3.0 | ||
def call(event) | ||
callable.call(event) | ||
end | ||
end | ||
end |
7 changes: 7 additions & 0 deletions
7
lib/evil_events/core/events/event_extensions/hookable/after_hook.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
module EvilEvents::Core::Events::EventExtensions::Hookable | ||
# @api private | ||
# @since 0.3.0 | ||
AfterHook = Class.new(AbstractHook) | ||
end |
7 changes: 7 additions & 0 deletions
7
lib/evil_events/core/events/event_extensions/hookable/before_hook.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
module EvilEvents::Core::Events::EventExtensions::Hookable | ||
# @api private | ||
# @since 0.3.0 | ||
BeforeHook = Class.new(AbstractHook) | ||
end |
7 changes: 7 additions & 0 deletions
7
lib/evil_events/core/events/event_extensions/hookable/on_error_hook.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
module EvilEvents::Core::Events::EventExtensions::Hookable | ||
# @api private | ||
# @since 0.3.0 | ||
OnErrorHook = Class.new(AbstractHook) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters