class_loader 3.0.6 → 3.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,7 +5,7 @@ module ClassLoader
5
5
  @loaded_classes, @after_callbacks = {}, {}
6
6
  @monitor = Monitor.new
7
7
  class << self
8
- attr_reader :loaded_classes, :monitor
8
+ attr_reader :loaded_classes, :monitor, :after_callbacks
9
9
 
10
10
  # Hierarchically searching for class file, according to modules hierarchy.
11
11
  #
@@ -113,6 +113,14 @@ module ClassLoader
113
113
 
114
114
  def after class_name, &block
115
115
  (@after_callbacks[class_name] ||= []) << block
116
+
117
+ # Firing it immediatelly if class has been already loaded.
118
+ # Checking if constant exist without loading it.
119
+ names = class_name.gsub(/^::/, '').split('::')
120
+ const_defined = names.inject Object do |current, const|
121
+ current = current && current.const_defined?(const) && current.const_get(const)
122
+ end
123
+ block.call if const_defined
116
124
  end
117
125
 
118
126
  def filter_backtrace backtrace
@@ -120,8 +128,6 @@ module ClassLoader
120
128
  end
121
129
 
122
130
  protected
123
- attr_reader :after_callbacks
124
-
125
131
  # Use this method to define class name to file name mapping, by default it uses underscored paths,
126
132
  # but You can override this method to use camel case for example.
127
133
  def get_file_name class_name
@@ -0,0 +1,15 @@
1
+ require 'rspec_ext'
2
+ require "class_loader"
3
+
4
+ describe 'Autoloading classes' do
5
+ it do
6
+ with_load_path "/Users/alex/projects/class_loader/spec/class_loader_spec/after" do
7
+ exp = mock
8
+ exp.should_receive :callback_fired
9
+ ClassLoader.after 'SomeClass' do |klass|
10
+ exp.callback_fired
11
+ end
12
+ SomeClass
13
+ end
14
+ end
15
+ end
@@ -8,6 +8,7 @@ describe 'Autoloading classes' do
8
8
  remove_constants :SomeNamespace, :SomeClass, :AnotherClass, :Tmp
9
9
 
10
10
  ClassLoader.loaded_classes.clear
11
+ ClassLoader.after_callbacks.clear
11
12
  ClassLoader.watcher.stop
12
13
  end
13
14
 
@@ -97,12 +98,18 @@ describe 'Autoloading classes' do
97
98
 
98
99
  it "after" do
99
100
  with_load_path "#{spec_dir}/after" do
101
+ exp = mock
102
+ exp.should_receive :callback_fired
100
103
  ClassLoader.after 'SomeClass' do |klass|
101
- klass.class_eval do
102
- def self.extension; :ok end
103
- end
104
+ exp.callback_fired
105
+ end
106
+ SomeClass
107
+
108
+ # Should fire immediatelly if class has been already loaded.
109
+ exp.should_receive :fired_immediatelly
110
+ ClassLoader.after 'SomeClass' do |klass|
111
+ exp.fired_immediatelly
104
112
  end
105
- SomeClass.extension.should == :ok
106
113
  end
107
114
  end
108
115
 
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.6
4
+ version: 3.0.7
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/another_spec.rb
26
27
  - spec/class_loader_spec/after/some_class.rb
27
28
  - spec/class_loader_spec/anonymous_class/some_namespace/some_class.rb
28
29
  - spec/class_loader_spec/another_namespace/some_namespace/namespace_a.rb
@@ -41,7 +42,6 @@ files:
41
42
  - spec/class_loader_spec/preloading/some_namespace/another_class.rb
42
43
  - spec/class_loader_spec/preloading/some_namespace.rb
43
44
  - spec/class_loader_spec/reloading/some_class.rb
44
- - spec/class_loader_spec/underscored/underscored_namespace/underscored_class.rb
45
45
  - spec/class_loader_spec.rb
46
46
  homepage: http://github.com/alexeypetrushin/class_loader
47
47
  licenses: []
@@ -1,2 +0,0 @@
1
- class UnderscoredClass
2
- end