class_loader 3.0.4 → 3.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,11 +1,10 @@
1
1
  require 'rake_ext'
2
2
 
3
- project(
3
+ project \
4
4
  name: "class_loader",
5
5
  gem: true,
6
- summary: "Automatically find, load and reload classes",
6
+ summary: "Automatically finds, loads and reloads classes",
7
7
  # version: '3.0.0',
8
8
 
9
9
  author: "Alexey Petrushin",
10
- homepage: "http://github.com/alexeypetrushin/class_loader"
11
- )
10
+ homepage: "http://github.com/alexeypetrushin/class_loader"
@@ -2,7 +2,7 @@ require 'class_loader/support'
2
2
  require 'monitor'
3
3
 
4
4
  module ClassLoader
5
- @loaded_classes = {}
5
+ @loaded_classes, @after_callbacks = {}, {}
6
6
  @monitor = Monitor.new
7
7
  class << self
8
8
  attr_reader :loaded_classes, :monitor
@@ -58,6 +58,9 @@ module ClassLoader
58
58
  # Getting the class itself.
59
59
  klass = namespace ? namespace.const_get(const, false) : Object.const_get(const, false)
60
60
 
61
+ # Firing after callbacks.
62
+ if callbacks = after_callbacks[klass.name] then callbacks.each{|c| c.call klass} end
63
+
61
64
  loaded_classes[class_name] = klass
62
65
 
63
66
  return klass
@@ -86,7 +89,7 @@ module ClassLoader
86
89
 
87
90
  # Watch and reload files.
88
91
  def watch *paths
89
- paths.each{|path| watcher.paths << path}
92
+ paths.each{|path| watcher.paths << path unless watcher.paths.include? path}
90
93
  watcher.start
91
94
  end
92
95
 
@@ -95,11 +98,17 @@ module ClassLoader
95
98
  @watcher ||= ClassLoader::Watcher.new monitor
96
99
  end
97
100
 
101
+ def after class_name, &block
102
+ (@after_callbacks[class_name] ||= []) << block
103
+ end
104
+
98
105
  def filter_backtrace backtrace
99
106
  backtrace.reject{|path| path.include?("/lib/class_loader") or path.include?('monitor.rb')}
100
107
  end
101
108
 
102
109
  protected
110
+ attr_reader :after_callbacks
111
+
103
112
  # Use this method to define class name to file name mapping, by default it uses underscored paths,
104
113
  # but You can override this method to use camel case for example.
105
114
  def get_file_name class_name
data/readme.md CHANGED
@@ -1,4 +1,4 @@
1
- # ClassLoader automatically finds, loads and reloads classes
1
+ # Automatically finds, loads and reloads classes
2
2
 
3
3
  Suppose there's following directory structure:
4
4
 
@@ -17,7 +17,7 @@ SomeClass
17
17
  SomeNamespace::AnotherClass
18
18
  ```
19
19
 
20
- No need for `require` or `autoload`:
20
+ No need for `require`, `autoload` and code like this:
21
21
 
22
22
  ``` ruby
23
23
  require 'some_class'
@@ -31,18 +31,28 @@ module SomeNamespace
31
31
  end
32
32
  ```
33
33
 
34
- You can also tell it to watch and reload changes:
34
+ Watching and reloading changed classes:
35
35
 
36
36
  ``` ruby
37
37
  ClassLoader.watch 'my_app/lib'
38
38
  ```
39
39
 
40
- Or preload classes eagerly:
40
+ Preload classes eagerly:
41
41
 
42
42
  ``` ruby
43
43
  ClassLoader.preload 'my_app/lib'
44
44
  ```
45
45
 
46
+ Execute callbacks after loading class:
47
+
48
+ ``` ruby
49
+ ClassLoader.after 'SomeClass' do |klass|
50
+ klass.include SomeModule
51
+ end
52
+ ```
53
+
54
+ It's also very small, sources are about 150 lines (and third of it are comments).
55
+
46
56
  ## Installation
47
57
 
48
58
  $ gem install class_loader
@@ -0,0 +1,2 @@
1
+ class SomeClass
2
+ end
@@ -5,28 +5,10 @@ describe 'Autoloading classes' do
5
5
  with_tmp_spec_dir
6
6
 
7
7
  after do
8
+ remove_constants :SomeNamespace, :SomeClass, :AnotherClass, :Tmp
9
+
8
10
  ClassLoader.loaded_classes.clear
9
11
  ClassLoader.watcher.stop
10
-
11
- remove_constants %w(
12
- SomeNamespace
13
- AnotherNamespace
14
-
15
- SomeClass
16
- AnotherClass
17
-
18
- BasicSpec
19
- OnlyOnceSpec
20
- NamespaceTypeResolving NamespaceIsAlreadyDefinedAsClass
21
- InvalidInfinityLoopClassName
22
- AnonymousSpec
23
- AnotherNamespace
24
- ClassReloadingSpec
25
- UnloadOldClass
26
- PreloadingSpec
27
- UnderscoredNamespace
28
- Tmp
29
- )
30
12
  end
31
13
 
32
14
  it "basics" do
@@ -106,6 +88,17 @@ describe 'Autoloading classes' do
106
88
  end
107
89
  end
108
90
 
91
+ it "after" do
92
+ with_load_path "#{spec_dir}/after" do
93
+ ClassLoader.after 'SomeClass' do |klass|
94
+ klass.class_eval do
95
+ def self.extension; :ok end
96
+ end
97
+ end
98
+ SomeClass.extension.should == :ok
99
+ end
100
+ end
101
+
109
102
  it "should reload class files" do
110
103
  with_load_path "#{spec_dir}/reloading" do
111
104
  watcher = ClassLoader.watcher
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: class_loader
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.4
4
+ version: 3.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -23,6 +23,7 @@ files:
23
23
  - lib/class_loader/support.rb
24
24
  - lib/class_loader/watcher.rb
25
25
  - lib/class_loader.rb
26
+ - spec/class_loader_spec/after/some_class.rb
26
27
  - spec/class_loader_spec/anonymous_class/some_namespace/some_class.rb
27
28
  - spec/class_loader_spec/another_namespace/some_namespace/namespace_a.rb
28
29
  - spec/class_loader_spec/another_namespace/some_namespace/namespace_b.rb
@@ -40,7 +41,6 @@ files:
40
41
  - spec/class_loader_spec/preloading/some_namespace.rb
41
42
  - spec/class_loader_spec/reloading/some_class.rb
42
43
  - spec/class_loader_spec/underscored/underscored_namespace/underscored_class.rb
43
- - spec/class_loader_spec/unload_old_class/UnloadOldClass.rb
44
44
  - spec/class_loader_spec.rb
45
45
  homepage: http://github.com/alexeypetrushin/class_loader
46
46
  licenses: []
@@ -65,5 +65,5 @@ rubyforge_project:
65
65
  rubygems_version: 1.8.6
66
66
  signing_key:
67
67
  specification_version: 3
68
- summary: Automatically find, load and reload classes
68
+ summary: Automatically finds, loads and reloads classes
69
69
  test_files: []
@@ -1,2 +0,0 @@
1
- class UnloadOldClass
2
- end