Class: Tins::Deflect::DeflectorCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/tins/dslkit.rb

Overview

This class implements a collection of deflectors, to make them available by emulating Ruby’s message dispatch.

Instance Method Summary collapse

Constructor Details

#initializeDeflectorCollection

Returns a new instance of DeflectorCollection.



335
336
337
# File 'lib/tins/dslkit.rb', line 335

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.



342
343
344
345
346
347
# File 'lib/tins/dslkit.rb', line 342

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.



357
358
359
360
361
362
363
# File 'lib/tins/dslkit.rb', line 357

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.



367
368
369
370
371
372
373
# File 'lib/tins/dslkit.rb', line 367

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.

Returns:

  • (Boolean)


351
352
353
# File 'lib/tins/dslkit.rb', line 351

def member?(klass, id)
  !!(k = @classes[klass] and k.key?(id.to_s))
end