Class: Context::Bridge

Inherits:
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



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

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.



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

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)


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

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



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

def evalClass(mod, symbol)
    begin
  		    mod.unless_nil do class_eval(symbol.to_s) end
  		rescue
  		    nil
  		end
end

#symbolToS(mod, symbol) ⇒ Object

Convert the module name and symbol to a string



27
28
29
# File 'lib/Context/Bridge.rb', line 27

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