Class: Context::Bridge
- Inherits:
-
Object
- Object
- Context::Bridge
- 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
-
#classExists?(mod, symbol) ⇒ Boolean
Returns true if the mod and symbol evaluate to a class in the system.
-
#evalClass(mod, symbol) ⇒ Object
Evaluate the module and symbol, returning the class.
-
#initialize(mods) ⇒ Bridge
constructor
Takes either a module name, or an array of module names.
-
#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.
-
#symbolToS(mod, symbol) ⇒ Object
Convert the module name and symbol to a string.
Constructor Details
#initialize(mods) ⇒ Bridge
Takes either a module name, or an array of module names
15 16 17 18 19 20 21 22 23 |
# File 'lib/Context/Bridge.rb', line 15 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.
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/Context/Bridge.rb', line 51 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.
44 45 46 |
# File 'lib/Context/Bridge.rb', line 44 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
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/Context/Bridge.rb', line 32 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
26 27 28 |
# File 'lib/Context/Bridge.rb', line 26 def symbolToS(mod, symbol) mod.to_s + "::" + symbol.to_s end |