diff --git a/lib/sidetiq/subclass_tracking.rb b/lib/sidetiq/subclass_tracking.rb new file mode 100644 index 0000000..07e18b7 --- /dev/null +++ b/lib/sidetiq/subclass_tracking.rb @@ -0,0 +1,20 @@ +module Sidetiq + module SubclassTracking + def subclasses(deep = false) + @subclasses ||= [] + + if deep + @subclasses.inject([]) do |all, subclass| + (all << subclass) + subclass.subclasses(true) + end + else + @subclasses + end + end + + def inherited(klass) + super + subclasses << klass + end + end +end diff --git a/test/test_subclass_tracking.rb b/test/test_subclass_tracking.rb new file mode 100644 index 0000000..b718d7c --- /dev/null +++ b/test/test_subclass_tracking.rb @@ -0,0 +1,22 @@ +require_relative 'helper' + +class TestSubclassTracking < Sidetiq::TestCase + class Foo + extend Sidetiq::SubclassTracking + end + + class Bar < Foo + end + + class Baz < Bar + end + + def test_subclasses_non_recursive + assert_equal [Bar], Foo.subclasses + end + + def test_subclasses_recursive + assert_equal [Bar, Baz], Foo.subclasses(true) + end +end +