Class: Dragonfly::FunctionManager

Inherits:
Object
  • Object
show all
Includes:
Configurable, Loggable
Defined in:
lib/dragonfly/function_manager.rb

Direct Known Subclasses

Analyser, Encoder, Generator, Processor

Defined Under Namespace

Classes: NotDefined, UnableToHandle

Instance Attribute Summary (collapse)

Attributes included from Loggable

#log_object

Instance Method Summary (collapse)

Methods included from Configurable

included

Methods included from Loggable

#log, #log=, #use_same_log_as

Constructor Details

- (FunctionManager) initialize

Returns a new instance of FunctionManager



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

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

Instance Attribute Details

- (Object) functions (readonly)

Returns the value of attribute functions



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

def functions
  @functions
end

- (Object) objects (readonly)

Returns the value of attribute objects



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

def objects
  @objects
end

Instance Method Details

- (Object) add(name, callable_obj = nil, &block)



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

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

- (Object) call_last(meth, *args)



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

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

- (Object) get_registered(klass)



50
51
52
# File 'lib/dragonfly/function_manager.rb', line 50

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

- (Object) inspect



54
55
56
# File 'lib/dragonfly/function_manager.rb', line 54

def inspect
  "<#{self.class.name} with functions: #{functions.keys.map{|k| k.to_s }.sort.join(', ')} >"
end

- (Object) register(klass, *args, &block)



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

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)
  obj.use_as_fallback_config(self) if obj.is_a?(Configurable)
  methods_to_add(obj).each do |meth|
    add meth, obj.method(meth)
  end
  objects << obj
  obj
end