mirror of
https://github.com/endofunky/sidetiq.git
synced 2022-11-09 13:53:30 -05:00
Add SubclassTracking module.
This commit is contained in:
parent
011f543cb2
commit
fdedc078f9
2 changed files with 42 additions and 0 deletions
20
lib/sidetiq/subclass_tracking.rb
Normal file
20
lib/sidetiq/subclass_tracking.rb
Normal file
|
@ -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
|
22
test/test_subclass_tracking.rb
Normal file
22
test/test_subclass_tracking.rb
Normal file
|
@ -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
|
||||
|
Loading…
Reference in a new issue