Class: Context::Bridge

Inherits:
Object
  • Object
show all
Defined in:
lib/Context/Bridge.rb

Overview

This class is used to specify the namespace for a symbol according to a priority list. When you create the Bridge you initialize it either with a single module name, or an array of module names. Calling “bridge.symbol” will return the class in desired namespace (or nil if it doesn’t exist) If the Bridge was initialized with an array, it will pick the first namespace that evaluates the symbol into something that actually exists.

Instance Method Summary collapse

Constructor Details

#initialize(mods) ⇒ Bridge

Takes either a module name, or an array of module names



14
15
16
17
18
19
20
21
22
# File 'lib/Context/Bridge.rb', line 14

def initialize(mods)
    if mods.class != Array
  			@mod = mods
  			@mods = nil
  	    else
  	        @mods = mods
  	        @mod = nil
  	    end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol) ⇒ Object

Return the class specified by the stored module and the symbol If an array of modules was stored, walk through them and pick the first one that creates an extant class.



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/Context/Bridge.rb', line 50

def method_missing(symbol)
    if !@mod.nil?
  			@mod.class_eval(symbol.to_s)
  	    elsif !@mods.nil?
  	        mod = @mods.find do |mod|
  	            classExists?(mod, symbol)
  	        end
  	        evalClass(mod, symbol)
  	    else
  	        nil
  	    end
end

Instance Method Details

#classExists?(mod, symbol) ⇒ Boolean

Returns true if the mod and symbol evaluate to a class in the system.

Returns:

  • (Boolean)


43
44
45
# File 'lib/Context/Bridge.rb', line 43

def classExists?(mod, symbol)
 symbolToS(mod, symbol).eql?(evalClass(mod, symbol).to_s)
end

#evalClass(mod, symbol) ⇒ Object

Evaluate the module and symbol, returning the class. If it doesn’t exist, return nil



31
32
33
34
35
36
37
38
39
40
# File 'lib/Context/Bridge.rb', line 31

def evalClass(mod, symbol)
          retVal = nil
          if !mod.nil?
              begin
                  retVal = mod.class_eval(symbol.to_s)
              rescue
              end
  		end
          return retVal
end

#symbolToS(mod, symbol) ⇒ Object

Convert the module name and symbol to a string



25
26
27
# File 'lib/Context/Bridge.rb', line 25

def symbolToS(mod, symbol)
    mod.to_s + "::" + symbol.to_s
end