Class: Dragonfly::FunctionManager

Inherits:
Object
  • Object
show all
Defined in:
lib/dragonfly/function_manager.rb

Direct Known Subclasses

Analyser, Encoder, Generator, Processor

Defined Under Namespace

Classes: NotDefined, UnableToHandle

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFunctionManager

Returns a new instance of FunctionManager.



10
11
12
13
# File 'lib/dragonfly/function_manager.rb', line 10

def initialize
  @functions = {}
  @objects = []
end

Instance Attribute Details

#functionsObject (readonly)

Returns the value of attribute functions



20
21
22
# File 'lib/dragonfly/function_manager.rb', line 20

def functions
  @functions
end

#objectsObject (readonly)

Returns the value of attribute objects



20
21
22
# File 'lib/dragonfly/function_manager.rb', line 20

def objects
  @objects
end

Instance Method Details

#add(name, callable_obj = nil, &block) ⇒ Object



15
16
17
18
# File 'lib/dragonfly/function_manager.rb', line 15

def add(name, callable_obj=nil, &block)
  functions[name] ||= []
  functions[name] << (callable_obj || block)
end

#call_last(meth, *args) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/dragonfly/function_manager.rb', line 33

def call_last(meth, *args)
  if functions[meth.to_sym]
    functions[meth.to_sym].reverse.each do |function|
      catch :unable_to_handle do
        return function.call(*args)
      end
    end
    # If the code gets here, then none of the registered functions were able to handle the method call
    raise UnableToHandle, "None of the functions registered with #{self} were able to deal with the method call " +
      "#{meth}(#{args.map{|a| a.inspect[0..100]}.join(',')}). You may need to register one that can."
  else
    raise NotDefined, "function #{meth} not registered with #{self}"
  end
end

#get_registered(klass) ⇒ Object



48
49
50
# File 'lib/dragonfly/function_manager.rb', line 48

def get_registered(klass)
  objects.reverse.detect{|o| o.instance_of?(klass) }
end

#inspectObject



52
53
54
# File 'lib/dragonfly/function_manager.rb', line 52

def inspect
  to_s.sub(/>$/, " with functions: #{functions.keys.map{|k| k.to_s }.sort.join(', ')} >")
end

#register(klass, *args, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/dragonfly/function_manager.rb', line 22

def register(klass, *args, &block)
  obj = klass.new(*args)
  obj.configure(&block) if block
  obj.use_same_log_as(self) if obj.is_a?(Loggable)
  methods_to_add(obj).each do |meth|
    add meth.to_sym, obj.method(meth)
  end
  objects << obj
  obj
end