require-here 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/.travis.yml +10 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +17 -0
- data/README.md +43 -0
- data/Rakefile +1 -0
- data/lib/require_here.rb +31 -0
- data/lib/require_here/version.rb +9 -0
- data/require-here.gemspec +44 -0
- data/spec/require_here_spec.rb +38 -0
- data/spec/support/lib_a.rb +2 -0
- metadata +92 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# require-here
|
2
|
+
|
3
|
+
Require constants into current namespace
|
4
|
+
|
5
|
+
[![Build Status](https://secure.travis-ci.org/hannestyden/require-here.png)](http://travis-ci.org/hannestyden/require-here)
|
6
|
+
|
7
|
+
# Example
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
# lib_a.rb
|
11
|
+
|
12
|
+
module GenericModuleName
|
13
|
+
def self.method
|
14
|
+
:lib_a
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# lib_b.rb
|
19
|
+
|
20
|
+
module GenericModuleName
|
21
|
+
def self.method
|
22
|
+
:lib_b
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# app.rb
|
27
|
+
|
28
|
+
module Application
|
29
|
+
module CertainAspect
|
30
|
+
require_here 'lib_a.rb'
|
31
|
+
end
|
32
|
+
|
33
|
+
module AnotherCertainAspect
|
34
|
+
require_here 'lib_b.rb'
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# The modules are not included, nor reopened, into the global namespace.
|
39
|
+
Application::CertainAspect::GenericModuleName.method # => :lib_a
|
40
|
+
Application::AnotherCertainAspect::GenericModuleName.method # => :lib_b
|
41
|
+
|
42
|
+
GenericModuleName rescue $! # => #<NameError: uninitialized constant Object::GenericModuleName>
|
43
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/require_here.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'require_here/version'
|
2
|
+
|
3
|
+
module RequireHere
|
4
|
+
LOOKUP = {}
|
5
|
+
|
6
|
+
def require_here(file_path)
|
7
|
+
# Existing constants before require.
|
8
|
+
existing = Object.constants
|
9
|
+
|
10
|
+
require(file_path)
|
11
|
+
|
12
|
+
# Introduced by require.
|
13
|
+
introduced = Object.constants - existing
|
14
|
+
|
15
|
+
# Move each introduced constant into the current namespace.
|
16
|
+
introduced.each_with_index do |c, i|
|
17
|
+
# Keep a reference to the constant.
|
18
|
+
LOOKUP[i] = Object.const_get(c)
|
19
|
+
# Remove the constant from the root.
|
20
|
+
Object.send(:remove_const, c)
|
21
|
+
# Place it in the current namespace.
|
22
|
+
module_eval %Q(#{c} = ::RequireHere::LOOKUP[#{i}])
|
23
|
+
end
|
24
|
+
|
25
|
+
LOOKUP.clear
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class Module
|
30
|
+
include RequireHere
|
31
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path('../lib', __FILE__)
|
3
|
+
require 'require_here/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'require-here'
|
7
|
+
s.version = RequireHere::Version::SEMANTIC
|
8
|
+
s.authors = [ 'Hannes Tydén' ]
|
9
|
+
s.email = [ 'hannes@soundcloud.com' ]
|
10
|
+
s.homepage = 'http://github.com/hannestyden/require-here'
|
11
|
+
s.summary = 'Require constants into current namespace.'
|
12
|
+
|
13
|
+
s.description = <<-DESCRIPTION
|
14
|
+
Keep your namespaces clean.
|
15
|
+
DESCRIPTION
|
16
|
+
|
17
|
+
# Required for validation.
|
18
|
+
s.rubyforge_project = 'require-here'
|
19
|
+
|
20
|
+
s.files = `git ls-files`.split("\n")
|
21
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
22
|
+
|
23
|
+
s.executables = `git ls-files -- bin/*`.
|
24
|
+
split("\n").map { |f| File.basename(f) }
|
25
|
+
|
26
|
+
s.require_paths = [ 'lib' ]
|
27
|
+
|
28
|
+
dependencies =
|
29
|
+
[
|
30
|
+
[ 'minitest', '~> 2.11' ],
|
31
|
+
]
|
32
|
+
|
33
|
+
developement_dependencies = []
|
34
|
+
|
35
|
+
runtime_dependencies = []
|
36
|
+
|
37
|
+
(dependencies + developement_dependencies).each do |dependency|
|
38
|
+
s.add_development_dependency *dependency
|
39
|
+
end
|
40
|
+
|
41
|
+
(dependencies + runtime_dependencies).each do |dependency|
|
42
|
+
s.add_runtime_dependency *dependency
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# Ensure gem is used over 1.9.x built in.
|
2
|
+
gem 'minitest' if RUBY_VERSION =~ /1.9.\d/
|
3
|
+
|
4
|
+
require 'minitest/spec'
|
5
|
+
require 'minitest/autorun'
|
6
|
+
|
7
|
+
$:.push File.expand_path('../../lib', __FILE__)
|
8
|
+
require 'require_here'
|
9
|
+
|
10
|
+
module RequireHere
|
11
|
+
module Version
|
12
|
+
describe 'semantic' do
|
13
|
+
it 'should be of version 0.0.0' do
|
14
|
+
SEMANTIC.must_equal '0.0.0'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe 'require_here' do
|
21
|
+
it 'should require the constants into the current namespace' do
|
22
|
+
module M
|
23
|
+
require_here './spec/support/lib_a'
|
24
|
+
end
|
25
|
+
|
26
|
+
defined?(M::A).must_equal 'constant'
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'should not require the constants into the global namespace' do
|
30
|
+
module M
|
31
|
+
require_here './spec/support/lib_a'
|
32
|
+
end
|
33
|
+
|
34
|
+
lambda do
|
35
|
+
A
|
36
|
+
end.must_raise NameError
|
37
|
+
end
|
38
|
+
end
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: require-here
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Hannes Tydén
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-05 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: minitest
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '2.11'
|
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: '2.11'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: minitest
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '2.11'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '2.11'
|
46
|
+
description: ! ' Keep your namespaces clean.
|
47
|
+
|
48
|
+
'
|
49
|
+
email:
|
50
|
+
- hannes@soundcloud.com
|
51
|
+
executables: []
|
52
|
+
extensions: []
|
53
|
+
extra_rdoc_files: []
|
54
|
+
files:
|
55
|
+
- .gitignore
|
56
|
+
- .travis.yml
|
57
|
+
- Gemfile
|
58
|
+
- Gemfile.lock
|
59
|
+
- README.md
|
60
|
+
- Rakefile
|
61
|
+
- lib/require_here.rb
|
62
|
+
- lib/require_here/version.rb
|
63
|
+
- require-here.gemspec
|
64
|
+
- spec/require_here_spec.rb
|
65
|
+
- spec/support/lib_a.rb
|
66
|
+
homepage: http://github.com/hannestyden/require-here
|
67
|
+
licenses: []
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options: []
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
80
|
+
requirements:
|
81
|
+
- - ! '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
requirements: []
|
85
|
+
rubyforge_project: require-here
|
86
|
+
rubygems_version: 1.8.21
|
87
|
+
signing_key:
|
88
|
+
specification_version: 3
|
89
|
+
summary: Require constants into current namespace.
|
90
|
+
test_files:
|
91
|
+
- spec/require_here_spec.rb
|
92
|
+
- spec/support/lib_a.rb
|