Class: Object

Inherits:
BasicObject
Defined in:
lib/absorb.rb

Overview

Pickup – The Object extension that gives us pickups

Constant Summary collapse

@@local_extensions =
[]

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/absorb.rb', line 5

def method_missing(name, *args, &block)
    klass = self
    @@local_extensions.each { |mod|
        if mod.instance_methods.include? name
            return mod
                .instance_method(name)
                .bind(klass)
                .call(*args, &block)
        end
    }

    super
end

Instance Method Details

#absorb(name, &block) ⇒ Object

Absorb lets you locally extend a class with a module

Example:

>> Array.absorb(MyArrayExtension) do

>> [1,2,3].nowHasThisMethodFromModule #=> Works! >> end >> [1,2,3].nowHasThisMethodFromModule #=> NoMethodError

Arguments:

module_name: (String)
block: (Block)


47
48
49
50
51
52
# File 'lib/absorb.rb', line 47

def absorb(name, &block)
    @@local_extensions.push(name)
    result = block.call
    @@local_extensions.pop
    result
end

#respond_to?(method_name, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/absorb.rb', line 19

def respond_to?(method_name, include_all=false)        
    klass = self
    result = super method_name, include_all

    if result then return result end
        
    @@local_extensions.each { |mod|
        if mod.instance_methods.include? method_name
            result = true
            return result
        end
    }

    result
end