Class: VResource::FileSystemProvider

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directories = File.expand_path('.')) ⇒ FileSystemProvider

Returns a new instance of FileSystemProvider.



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

def initialize directories = File.expand_path('.')
  @directories = Array(directories).collect{|path| File.expand_path path}
end

Instance Attribute Details

#directoriesObject

Returns the value of attribute directories.



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

def directories
  @directories
end

Instance Method Details

#class_delete(class_name) ⇒ Object



44
45
46
47
# File 'lib/vresource/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/vresource/file_system_provider.rb', line 40

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

#class_get(class_name) ⇒ Object



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

def class_get class_name
  path = ensure_exist!(real_class_path(class_name))
  
  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



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

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.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
  
  reloader.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.



124
125
126
127
# File 'lib/vresource/file_system_provider.rb', line 124

def class_to_virtual_file class_name
  path = ensure_exist!(real_class_path(class_name), "Class '#{class_name}' doesn't Exist!")      
  File.expand_path path
end

#reloaderObject



130
131
132
# File 'lib/vresource/file_system_provider.rb', line 130

def reloader
  @reloader ||= FileSystemReloader.new directories
end

#resource_delete(class_name, resource_name) ⇒ Object



61
62
63
64
# File 'lib/vresource/file_system_provider.rb', line 61

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)


86
87
88
89
90
# File 'lib/vresource/file_system_provider.rb', line 86

def resource_exist? class_name, resource_name
  class_path = ensure_exist!(real_class_path(class_name))
  
  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



55
56
57
58
59
# File 'lib/vresource/file_system_provider.rb', line 55

def resource_get class_name, resource_name
  path = ensure_exist!(real_resource_path(class_name, resource_name))
  
  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.



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

def resource_set class_name, resource_name, data
  class_path = ensure_exist!(real_class_path(class_name))
  
  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
  reloader.remember_file path
  return length
end