Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Require notifiers to be registered

Less magic. See [#104]
  • Loading branch information...
commit 15ba26a4425ef1d08ffe9ff051aea2161836b346 1 parent 1c3be10
@sr sr authored
View
17 lib/integrity.rb
@@ -47,6 +47,17 @@ def self.new(config=nil)
DataMapper.setup(:default, self.config[:database_uri])
end
+ def self.register_notifier(klass)
+ raise ArgumentError unless valid_notifier?(klass)
+
+ self.notifiers[klass.to_s.split(":").last] = klass
+ end
+
+ def self.notifiers
+ @notifiers ||= {}
+ @notifiers
+ end
+
def self.default_configuration
@defaults ||= { :database_uri => "sqlite3::memory:",
:export_directory => "/tmp/exports",
@@ -76,6 +87,12 @@ def self.logger
end
private_class_method :logger
+ def self.valid_notifier?(notifier)
+ notifier.respond_to?(:to_haml) && notifier.respond_to?(:notify_of_build) &&
+ notifier != Notifier::Base
+ end
+ private_class_method :valid_notifier?
+
class LogFormatter < Logger::Formatter
def call(severity, time, progname, msg)
time.strftime("[%H:%M:%S] ") + msg2str(msg) + "\n"
View
13 lib/integrity/helpers/forms.rb
@@ -16,12 +16,13 @@ def checkbox(name, condition, extras={})
attrs.update(extras)
end
- def notifier_form(notifier)
- haml(notifier.to_haml, :layout => :notifier, :locals => {
- :config => current_project.config_for(notifier.to_s.split(/::/).last),
- :notifier => "#{notifier.to_s.split(/::/).last}",
- :enabled => current_project.notifies?(notifier)
- })
+ def notifier_form
+ Notifier.available.each_pair { |name, klass|
+ haml_concat haml(klass.to_haml, :layout => :notifier, :locals => {
+ :notifier => name,
+ :enabled => current_project.notifies?(name),
+ :config => current_project.config_for(name) })
+ }
end
end
end
View
14 lib/integrity/notifier.rb
@@ -14,8 +14,12 @@ class Notifier
validates_is_unique :name, :scope => :project_id
validates_present :project_id
+ def self.register(klass)
+ Integrity.register_notifier(klass)
+ end
+
def self.available
- constants.map { |name| const_get(name) }.select { |notifier| valid_notifier?(notifier) }
+ Integrity.notifiers
end
def notify_of_build(build)
@@ -23,14 +27,8 @@ def notify_of_build(build)
end
private
-
def to_const
- self.class.module_eval(name)
- end
-
- def self.valid_notifier?(notifier)
- notifier.respond_to?(:to_haml) && notifier.respond_to?(:notify_of_build) && notifier != Notifier::Base
+ self.class.available[name]
end
- private_class_method :valid_notifier?
end
end
View
2  test/acceptance/build_notifications_test.rb
@@ -15,6 +15,8 @@ class BuildNotificationsTest < Test::Unit::AcceptanceTestCase
# in the global #before
load "helpers/acceptance/textfile_notifier.rb"
load "helpers/acceptance/email_notifier.rb"
+ Notifier.register(Integrity::Notifier::Textfile)
+ Notifier.register(Integrity::Notifier::Email)
end
scenario "an admin sets up a notifier and issue a manual build" do
View
3  test/helpers.rb
@@ -70,10 +70,11 @@ class << self
before(:each) do
RR.reset
DataMapper.auto_migrate!
- Integrity.instance_variable_set(:@config, nil)
Notifier.available.each { |n|
Notifier.send(:remove_const, n.to_s.split(":").last.to_sym)
}
+ Integrity.instance_variable_set(:@config, nil)
+ Integrity.instance_variable_set(:@notifiers, nil)
repository(:default) do
transaction = DataMapper::Transaction.new(repository)
View
17 test/unit/integrity_test.rb
@@ -32,4 +32,21 @@ class IntegrityTest < Test::Unit::TestCase
Integrity.config[:foo] = "bar"
Integrity.config[:foo].should == "bar"
end
+
+ describe "Registering a notifier" do
+ it "registers given notifier class" do
+ load "helpers/acceptance/textfile_notifier.rb"
+
+ Integrity.register_notifier(Integrity::Notifier::Textfile)
+ assert_equal Integrity::Notifier::Textfile, Integrity.notifiers["Textfile"]
+ end
+
+ it "raises ArgumentError if given class is not a valid notifier" do
+ assert_raise(ArgumentError) {
+ Integrity.register_notifier(Class.new)
+ }
+
+ assert Integrity.notifiers.empty?
+ end
+ end
end
View
18 test/unit/notifier_test.rb
@@ -66,18 +66,20 @@ class NotifierTest < Test::Unit::TestCase
end
end
- it "knows which notifiers are available" do
+ test "managing available notifiers" do
Notifier.gen(:irc)
- Notifier.gen(:twitter)
- Notifier.should have(2).available
- Notifier.available.should include(Integrity::Notifier::IRC)
- Notifier.available.should include(Integrity::Notifier::Twitter)
+ Notifier.register(Integrity::Notifier::IRC)
+
+ assert_equal 1, Notifier.available.size
end
it "knows how to notify the world of a build" do
- irc = Notifier.generate(:irc)
- build = Integrity::Build.generate
- Notifier::IRC.expects(:notify_of_build).with(build, irc.config)
+ irc = Notifier.gen(:irc)
+ Notifier.register(Integrity::Notifier::IRC)
+ build = Build.gen
+
+ mock(Notifier::IRC).notify_of_build(build, irc.config) { nil }
+
irc.notify_of_build(build)
end
end
View
3  views/new.haml
@@ -29,8 +29,7 @@
%input.hidden{ :name => "project_data[public]", :value => "0", :type => "hidden" }
%input.checkbox#project_public{ checkbox("project_data[public]", @project.public?) }
- - Integrity::Notifier.available.each do |notifier|
- = notifier_form(notifier)
+ - notifier_form
%p.submit
%button.positive{ :type => "submit" }= @project.new_record? ? "Create Project" : "Update Project"
Please sign in to comment.
Something went wrong with that request. Please try again.