Class: AutomateEm::Modules

Inherits:
Object
  • Object
show all
Defined in:
lib/automate-em/core/modules.rb

Constant Summary collapse

@@modules =

modules (dependency_id => module class)

{}
@@load_lock =
Mutex.new

Class Method Summary collapse

Class Method Details

.[](dep_id) ⇒ Object



6
7
8
9
10
# File 'lib/automate-em/core/modules.rb', line 6

def self.[] (dep_id)
	@@load_lock.synchronize {
		@@modules[dep_id]
	}
end

.lazy_load(dep) ⇒ Object



12
13
14
15
16
# File 'lib/automate-em/core/modules.rb', line 12

def self.lazy_load(dep)
	theClass = Modules[dep.id]
	theClass = Modules.load_module(dep) if theClass.nil?
	theClass
end

.load_module(dep) ⇒ Object



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
# File 'lib/automate-em/core/modules.rb', line 18

def self.load_module(dep)
	begin
		found = false
		file = "#{dep.classname.underscore}.rb"
		
		Rails.configuration.automate.module_paths.each do |path|
			if File.exists?("#{path}/#{file}")
				@@load_lock.synchronize {
					load "#{path}/#{file}"
				}
				found = true
				break
			end
		end
		
		if not found
			raise "File not found! (#{file})"
		end
		
		@@load_lock.synchronize {
			@@modules[dep.id] = dep.classname.constantize	# this is the return value
		}
	rescue Exception => e
		AutomateEm.print_error(System.logger, e, {
			:message => "device module #{dep.actual_name} error whilst loading.",
			:level => Logger::ERROR
		})
		
		return false
	end
end