Class: Spade::Exports

Inherits:
Object
  • Object
show all
Defined in:
lib/spade/exports.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ctx) ⇒ Exports

Returns a new instance of Exports.



24
25
26
# File 'lib/spade/exports.rb', line 24

def initialize(ctx)
  @context = ctx
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



22
23
24
# File 'lib/spade/exports.rb', line 22

def context
  @context
end

Instance Method Details

#[](name) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/spade/exports.rb', line 28

def [](name)
  
  begin
    if self.class.const_defined?(name)
      ret = self.class.const_get(name)
      
      # If we are returning a class, create a custom subclass the first 
      # time that also exposes the current context.
      if ret.instance_of? Class
        @klass_cache ||= {}
        unless @klass_cache[name]

          proc1 = proc { @context } 
          @klass_cache[name] = Class.new(ret) do
            
            @context = proc1.call

            def self.context
              @context
            end
            
          end

        end
        
        @klass_cache[name]
      else
        ret
      end
        
    else
      yield
    end
    
  rescue NameError => e
    yield
  end
end