Class: RubyExt::FileSystemProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/RubyExt/Resource/file_system_provider.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directories = lambda{[File.expand_path('.')]}) ⇒ FileSystemProvider

Returns a new instance of FileSystemProvider.



5
6
7
# File 'lib/RubyExt/Resource/file_system_provider.rb', line 5

def initialize directories = lambda{[File.expand_path('.')]}
	@directories = directories
end

Class Attribute Details

.cacheObject (readonly)

Returns the value of attribute cache.



144
145
146
# File 'lib/RubyExt/Resource/file_system_provider.rb', line 144

def cache
  @cache
end

Instance Attribute Details

#directoriesObject

Returns the value of attribute directories.



3
4
5
# File 'lib/RubyExt/Resource/file_system_provider.rb', line 3

def directories
  @directories
end

Class Method Details

.remember_file(path) ⇒ Object



170
171
172
# File 'lib/RubyExt/Resource/file_system_provider.rb', line 170

def remember_file path			
	@files[path] = File.mtime(path)
end

.start_watching(interval = 2, directories = Dir.glob("**/lib")) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/RubyExt/Resource/file_system_provider.rb', line 146

def start_watching interval = 2, directories = Dir.glob("**/lib")
	stop_watching
	@watch_thread = Thread.new do					
		reset_changed_files directories
		while true
			sleep interval
			begin
				check_for_changed_files(directories).each do |type, klass, res|												
					Resource.notify_observers :update_resource, type, klass, res
				end							
			rescue Exception => e
				warn e
			end
		end
	end
end

.stop_watchingObject



163
164
165
166
167
168
# File 'lib/RubyExt/Resource/file_system_provider.rb', line 163

def stop_watching
	if @watch_thread
		@watch_thread.kill
		@watch_thread = nil
	end
end

Instance Method Details

#class_delete(class_name) ⇒ Object



44
45
46
47
# File 'lib/RubyExt/Resource/file_system_provider.rb', line 44

def class_delete class_name
	path = real_class_path class_name
	File.delete path if path
end

#class_exist?(class_name) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/RubyExt/Resource/file_system_provider.rb', line 40

def class_exist? class_name
	real_class_path(class_name) != nil
end

#class_get(class_name) ⇒ Object

Raises:



9
10
11
12
13
14
15
16
17
18
# File 'lib/RubyExt/Resource/file_system_provider.rb', line 9

def class_get class_name
	path = real_class_path class_name
	raise Resource::NotExist unless path
	
	if File.directory? path
		"module #{File.basename(path)}; end;"
	else
		File.read path
	end			
end

#class_set(class_name, data, directory_for_class_file = nil) ⇒ Object

It updates existing class, if class doesn’t exist it creates one in directory provided by filter



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/RubyExt/Resource/file_system_provider.rb', line 21

def class_set class_name, data, directory_for_class_file = nil
	directory_for_class_file ||= Dir.getwd
	directory_for_class_file = File.expand_path directory_for_class_file
	unless @directories.call.include? directory_for_class_file
		raise "Dirctories should include '#{directory_for_class_file}'!"
	end
	
	path = real_class_path class_name			
	unless path
		path = class_to_files(class_name).select{|f| f.include?(directory_for_class_file)}.first
		dir = path.sub(/\w+\.rb/, "")
		FileUtils.mkdir dir unless dir.empty? or File.exist? dir			
	end
	
	length = File.write(path, data)
	FileSystemProvider.remember_file path
	return length
end

#class_to_virtual_file(class_name) ⇒ Object

Different Providers can use different class path interpretation. So we return virtual path only if this path really exist.



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/RubyExt/Resource/file_system_provider.rb', line 127

def class_to_virtual_file class_name
	path = nil
	if FileSystemProvider.cache.include? class_name
		path = FileSystemProvider.cache[class_name]
	else
		path = real_class_path class_name								
		raise Resource::NotExist, "Class '#{class_name}' doesn't Exist!" unless path
		
		FileSystemProvider.cache[class_name] = path
	end						
	
	File.expand_path path
end

#resource_delete(class_name, resource_name) ⇒ Object



62
63
64
65
# File 'lib/RubyExt/Resource/file_system_provider.rb', line 62

def resource_delete class_name, resource_name
	path = real_resource_path class_name, resource_name
	File.delete path if path			
end

#resource_exist?(class_name, resource_name) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



88
89
90
91
92
93
# File 'lib/RubyExt/Resource/file_system_provider.rb', line 88

def resource_exist? class_name, resource_name
	class_path = real_class_path class_name
	raise Resource::NotExist unless class_path
	
	real_resource_path(class_name, resource_name) != nil
end

#resource_get(class_name, resource_name) ⇒ Object

First search for “./class_name.resource_name” files And then search for “./class_name.res/resource_name” files

Raises:



55
56
57
58
59
60
# File 'lib/RubyExt/Resource/file_system_provider.rb', line 55

def resource_get class_name, resource_name
	path = real_resource_path class_name, resource_name
	raise Resource::NotExist unless path
	
	File.read path
end

#resource_set(class_name, resource_name, data) ⇒ Object

It can only update existing resource, it can’t create a new one First search for the same resource and owerwrites it If such resource doesn’t exists writes to “./class_name.res/resource_name” file.

Raises:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/RubyExt/Resource/file_system_provider.rb', line 71

def resource_set class_name, resource_name, data
	class_path = real_class_path class_name
	raise Resource::NotExist unless class_path
	
	path = real_resource_path class_name, resource_name
	unless path
		class_path = class_path.sub(/\.rb$/, "")
		dir = "#{class_path}.res"				
		FileUtils.mkdir dir unless File.exist? dir
		path = "#{dir}/#{resource_name}"
	end
	
	length =  File.write path, data
	FileSystemProvider.remember_file path
	return length
end