cargo 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +19 -0
- data/README.markdown +128 -0
- data/Rakefile +5 -0
- data/cargo.gemspec +10 -0
- data/lib/cargo.rb +25 -0
- data/test/cargo_test.rb +45 -0
- data/test/foo-1.0.0.rb +19 -0
- data/test/foo-2.0.0.rb +19 -0
- data/test/helper.rb +26 -0
- metadata +75 -0
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2010 Michel Martens & Damian Janowski
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
Cargo
|
2
|
+
=====
|
3
|
+
|
4
|
+
Require libraries without cluttering your namespace.
|
5
|
+
|
6
|
+
Description
|
7
|
+
-----------
|
8
|
+
|
9
|
+
Consider these two versions of the same library:
|
10
|
+
|
11
|
+
# foo-1.0.0.rb
|
12
|
+
class Foo
|
13
|
+
VERSION = "1.0.0"
|
14
|
+
end
|
15
|
+
|
16
|
+
# foo-2.0.0.rb
|
17
|
+
class Foo
|
18
|
+
VERSION = "2.0.0"
|
19
|
+
end
|
20
|
+
|
21
|
+
If you want to use both of them in your application, `require` and
|
22
|
+
`load` will be of little help. Consider what happens when you require
|
23
|
+
both libraries:
|
24
|
+
|
25
|
+
>> require "foo-1.0.0"
|
26
|
+
=> true
|
27
|
+
>> require "foo-2.0.0"
|
28
|
+
=> true
|
29
|
+
>> Foo::VERSION
|
30
|
+
=> "2.0.0"
|
31
|
+
|
32
|
+
As both files are opening the same class, there's no way for you to use
|
33
|
+
them independently.
|
34
|
+
|
35
|
+
Cargo solves that problem in a very simple way:
|
36
|
+
|
37
|
+
>> Foo1 = import("foo-1.0.0")
|
38
|
+
=> #<Module:0x000001009ffa28>::Foo
|
39
|
+
>> Foo2 = import("foo-2.0.0")
|
40
|
+
=> #<Module:0x000001009f2828>::Foo
|
41
|
+
>> Foo1::VERSION
|
42
|
+
=> "1.0.0"
|
43
|
+
>> Foo2::VERSION
|
44
|
+
=> "2.0.0"
|
45
|
+
|
46
|
+
How does it work?
|
47
|
+
-----------------
|
48
|
+
|
49
|
+
The command `Kernel.load` accepts a second parameter, which value is
|
50
|
+
`false` by default. When the passed value is `true`, the script is
|
51
|
+
executed under an anonymous module, protecting the calling program's
|
52
|
+
global namespace. No local variables in the loaded file are propagated
|
53
|
+
to the loading environment.
|
54
|
+
|
55
|
+
By using a global module as a temporal storage, it is possible to
|
56
|
+
transfer a value from the anonymous module to the caller program.
|
57
|
+
|
58
|
+
Usage
|
59
|
+
-----
|
60
|
+
|
61
|
+
After requiring `cargo` in the calling program, two methods will
|
62
|
+
be available: `import` and `export`. Both use the global module
|
63
|
+
`Cargo` for temporal storage.
|
64
|
+
|
65
|
+
This is how you will use it in the calling program:
|
66
|
+
|
67
|
+
$ cat calling_program.rb
|
68
|
+
require "cargo"
|
69
|
+
|
70
|
+
MyFoo = import("foo")
|
71
|
+
|
72
|
+
|
73
|
+
And for that to work, the imported library should look like this:
|
74
|
+
|
75
|
+
$ cat foo.rb
|
76
|
+
class Foo
|
77
|
+
VERSION = "1.0.0"
|
78
|
+
end
|
79
|
+
|
80
|
+
export(Foo)
|
81
|
+
|
82
|
+
Note that you can only export one value per file. Multiple calls to
|
83
|
+
`export` just change the value to be exported. This constraint is in
|
84
|
+
place in order to make for a simple and clean implementation.
|
85
|
+
|
86
|
+
How to prepare your library for Cargo
|
87
|
+
-------------------------------------
|
88
|
+
|
89
|
+
In your library entry point, you need to use `export` and provide a value. As
|
90
|
+
the `export` method won't be available unless the caller is using
|
91
|
+
Cargo, you can make a conditional call:
|
92
|
+
|
93
|
+
export(Foo) if defined?(export)
|
94
|
+
|
95
|
+
If your library has just one file, that single line will make your code
|
96
|
+
ready to be imported. Otherwise, you will need to require Cargo and
|
97
|
+
make sure you don't mix import with require for local files.
|
98
|
+
|
99
|
+
Installation
|
100
|
+
------------
|
101
|
+
|
102
|
+
$ gem install cargo
|
103
|
+
|
104
|
+
License
|
105
|
+
-------
|
106
|
+
|
107
|
+
Copyright (c) 2010 Michel Martens & Damian Janowski
|
108
|
+
|
109
|
+
Permission is hereby granted, free of charge, to any person
|
110
|
+
obtaining a copy of this software and associated documentation
|
111
|
+
files (the "Software"), to deal in the Software without
|
112
|
+
restriction, including without limitation the rights to use,
|
113
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
114
|
+
copies of the Software, and to permit persons to whom the
|
115
|
+
Software is furnished to do so, subject to the following
|
116
|
+
conditions:
|
117
|
+
|
118
|
+
The above copyright notice and this permission notice shall be
|
119
|
+
included in all copies or substantial portions of the Software.
|
120
|
+
|
121
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
122
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
123
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
124
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
125
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
126
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
127
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
128
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
data/cargo.gemspec
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "cargo"
|
3
|
+
s.version = "0.0.1"
|
4
|
+
s.summary = "Require libraries without cluttering your namespace."
|
5
|
+
s.description = "Cargo lets you use different versions of the same library by implementing two simple commands, which let you load programs without messing with your namespace."
|
6
|
+
s.authors = ["Michel Martens", "Damian Janowski"]
|
7
|
+
s.email = ["michel@soveran.com", "djanowski@dimaion.com"]
|
8
|
+
s.homepage = "http://github.com/soveran/cargo"
|
9
|
+
s.files = ["LICENSE", "README.markdown", "Rakefile", "lib/cargo.rb", "cargo.gemspec", "test/cargo_test.rb", "test/foo-1.0.0.rb", "test/foo-2.0.0.rb", "test/helper.rb"]
|
10
|
+
end
|
data/lib/cargo.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
module Cargo
|
2
|
+
VERSION = "0.0.1"
|
3
|
+
|
4
|
+
def self.import(file)
|
5
|
+
unless file.match(/\.rb$/)
|
6
|
+
file = "#{file}.rb"
|
7
|
+
end
|
8
|
+
|
9
|
+
load(file, true)
|
10
|
+
|
11
|
+
@cargo
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.export(cargo)
|
15
|
+
@cargo = cargo
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def import(file)
|
20
|
+
Cargo.import(file)
|
21
|
+
end
|
22
|
+
|
23
|
+
def export(cargo)
|
24
|
+
Cargo.export(cargo)
|
25
|
+
end
|
data/test/cargo_test.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require File.join(File.dirname(__FILE__), "helper")
|
4
|
+
require "ruby-debug"
|
5
|
+
|
6
|
+
setup do
|
7
|
+
unless defined?(Foo1)
|
8
|
+
Foo1 = import("foo-1.0.0")
|
9
|
+
end
|
10
|
+
|
11
|
+
unless defined?(Foo2)
|
12
|
+
Foo2 = import("foo-2.0.0")
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
test "Foo is not available" do
|
17
|
+
assert nil == defined?(Foo)
|
18
|
+
end
|
19
|
+
|
20
|
+
test "Foo1 is a class" do
|
21
|
+
assert Class == Foo1.class
|
22
|
+
end
|
23
|
+
|
24
|
+
test "methods are available" do
|
25
|
+
assert "Hello" == Foo1.new.bar
|
26
|
+
end
|
27
|
+
|
28
|
+
test "methods on nested classes are available" do
|
29
|
+
assert "Hello" == Foo1::Bar.new.baz
|
30
|
+
assert "Hello" == Foo1::Bar::Baz.new.qux
|
31
|
+
end
|
32
|
+
|
33
|
+
test "nested classes are not available in the top level" do
|
34
|
+
begin
|
35
|
+
Bar::Baz.new.qux
|
36
|
+
rescue
|
37
|
+
assert NameError == $!.class
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
test "Foo2 should be possible" do
|
42
|
+
assert "Hello" == Foo2.new.bar
|
43
|
+
end
|
44
|
+
|
45
|
+
puts " ✔ All tests passed"
|
data/test/foo-1.0.0.rb
ADDED
data/test/foo-2.0.0.rb
ADDED
data/test/helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require File.join(File.dirname(__FILE__), "..", "lib", "cargo")
|
4
|
+
|
5
|
+
AssertionFailed = Class.new(StandardError)
|
6
|
+
|
7
|
+
def assert(value)
|
8
|
+
return if value
|
9
|
+
|
10
|
+
file, line = caller[0].split(":")
|
11
|
+
code = File.readlines(file)[line.to_i - 1]
|
12
|
+
|
13
|
+
puts " ✗ #{code.strip}"
|
14
|
+
puts " #{file}:#{line}"
|
15
|
+
exit(1)
|
16
|
+
end
|
17
|
+
|
18
|
+
def setup(&block)
|
19
|
+
@_setup = block
|
20
|
+
end
|
21
|
+
|
22
|
+
def test(name = nil, &block)
|
23
|
+
@_test = name
|
24
|
+
|
25
|
+
block.call(@_setup.call)
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cargo
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Michel Martens
|
13
|
+
- Damian Janowski
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-07-26 00:00:00 -03:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: Cargo lets you use different versions of the same library by implementing two simple commands, which let you load programs without messing with your namespace.
|
23
|
+
email:
|
24
|
+
- michel@soveran.com
|
25
|
+
- djanowski@dimaion.com
|
26
|
+
executables: []
|
27
|
+
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files: []
|
31
|
+
|
32
|
+
files:
|
33
|
+
- LICENSE
|
34
|
+
- README.markdown
|
35
|
+
- Rakefile
|
36
|
+
- lib/cargo.rb
|
37
|
+
- cargo.gemspec
|
38
|
+
- test/cargo_test.rb
|
39
|
+
- test/foo-1.0.0.rb
|
40
|
+
- test/foo-2.0.0.rb
|
41
|
+
- test/helper.rb
|
42
|
+
has_rdoc: true
|
43
|
+
homepage: http://github.com/soveran/cargo
|
44
|
+
licenses: []
|
45
|
+
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options: []
|
48
|
+
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
segments:
|
57
|
+
- 0
|
58
|
+
version: "0"
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
segments:
|
65
|
+
- 0
|
66
|
+
version: "0"
|
67
|
+
requirements: []
|
68
|
+
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 1.3.7
|
71
|
+
signing_key:
|
72
|
+
specification_version: 3
|
73
|
+
summary: Require libraries without cluttering your namespace.
|
74
|
+
test_files: []
|
75
|
+
|