Class: Tins::Deflect::DeflectorCollection
Overview
This class implements a collection of deflectors, to make them available by emulating Ruby's message dispatch.
Instance Method Summary collapse
-
#add(klass, id, deflector) ⇒ Object
Add a new deflector deflector for class klass and method name id, and return self.
-
#delete(klass, id) ⇒ Object
Delete the deflecotor class klass and method name id.
-
#find(klass, id) ⇒ Object
Try to find a deflector for class klass and method id and return it.
-
#initialize ⇒ DeflectorCollection
constructor
A new instance of DeflectorCollection.
-
#member?(klass, id) ⇒ Boolean
Return true if messages are deflected for class klass and method name id, otherwise return false.
Constructor Details
#initialize ⇒ DeflectorCollection
Returns a new instance of DeflectorCollection.
630 631 632 |
# File 'lib/tins/dslkit.rb', line 630 def initialize @classes = {} end |
Instance Method Details
#add(klass, id, deflector) ⇒ Object
Add a new deflector deflector for class klass and method name id, and return self.
637 638 639 640 641 642 |
# File 'lib/tins/dslkit.rb', line 637 def add(klass, id, deflector) k = @classes[klass] k = @classes[klass] = {} unless k k[id.to_s] = deflector self end |
#delete(klass, id) ⇒ Object
Delete the deflecotor class klass and method name id. Returns the deflector if any was found, otherwise returns true.
652 653 654 655 656 657 658 |
# File 'lib/tins/dslkit.rb', line 652 def delete(klass, id) if k = @classes[klass] d = k.delete id.to_s @classes.delete klass if k.empty? d end end |
#find(klass, id) ⇒ Object
Try to find a deflector for class klass and method id and return it. If none was found, return nil instead.
662 663 664 665 666 667 668 |
# File 'lib/tins/dslkit.rb', line 662 def find(klass, id) klass.ancestors.find do |k| if d = @classes[k] and d = d[id.to_s] return d end end end |
#member?(klass, id) ⇒ Boolean
Return true if messages are deflected for class klass and method name id, otherwise return false.
646 647 648 |
# File 'lib/tins/dslkit.rb', line 646 def member?(klass, id) !!(k = @classes[klass] and k.key?(id.to_s)) end |