dynamic_ruby 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/dynamic_ruby.rb +21 -0
- data/spec/dynamic_ruby_spec.rb +45 -0
- metadata +47 -0
data/lib/dynamic_ruby.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
def dynamic(bindings={}, &block)
|
2
|
+
if !block_given?
|
3
|
+
Thread.current
|
4
|
+
else
|
5
|
+
defined = {}
|
6
|
+
bindings.each do |name, val|
|
7
|
+
if Thread.current[name]
|
8
|
+
defined[name] = Thread.current[name]
|
9
|
+
end
|
10
|
+
Thread.current[name] = val
|
11
|
+
end
|
12
|
+
block.call
|
13
|
+
bindings.each do |name, val|
|
14
|
+
if defined[name]
|
15
|
+
Thread.current[name] = defined[name]
|
16
|
+
else
|
17
|
+
Thread.current[name] = nil
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require "minitest/autorun"
|
2
|
+
require_relative "../lib/dynamic_ruby"
|
3
|
+
|
4
|
+
describe "dynamic scope" do
|
5
|
+
it "should access dynamically declared variables" do
|
6
|
+
result = nil
|
7
|
+
testfun = lambda { dynamic[:name] }
|
8
|
+
dynamic(name: "john") do
|
9
|
+
result = testfun.call
|
10
|
+
end
|
11
|
+
result.must_equal "john"
|
12
|
+
testfun.call.must_be_nil
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should stack access to dynamically declare variables" do
|
16
|
+
result = nil
|
17
|
+
testfun = lambda { dynamic[:name] }
|
18
|
+
dynamic(name: "right") do
|
19
|
+
dynamic(name: "wrong") do
|
20
|
+
|
21
|
+
end
|
22
|
+
result = testfun.call
|
23
|
+
end
|
24
|
+
result.must_equal "right"
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should respect thread local variables" do
|
28
|
+
result = nil
|
29
|
+
testfun = lambda { dynamic[:name] }
|
30
|
+
thr = Thread.new do
|
31
|
+
dynamic(name: "right") do
|
32
|
+
sleep 0.1
|
33
|
+
result = testfun.call
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
Thread.new do
|
38
|
+
dynamic(name: "wrong") do
|
39
|
+
sleep 1
|
40
|
+
end
|
41
|
+
end
|
42
|
+
thr.join
|
43
|
+
result.must_equal "right"
|
44
|
+
end
|
45
|
+
end
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dynamic_ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jürgen Bickert
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-05-20 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: A thin wrapper around ruby for easy use of dynamic scope in your rubies
|
15
|
+
email: juergenbickert@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/dynamic_ruby.rb
|
21
|
+
- spec/dynamic_ruby_spec.rb
|
22
|
+
homepage: http://juergenbickert.de
|
23
|
+
licenses: []
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ! '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubyforge_project:
|
43
|
+
rubygems_version: 1.8.15
|
44
|
+
signing_key:
|
45
|
+
specification_version: 3
|
46
|
+
summary: A library which correctly implements dynamic scope for ruby.
|
47
|
+
test_files: []
|