Skip to content

Commit

Permalink
Move notifier registering logic into one place
Browse files Browse the repository at this point in the history
  • Loading branch information
sr committed Apr 5, 2009
1 parent 728b832 commit 6e60639
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 46 deletions.
25 changes: 4 additions & 21 deletions lib/integrity.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -40,17 +40,6 @@ def self.new(config=nil)
DataMapper.setup(:default, self.config[:database_uri]) DataMapper.setup(:default, self.config[:database_uri])
end 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 def self.default_configuration
@defaults ||= { :database_uri => "sqlite3::memory:", @defaults ||= { :database_uri => "sqlite3::memory:",
:export_directory => "/tmp/exports", :export_directory => "/tmp/exports",
Expand Down Expand Up @@ -80,15 +69,9 @@ def self.logger
end end
private_class_method :logger private_class_method :logger


def self.valid_notifier?(notifier) class LogFormatter < Logger::Formatter
notifier.respond_to?(:to_haml) && notifier.respond_to?(:notify_of_build) && def call(severity, time, progname, msg)
notifier != Notifier::Base time.strftime("[%H:%M:%S] ") + msg2str(msg) + "\n"
end
private_class_method :valid_notifier?

class LogFormatter < Logger::Formatter
def call(severity, time, progname, msg)
time.strftime("[%H:%M:%S] ") + msg2str(msg) + "\n"
end
end end
end
end end
15 changes: 12 additions & 3 deletions lib/integrity/notifier.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -15,13 +15,22 @@ class Notifier
validates_is_unique :name, :scope => :project_id validates_is_unique :name, :scope => :project_id
validates_present :project_id validates_present :project_id


def self.available
@@_notifiers ||= {}
@@_notifiers
end

def self.register(klass) def self.register(klass)
Integrity.register_notifier(klass) raise ArgumentError unless valid?(klass)

available[klass.to_s.split(":").last] = klass
end end


def self.available def self.valid?(notifier)
Integrity.notifiers notifier.respond_to?(:to_haml) && notifier.respond_to?(:notify_of_build) &&
notifier != Notifier::Base
end end
private_class_method :valid?


def notify_of_build(build) def notify_of_build(build)
to_const.notify_of_build(build, config) to_const.notify_of_build(build, config)
Expand Down
3 changes: 2 additions & 1 deletion test/helpers.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ class << self
Notifier.available.each { |n| Notifier.available.each { |n|
Notifier.send(:remove_const, n.to_s.split(":").last.to_sym) Notifier.send(:remove_const, n.to_s.split(":").last.to_sym)
} }

Notifier.available.clear
Integrity.instance_variable_set(:@config, nil) Integrity.instance_variable_set(:@config, nil)
Integrity.instance_variable_set(:@notifiers, nil)
end end


after(:each) do after(:each) do
Expand Down
17 changes: 0 additions & 17 deletions test/unit/integrity_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -32,21 +32,4 @@ class IntegrityTest < Test::Unit::TestCase
Integrity.config[:foo] = "bar" Integrity.config[:foo] = "bar"
Integrity.config[:foo].should == "bar" Integrity.config[:foo].should == "bar"
end 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 end
20 changes: 16 additions & 4 deletions test/unit/notifier_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -66,11 +66,23 @@ class NotifierTest < Test::Unit::TestCase
end end
end end


test "managing available notifiers" do describe "Registering a notifier" do
Notifier.gen(:irc) it "registers given notifier class" do
Notifier.register(Integrity::Notifier::IRC) load "helpers/acceptance/textfile_notifier.rb"

Notifier.register(Integrity::Notifier::Textfile)

assert_equal Integrity::Notifier::Textfile,
Notifier.available["Textfile"]
end

it "raises ArgumentError if given class is not a valid notifier" do
assert_raise(ArgumentError) {
Notifier.register(Class.new)
}


assert_equal 1, Notifier.available.size assert Notifier.available.empty?
end
end end


it "knows how to notify the world of a build" do it "knows how to notify the world of a build" do
Expand Down

0 comments on commit 6e60639

Please sign in to comment.