Class: ClassLoader::FileSystemAdapter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(class_name_translator) ⇒ FileSystemAdapter

Returns a new instance of FileSystemAdapter.



5
6
7
8
9
# File 'lib/class_loader/file_system_adapter.rb', line 5

def initialize class_name_translator
  @translator = class_name_translator
  @paths, @watched_paths, @file_name_cache = [], [], {}
  @watched_files, @first_check = {}, true
end

Instance Attribute Details

#translatorObject (readonly)

Returns the value of attribute translator.



3
4
5
# File 'lib/class_loader/file_system_adapter.rb', line 3

def translator
  @translator
end

Instance Method Details

#add_path(path, watch = false) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/class_loader/file_system_adapter.rb', line 67

def add_path path, watch = false      
  path = File.expand_path(path)
  raise "#{path} already added!" if paths.include? path
  
  paths << path
  watched_paths << path if watch
end

#clearObject



75
76
77
78
# File 'lib/class_loader/file_system_adapter.rb', line 75

def clear
  @paths, @watched_paths, @file_name_cache = [], [], {}
  @watched_files, @first_check = {}, true
end

#each_changed_class(&block) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/class_loader/file_system_adapter.rb', line 80

def each_changed_class &block
  if @first_check
    each_watched_file{|base_path, file_path| remember_file file_path}
    @first_check = false
  else
    each_watched_file do |base_path, file_path|
      if file_changed? file_path
        remember_file file_path
        
        normalized_path = file_path.sub(/\.rb$/, "")
        block.call _to_class_name(normalized_path, base_path)
      end
    end
  end      
end

#each_class(&block) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/class_loader/file_system_adapter.rb', line 96

def each_class &block
  @paths.each do |base_path|          
    Dir.glob("#{base_path}/**/*.rb").each do |file_path|
      normalized_path = file_path.sub(/\.rb$/, "")
      block.call _to_class_name(normalized_path, base_path)
    end
  end
end

#exist?(class_name) ⇒ Boolean Also known as: exists?

Returns:

  • (Boolean)


11
12
13
# File 'lib/class_loader/file_system_adapter.rb', line 11

def exist? class_name
  !!to_file_path(class_name)
end

#read(class_name) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/class_loader/file_system_adapter.rb', line 16

def read class_name      
  file_path = to_file_path class_name
  return nil unless file_path
  
  if file_path =~ /\.rb$/
    File.open(file_path){|f| f.read}        
  else
    "module #{class_name}; end;"
  end  
end

#to_class_name(normalized_path) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/class_loader/file_system_adapter.rb', line 56

def to_class_name normalized_path
  raise "Internal error, file_name should be absolute path (#{normalized_path})!" unless normalized_path =~ /^\//  
  raise "Internal error, file_name should be without .rb suffix (#{normalized_path})!" if normalized_path =~ /\.rb$/  
  
  if base_path = paths.find{|path| normalized_path.start_with? path}
    _to_class_name normalized_path, base_path
  else
    nil
  end
end

#to_file_path(class_name) ⇒ Object



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
# File 'lib/class_loader/file_system_adapter.rb', line 27

def to_file_path class_name
  file_path, exist = @file_name_cache[class_name] || []
  unless exist
    file_name = translator.to_file_path class_name
    file_path = catch :found do
      # files
      paths.each do |base|
        try = "#{base}/#{file_name}.rb"
        if File.exist? try
          throw :found, try
        end
      end
      
      # dirs
      paths.each do |base|
        try = "#{base}/#{file_name}"
        if File.exist? try
          throw :found, try
        end
      end
      
      nil
    end
    
    @file_name_cache[class_name] = [file_path, true]
  end
  file_path
end