wrap_in_module 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,96 +13,53 @@ module WrapInModule
13
13
  #
14
14
  # See intro.txt[link:files/intro_txt.html] for an overview.
15
15
 
16
- class Script < Module
17
- # The file with which the Script was instantiated.
18
- attr_reader :__main_file
19
-
20
- # The directory in which main_file is located, and relative to which
21
- # #load searches for files before falling back to Kernel#load.
22
- attr_reader :__dir
23
-
24
- # A hash that maps <tt>filename=>true</tt> for each file that has been
25
- # required locally by the script. This has the same semantics as <tt>$"</tt>,
26
- # alias <tt>$LOADED_FEATURES</tt>, except that it is local to this script.
27
- attr_reader :__loaded_features
28
-
29
- class << self
30
- alias load new
31
- end
32
-
16
+ class Script
33
17
  # Creates new Script, and loads _main_file_ in the scope of the Script. If a
34
18
  # block is given, the script is passed to it before loading from the file, and
35
19
  # constants can be defined as inputs to the script.
36
20
 
37
- def initialize(main_file) # :yields: self
38
- extend ScriptModuleMethods
39
- @__main_file = File.expand_path(main_file)
40
- @__dir = File.dirname(@__main_file)
41
- @__loaded_features = {}
21
+ def initialize(_module, main_file) # :yields: self
22
+ @__module = _module
23
+ @__module.extend LoadInModuleMethods
24
+ @__module.extend ScriptModuleMethods
42
25
 
43
26
  yield self if block_given?
44
- load_in_module(main_file)
45
- end
46
-
47
- # Loads _file_ into this Script. Searches relative to the local dir, that is,
48
- # the dir of the file given in the original call to
49
- # <tt>Script.load(file)</tt>, loads the file, if found, into this Script's
50
- # scope, and returns true. If the file is not found, falls back to
51
- # <tt>Kernel.load</tt>, which searches on <tt>$LOAD_PATH</tt>, loads the file,
52
- # if found, into global scope, and returns true. Otherwise, raises
53
- # <tt>LoadError</tt>.
54
- #
55
- # The _wrap_ argument is passed to <tt>Kernel.load</tt> in the fallback case,
56
- # when the file is not found locally.
57
- #
58
- # Typically called from within the main file to load additional sub files, or
59
- # from those sub files.
60
-
61
- def load(file, wrap = false)
62
- load_in_module(File.join(@__dir, file))
63
- true
64
- rescue MissingFile
65
- super
66
- end
67
27
 
68
- # Analogous to <tt>Kernel#require</tt>. First tries the local dir, then falls
69
- # back to <tt>Kernel#require</tt>. Will load a given _feature_ only once.
70
- #
71
- # Note that extensions (*.so, *.dll) can be required in the global scope, as
72
- # usual, but not in the local scope. (This is not much of a limitation in
73
- # practice--you wouldn't want to load an extension more than once.) This
74
- # implementation falls back to <tt>Kernel#require</tt> when the argument is an
75
- # extension or is not found locally.
76
-
77
- def require(feature)
78
- unless @__loaded_features[feature]
79
- @__loaded_features[feature] = true
80
- file = File.join(@__dir, feature)
81
- file += ".rb" unless /\.rb$/ =~ file
82
- load_in_module(file)
28
+ @__module.module_eval do
29
+ # The file with which the Script was instantiated.
30
+ attr_reader :__main_file
31
+
32
+ # The directory in which main_file is located, and relative to which
33
+ # #load searches for files before falling back to Kernel#load.
34
+ attr_reader :__dir
35
+
36
+ # A hash that maps <tt>filename=>true</tt> for each file that has been
37
+ # required locally by the script. This has the same semantics as <tt>$"</tt>,
38
+ # alias <tt>$LOADED_FEATURES</tt>, except that it is local to this script.
39
+ attr_reader :__loaded_features
40
+
41
+ @__main_file = File.expand_path(main_file)
42
+ @__dir = File.dirname(@__main_file)
43
+ @__loaded_features = {}
44
+ load_in_module(main_file)
83
45
  end
84
- rescue MissingFile
85
- @__loaded_features[feature] = false
86
- super
87
46
  end
88
-
89
- # Raised by #load_in_module, caught by #load and #require.
90
- class MissingFile < LoadError; end
91
-
92
- # Loads _file_ in this module's context. Note that <tt>\_\_FILE\_\_</tt> and
93
- # <tt>\_\_LINE\_\_</tt> work correctly in _file_.
94
- # Called by #load and #require; not normally called directly.
95
-
96
- def load_in_module(__file__)
97
- module_eval("@__script_scope ||= binding\n" + IO.read(__file__),
98
- File.expand_path(__file__), 0)
99
- # start numbering at 0 because of the extra line.
100
- # The extra line does nothing in sub-script files.
101
- rescue Errno::ENOENT
102
- if /#{__file__}$/ =~ $!.message # No extra locals in this scope.
103
- raise MissingFile, $!.message
104
- else
105
- raise
47
+
48
+ module LoadInModuleMethods
49
+ # Loads _file_ in this module's context. Note that <tt>\_\_FILE\_\_</tt> and
50
+ # <tt>\_\_LINE\_\_</tt> work correctly in _file_.
51
+ # Called by #load and #require; not normally called directly.
52
+ def load_in_module(__file__)
53
+ module_eval("@__script_scope ||= binding\n" + IO.read(__file__),
54
+ File.expand_path(__file__), 0)
55
+ # start numbering at 0 because of the extra line.
56
+ # The extra line does nothing in sub-script files.
57
+ rescue Errno::ENOENT
58
+ if /#{__file__}$/ =~ $!.message # No extra locals in this scope.
59
+ raise MissingFile, $!.message
60
+ else
61
+ raise
62
+ end
106
63
  end
