easyload 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,4 +1,5 @@
1
1
  *.gem
2
+ *.rbc
2
3
  .bundle
3
4
  .yardoc
4
5
  Gemfile.lock
@@ -0,0 +1,14 @@
1
+ # -*- encoding: utf-8 -*-
2
+ module Easyload
3
+ module Helpers
4
+ # Looks for a file on the $LOAD_PATH and returns the full path to the first match.
5
+ def self.find_loadable_file(path)
6
+ $LOAD_PATH.each do |load_root|
7
+ full_load_path = File.join(load_root, path)
8
+ return full_load_path if File.exists? full_load_path
9
+ end
10
+
11
+ return nil
12
+ end
13
+ end
14
+ end
@@ -1,5 +1,10 @@
1
1
  # -*- encoding: utf-8 -*-
2
+ require 'easyload/helpers'
3
+ require 'monitor'
4
+
2
5
  module Easyload
6
+ LOAD_LOCK = Monitor.new
7
+
3
8
  # The singleton methods that are defined for a module that includes {Easyload}.
4
9
  module SingletonExtensions
5
10
  # The root path that easyload should use when loading a child constant for this module.
@@ -39,34 +44,58 @@ module Easyload
39
44
  path_component = self.easyload_path_component_for_sym(sym)
40
45
  easyload_path = File.join(@easyload_root, "#{path_component}.rb")
41
46
 
42
- # Search for the file to include
43
- $LOAD_PATH.each do |load_root|
44
- full_load_path = File.join(load_root, easyload_path)
45
- if File.exists? full_load_path
46
- self.module_eval(File.read(full_load_path))
47
-
48
- # Did we get our target constant?
49
- if self.const_defined? sym
50
- target_const = self.const_get(sym)
51
- class << target_const
52
- include SingletonExtensions
53
- end
54
-
55
- target_const.easyload_from(File.join(self.easyload_root, path_component))
56
-
57
- return self.const_get(sym)
58
-
59
- # Warn but still break the load process. We don't want to support ambiguous load
60
- # paths.
61
- else
62
- $stderr.puts "Attempted to easyload #{sym} from '#{full_load_path}', but it doesn't appear to exist in that source file."
63
- end
64
-
65
- break
66
- end
47
+ # Do we have a file to easyload?
48
+ path_to_easyload = Helpers.find_loadable_file(easyload_path)
49
+ if path_to_easyload.nil?
50
+ raise NameError.new("Unknown constant #{self}::#{sym} - tried to easyload it from '#{@easyload_root}/#{path_component}'", sym)
67
51
  end
52
+ file_source = File.read(path_to_easyload)
68
53
 
69
- raise NameError("Unknown constant #{self}::#{sym} - tried to easyload it from '#{@easyload_root}/#{path_component}'", sym)
54
+ # Perform the easyload.
55
+ LOAD_LOCK.synchronize do
56
+ $parent = self
57
+ self.module_eval(file_source)
58
+ end
59
+
60
+ #puts "===="
61
+ #puts self
62
+ #puts "===="
63
+
64
+ # Perform the easyload with a manual eval. module_eval doesn't allow for relative constants
65
+ # in the easyloaded file, so we need to manually construct the context to evaluate within.
66
+ #context_type = self.class == Class ? 'class' : 'module'
67
+ #begin
68
+ # ROOT_BINDING.eval("#{context_type} #{self.name}; #{file_source}; end")
69
+ #rescue
70
+ # puts "#{context_type} #{self.name}; #{file_source}; end"
71
+ # raise
72
+ #end
73
+
74
+ #::Kernel.eval("#{context_type} #{self.name}; #{file_source}; end")
75
+ #puts "----"
76
+ #easyload_binding = self.module_eval('::Kernel.binding')
77
+ #easyload_binding.eval(file_source, path_to_easyload)
78
+
79
+ #self.instance_eval do
80
+ # ::Kernel.eval(file_source, nil, path_to_easyload)
81
+ #end
82
+
83
+ #global_eval("#{context_type} #{self.name}; #{file_source}; end")
84
+
85
+ # Did we get our target constant?
86
+ if not self.const_defined? sym
87
+ raise LoadError.new("Attempted to easyload #{sym} from '#{path_to_easyload}', but it doesn't appear to exist in that source file.")
88
+ end
89
+
90
+ # Make sure that we propagate easyload behaviors
91
+ target_const = self.const_get(sym)
92
+ class << target_const
93
+ include SingletonExtensions
94
+ end
95
+
96
+ target_const.easyload_from(File.join(self.easyload_root, path_component))
97
+
98
+ return self.const_get(sym)
70
99
  end
71
100
  end
72
101
  end
@@ -1,4 +1,4 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  module Easyload
3
- VERSION = '0.1.1'
3
+ VERSION = '0.2.0'
4
4
  end
data/spec/naming_spec.rb CHANGED
@@ -1,10 +1,6 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  require 'easyload'
3
3
 
4
- module NamingExamples
5
- Easyload
6
- end
7
-
8
4
  describe 'Easyload file names' do
9
5
  it 'should use a single lowercase name for a single word constant' do
10
6
  Examples.easyload_path_component_for_sym(:Single).should == 'single'
@@ -0,0 +1,4 @@
1
+ # -*- encoding: utf-8 -*-
2
+ class Bar
3
+
4
+ end
@@ -0,0 +1,4 @@
1
+ # -*- encoding: utf-8 -*-
2
+ module Foo
3
+ REFERS_TO = $parent::Bar
4
+ end
@@ -0,0 +1,12 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'easyload'
3
+
4
+ module RelativeConstants
5
+ include Easyload
6
+ end
7
+
8
+ describe 'Easyloaded modules with relatively referenced constants' do
9
+ it 'should be able to reference relative constants via $parent' do
10
+ RelativeConstants::Foo::REFERS_TO.should == RelativeConstants::Bar
11
+ end
12
+ end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- - 1
9
- version: 0.1.1
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ian MacLeod
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-02-22 00:00:00 -08:00
17
+ date: 2011-02-24 00:00:00 -08:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -79,6 +79,7 @@ files:
79
79
  - Rakefile
80
80
  - easyload.gemspec
81
81
  - lib/easyload.rb
82
+ - lib/easyload/helpers.rb
82
83
  - lib/easyload/singleton_extensions.rb
83
84
  - lib/easyload/version.rb
84
85
  - spec/basic_spec.rb
@@ -89,6 +90,9 @@ files:
89
90
  - spec/examples/nested/leaf.rb
90
91
  - spec/examples/node.rb
91
92
  - spec/naming_spec.rb
93
+ - spec/relative_constants/bar.rb
94
+ - spec/relative_constants/foo.rb
95
+ - spec/relative_constants_spec.rb
92
96
  has_rdoc: true
93
97
  homepage: ""
94
98
  licenses: []
@@ -130,3 +134,6 @@ test_files:
130
134
  - spec/examples/nested/leaf.rb
131
135
  - spec/examples/node.rb
132
136
  - spec/naming_spec.rb
137
+ - spec/relative_constants/bar.rb
138
+ - spec/relative_constants/foo.rb
139
+ - spec/relative_constants_spec.rb