forked from SerpicoProject/Serpico
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the observer-like design pattern for plugins
- Loading branch information
Showing
3 changed files
with
65 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# For more information on the behavior of this class, look at helpers/plugin_notifier.rb | ||
class PluginListener | ||
# By design, a PluginListener will return no information when notified. | ||
# => This should be overridden by each plugin that need to add content into a report XML | ||
def notify_report_generated(report_id) | ||
return "" | ||
end | ||
|
||
# By design, a PluginListener will do no cleanup when notified. | ||
# => This should be overridden by each plugin that need to cleanup his local database when reports are deleted. | ||
def notify_report_deleted(report_id) | ||
return | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
require 'singleton' | ||
|
||
# This class is used to reproduce an Observer-like design pattern | ||
# => Every plugin that inherits the PluginListener class will receive the events listed in this class | ||
# | ||
# => Do note that the methods in this class should remain generic. If you need something that only applies to 1YWoswqCayG3vIFHuRmnku8g | ||
# plugin, consider doing it elsewhere. | ||
class PluginNotifier | ||
include Singleton | ||
|
||
def initialize | ||
@plugins = [] | ||
end | ||
|
||
# Add a plugin PluginListener | ||
# => The listener will be notified when a report is generated so he can add XML content into the report | ||
# => He will also be notified when a report is deleted so he can cleanup his local database | ||
def attach_plugin(observed_plugin) | ||
if observed_plugin and observed_plugin.class <= PluginListener | ||
@plugins.push observed_plugin | ||
else | ||
raise 'All observed classes must be non-null and inherit from the PluginListener class.' | ||
end | ||
end | ||
|
||
def detach_plugin(observed_plugin) | ||
@plugins.remove observed_plugin | ||
end | ||
|
||
def notify_report_generated(report_id) | ||
returned_xml = "<plugins>\n" | ||
|
||
@plugins.each { |observer| | ||
returned_xml << observer.notify_report_generated(report_id) | ||
} | ||
|
||
returned_xml << "</plugins>\n" | ||
return returned_xml | ||
end | ||
|
||
def notify_report_deleted(report_id) | ||
@plugins.each { |observer| observer.notify_report_deleted(report_id) } | ||
end | ||
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