Module: RubyExt::ClassLoader

Extended by:
Observable
Defined in:
lib/RubyExt/ClassLoader.rb

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Observable

add_observer, delete_observer, delete_observers, notify_observers, observers_count

Class Attribute Details

.error_on_defined_constantObject

Returns the value of attribute error_on_defined_constant.



9
10
11
# File 'lib/RubyExt/ClassLoader.rb', line 9

def error_on_defined_constant
  @error_on_defined_constant
end

Class Method Details

.load_class(namespace, const, reload = false) ⇒ Object



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
# File 'lib/RubyExt/ClassLoader.rb', line 20

def load_class namespace, const, reload = false						
@monitor.synchronize do
	namespace = nil if namespace == Object or namespace == Module
	target_namespace = namespace
	
	# Name hack (for anonymous classes)
	namespace = eval "#{name_hack(namespace)}" if namespace
	
	class_name = namespace ? "#{namespace.name}::#{const}" : const
	simple_also_tried = false
	begin
		simple_also_tried = (namespace == nil)
		
		if try_load class_name, const
			defined_in_home_scope = namespace ? namespace.const_defined?(const) : \
			Object.const_defined?(const)
			
			raise_without_self NameError, "Class Name '#{class_name}' doesn't\
 correspond to File Name '#{Resource.class_to_virtual_file(class_name)}'!", ClassLoader \
			unless defined_in_home_scope
				
				unless reload
					if @loaded_classes.include? class_name
						if error_on_defined_constant	
							raise_without_self NameError,
						"Class '#{class_name}' is not defined in the '#{target_namespace}' Namespace!",
							ClassLoader
						else
							warn "Warn: Class '#{class_name}' is not defined in the '#{target_namespace}' Namespace!"
							puts caller
						end
					end
				end
				
				result = namespace ? namespace.const_get(const) : Object.const_get(const)
				
				@loaded_classes[class_name] = target_namespace
				notify_observers :update_class, result
				return result
			elsif namespace
				namespace = Module.namespace_for(namespace.name)
				class_name = namespace ? "#{namespace.name}::#{const}" : const
			end
		end until simple_also_tried
		
		raise_without_self NameError, "uninitialized constant '#{class_name}'!",
		ClassLoader
	end
end

.reload_class(class_name) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/RubyExt/ClassLoader.rb', line 11

def reload_class class_name
	@monitor.synchronize do
		class_name = class_name.sub(/^::/, "")
		namespace = Module.namespace_for(class_name);
		name = class_name.sub(/^#{namespace}::/, "")
		return load_class namespace, name, true
	end
end

.wrap_inside_namespace(namespace, script) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/RubyExt/ClassLoader.rb', line 70

def wrap_inside_namespace namespace, script
	nesting = []
	if namespace
		current_scope = ""
		namespace.name.split("::").each do |level|
			current_scope += "::#{level}"
			type = eval current_scope, TOPLEVEL_BINDING, __FILE__, __LINE__
			nesting << [level, (type.class == Module ? "module" : "class")]
		end
	end
	begining = nesting.collect{|l, t| "#{t} #{l};"}.join(' ')
	ending = nesting.collect{"end"}.join('; ')
	return "#{begining}#{script} \n#{ending}"
end