107
64
  end
108
65
 
@@ -131,6 +88,51 @@ module WrapInModule
131
88
  def __local_variable_get(name)
132
89
  eval(name.to_s, __script_scope)
133
90
  end
91
+
92
+ # Raised by #load_in_module, caught by #load and #require.
93
+ class MissingFile < LoadError; end
94
+
95
+ # Loads _file_ into this Script. Searches relative to the local dir, that is,
96
+ # the dir of the file given in the original call to
97
+ # <tt>Script.load(file)</tt>, loads the file, if found, into this Script's
98
+ # scope, and returns true. If the file is not found, falls back to
99
+ # <tt>Kernel.load</tt>, which searches on <tt>$LOAD_PATH</tt>, loads the file,
100
+ # if found, into global scope, and returns true. Otherwise, raises
101
+ # <tt>LoadError</tt>.
102
+ #
103
+ # The _wrap_ argument is passed to <tt>Kernel.load</tt> in the fallback case,
104
+ # when the file is not found locally.
105
+ #
106
+ # Typically called from within the main file to load additional sub files, or
107
+ # from those sub files.
108
+
109
+ def load(file, wrap = false)
110
+ load_in_module(File.join(@__dir, file))
111
+ true
112
+ rescue MissingFile
113
+ super
114
+ end
115
+
116
+ # Analogous to <tt>Kernel#require</tt>. First tries the local dir, then falls
117
+ # back to <tt>Kernel#require</tt>. Will load a given _feature_ only once.
118
+ #
119
+ # Note that extensions (*.so, *.dll) can be required in the global scope, as
120
+ # usual, but not in the local scope. (This is not much of a limitation in
121
+ # practice--you wouldn't want to load an extension more than once.) This
122
+ # implementation falls back to <tt>Kernel#require</tt> when the argument is an
123
+ # extension or is not found locally.
124
+
125
+ def require(feature)
126
+ unless @__loaded_features[feature]
127
+ @__loaded_features[feature] = true
128
+ file = File.join(@__dir, feature)
129
+ file += ".rb" unless /\.rb$/ =~ file
130
+ load_in_module(file)
131
+ end
132
+ rescue MissingFile
133
+ @__loaded_features[feature] = false
134
+ super
135
+ end
134
136
  end
135
137
  end
136
138
  end
@@ -1,3 +1,3 @@
1
1
  module WrapInModule
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -0,0 +1,2 @@
1
+ class Hoopty
2
+ end
@@ -0,0 +1,7 @@
1
+ require 'single_top_level_class'
2
+
3
+ class Doopty
4
+ def test_hoopty
5
+ Hoopty.new
6
+ end
7
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe "WrapInModule" do
4
+ it "loads a file with a single top level class" do
5
+ module FooMod; end
6
+ WrapInModule::Script.new(FooMod, File.dirname(__FILE__) + '/../example_data/single_top_level_class.rb')
7
+ FooMod::Hoopty.new
8
+ end
9
+
10
+ it "loads a file without overwriting existing module definition" do
11
+ module FooMod; BOB="hello there I am bob"; end
12
+ WrapInModule::Script.new(FooMod, File.dirname(__FILE__) + '/../example_data/single_top_level_class.rb')
13
+ FooMod::BOB
14
+ end
15
+
16
+ it "loads a file with a top level class that requires another file with a top level class" do
17
+ module FooMod; end
18
+ WrapInModule::Script.new(FooMod, File.dirname(__FILE__) + '/../example_data/top_level_class_requires_another_file.rb')
19
+ FooMod::Doopty.new
20
+ FooMod::Hoopty.new
21
+ end
22
+
23
+ it "verify that classes required inside of the top level loaded file are accesible in the non namespaces scope" do
24
+ module FooMod; end
25
+ WrapInModule::Script.new(FooMod, File.dirname(__FILE__) + '/../example_data/top_level_class_requires_another_file.rb')
26
+ a = FooMod::Doopty.new
27
+ a.test_hoopty
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
2
+
3
+ require 'wrap_in_module'
@@ -14,4 +14,6 @@ Gem::Specification.new do |gem|
14
14
  gem.name = "wrap_in_module"
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = WrapInModule::VERSION
17
+
18
+ gem.add_development_dependency 'rspec'
17
19
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wrap_in_module
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,24 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-22 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2012-08-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
14
30
  description: Ruby gem that allows you to load a ruby file into a module. Think scoping
15
31
  bunch of code inside a module.
16
32
  email:
@@ -27,6 +43,10 @@ files:
27
43
  - Rakefile
28
44
  - lib/wrap_in_module.rb
29
45
  - lib/wrap_in_module/version.rb
46
+ - spec/example_data/single_top_level_class.rb
47
+ - spec/example_data/top_level_class_requires_another_file.rb
48
+ - spec/lib/wrap_in_module_spec.rb
49
+ - spec/spec_helper.rb
30
50
  - wrap_in_module.gemspec
31
51
  homepage: http://github.com/realpractice/wrap_in_module
32
52
  licenses: []
@@ -53,4 +73,8 @@ signing_key:
53
73
  specification_version: 3
54
74
  summary: Ruby gem that allows you to load a ruby file into a module. Think scoping
55
75
  bunch of code inside a module.
56
- test_files: []
76
+ test_files:
77
+ - spec/example_data/single_top_level_class.rb
78
+ - spec/example_data/top_level_class_requires_another_file.rb
79
+ - spec/lib/wrap_in_module_spec.rb
80
+ - spec/spec_helper.rb