Class: LanGrove::ClassLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/langrove/ext/class_loader.rb

Class Method Summary collapse

Class Method Details

.create(class_config, logger = nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/langrove/ext/class_loader.rb', line 9

def self.create( class_config, logger = nil )
  
  logger.info( "LOAD: #{class_config.inspect}" ) unless logger.nil?
  
  raise ClassLoaderException.new( 
  
    "class_config requires :module" 
    
  ) unless class_config.has_key? :module
  
  raise ClassLoaderException.new( 
  
    "class_config requires :class" 
    
  ) unless class_config.has_key? :class
  
  
  module_name = class_config[:module]
  class_name  = class_config[:class]
  
  
  #
  # First try local implementation root
  #
  
  exception = nil
  
  location = nil
  
  begin
    
    location = "#{module_name.underscore}/#{class_name.underscore}"
    
    eval "require '#{location}'"
    
    return Object.const_get( module_name ).const_get( class_name )
    
  rescue LoadError => e
    
    exception = e
    
  end
  
  #
  # Fall back to langrove gem lib
  #
  
  begin
    
    path = File.expand_path('../../',  __FILE__)
    
    location = "#{path}/#{module_name.underscore}/#{class_name.underscore}"
    
    begin
    
      eval "require '#{location}'"
      
    rescue; end
    
    return LanGrove.const_get( module_name ).const_get( class_name )
    
  rescue Exception => e
    
    #
    # Raise from the original exception to
    # inform the local implementation
    # it is missing a module/class .rb
    # "no such file 'module/class"
    #
    # And not confuse the issue by raising
    # "no such file 'langrove/module/class"
    #
    logger.error "FAIL: #{exception.message}.rb" unless logger.nil?
    
    raise ClassLoaderException.new "#{exception.message}.rb"
    
  end
  
end