bondage 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +17 -0
- data/Manifest.txt +15 -0
- data/README.rdoc +74 -0
- data/Rakefile +25 -0
- data/description.rdoc +12 -0
- data/lib/bondage.rb +68 -0
- data/script/console +10 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/spec/bondage_spec.rb +111 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +10 -0
- data/synopsis.rb +26 -0
- data/tasks/readme.rake +7 -0
- data/tasks/rspec.rake +21 -0
- metadata +79 -0
data/History.txt
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
== 0.1.1 2009-11-08
|
2
|
+
|
3
|
+
* Updated newgem
|
4
|
+
|
5
|
+
== 0.1.0 2009-02-15
|
6
|
+
|
7
|
+
* Added a to_s method which adds "+Bondage" to the class name.
|
8
|
+
* Specs were working from TextMate, but there was an array ordering mismatch from the command line.
|
9
|
+
* Redid readme so the file is constructed from pieces, to avoid some include kinks.
|
10
|
+
|
11
|
+
== 0.0.1 2009-02-14
|
12
|
+
|
13
|
+
* Initial release:
|
14
|
+
* Hashes of local, global, etc.
|
15
|
+
* Enumerable for locals
|
16
|
+
* [] syntax for getting/setting variables
|
17
|
+
* Sanitize identifier names
|
data/Manifest.txt
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
History.txt
|
2
|
+
Manifest.txt
|
3
|
+
README.rdoc
|
4
|
+
Rakefile
|
5
|
+
description.rdoc
|
6
|
+
lib/bondage.rb
|
7
|
+
script/console
|
8
|
+
script/destroy
|
9
|
+
script/generate
|
10
|
+
spec/bondage_spec.rb
|
11
|
+
spec/spec.opts
|
12
|
+
spec/spec_helper.rb
|
13
|
+
synopsis.rb
|
14
|
+
tasks/readme.rake
|
15
|
+
tasks/rspec.rake
|
data/README.rdoc
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
= bondage
|
2
|
+
|
3
|
+
* http://github.com/JustinLove/bondage
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
class Binding stinks, module Bondage stinks less. Bondage provides hashes of local, global, instance, etc. variables, and provides the Enumerable interface for locals directly. It also provides [] syntax to get/set binding variables directly.
|
8
|
+
|
9
|
+
== FEATURES/PROBLEMS:
|
10
|
+
|
11
|
+
* Note that hashes provided by locals & co are plain old hashes (writing to them won't affect the binding) with references to the real data (modifying the values themselves will be reflected in the binding.)
|
12
|
+
* Warning: Uses eval internally, so don't use it with untrusted data. An attempt is made to sanitize identifier names. However, if your implementation doesn't support ObjectSpace, the second argument to []= is still evaled directly.
|
13
|
+
|
14
|
+
== SYNOPSIS:
|
15
|
+
|
16
|
+
require 'lib/bondage'
|
17
|
+
|
18
|
+
x = 1
|
19
|
+
y = 2
|
20
|
+
|
21
|
+
b = binding.extend(Bondage)
|
22
|
+
|
23
|
+
# return variable hashes
|
24
|
+
p b.locals # => {:x=>1, :y=>2, :b=>#<Binding:0xxxxxxx>}
|
25
|
+
p b.globals[:$PROGRAM_NAME] # => "synopis.rb"
|
26
|
+
|
27
|
+
#concerned about the environement?
|
28
|
+
p b.globals # => {:#!=>nil, :$SAFE=>0, ...}
|
29
|
+
|
30
|
+
# set and retrieve
|
31
|
+
b[:z] = ["one", "two", "three"]
|
32
|
+
p b[:$LOAD_PATH] # => ["./lib", ...]
|
33
|
+
p b[:@pizza] # => nil
|
34
|
+
|
35
|
+
# supports Enumerable over locals
|
36
|
+
p b.find {|var, value| value == 1 } # => [:x, 1]
|
37
|
+
|
38
|
+
# Or if you're feeling really kinky
|
39
|
+
class Binding
|
40
|
+
include Bondage
|
41
|
+
end
|
42
|
+
|
43
|
+
== REQUIREMENTS:
|
44
|
+
|
45
|
+
Tested under Ruby 1.8.7.
|
46
|
+
|
47
|
+
== INSTALL:
|
48
|
+
|
49
|
+
sudo gem install bondage
|
50
|
+
|
51
|
+
== LICENSE:
|
52
|
+
|
53
|
+
(The MIT License)
|
54
|
+
|
55
|
+
Copyright (c) 2009 Justin Love
|
56
|
+
|
57
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
58
|
+
a copy of this software and associated documentation files (the
|
59
|
+
'Software'), to deal in the Software without restriction, including
|
60
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
61
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
62
|
+
permit persons to whom the Software is furnished to do so, subject to
|
63
|
+
the following conditions:
|
64
|
+
|
65
|
+
The above copyright notice and this permission notice shall be
|
66
|
+
included in all copies or substantial portions of the Software.
|
67
|
+
|
68
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
69
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
70
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
71
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
72
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
73
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
74
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
gem 'hoe', '>= 2.1.0'
|
3
|
+
require 'hoe'
|
4
|
+
require 'fileutils'
|
5
|
+
require './lib/bondage'
|
6
|
+
|
7
|
+
Hoe.plugin :newgem
|
8
|
+
# Hoe.plugin :website
|
9
|
+
# Hoe.plugin :cucumberfeatures
|
10
|
+
|
11
|
+
# Generate all the Rake tasks
|
12
|
+
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
13
|
+
$hoe = Hoe.spec 'bondage' do
|
14
|
+
self.developer 'Justin Love', 'git@JustinLove.name'
|
15
|
+
self.rubyforge_name = self.name # TODO this is default value
|
16
|
+
# self.extra_deps = [['activesupport','>= 2.0.2']]
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'newgem/tasks'
|
21
|
+
Dir['tasks/**/*.rake'].each { |t| load t }
|
22
|
+
|
23
|
+
# TODO - want other tests/tasks run by default? Add them to the list
|
24
|
+
# remove_task :default
|
25
|
+
# task :default => [:spec, :features]
|
data/description.rdoc
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
= bondage
|
2
|
+
|
3
|
+
* http://github.com/JustinLove/bondage
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
class Binding stinks, module Bondage stinks less. Bondage provides hashes of local, global, instance, etc. variables, and provides the Enumerable interface for locals directly. It also provides [] syntax to get/set binding variables directly.
|
8
|
+
|
9
|
+
== FEATURES/PROBLEMS:
|
10
|
+
|
11
|
+
* Note that hashes provided by locals & co are plain old hashes (writing to them won't affect the binding) with references to the real data (modifying the values themselves will be reflected in the binding.)
|
12
|
+
* Warning: Uses eval internally, so don't use it with untrusted data. An attempt is made to sanitize identifier names. However, if your implementation doesn't support ObjectSpace, the second argument to []= is still evaled directly.
|
data/lib/bondage.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__)) unless
|
2
|
+
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
|
+
|
4
|
+
#:include:description.rdoc
|
5
|
+
#== SYNOPSIS
|
6
|
+
#:include:synopsis.rb
|
7
|
+
|
8
|
+
module Bondage
|
9
|
+
VERSION = '0.1.1'
|
10
|
+
|
11
|
+
# Adds "+Bondage" to default string
|
12
|
+
def to_s
|
13
|
+
c = self.class.to_s
|
14
|
+
super.sub(c, c + "+Bondage")
|
15
|
+
end
|
16
|
+
|
17
|
+
# Hash of local variables
|
18
|
+
def locals; list_all :local_variables; end
|
19
|
+
# Hash of global variables (with '$')
|
20
|
+
def globals; list_all :global_variables; end
|
21
|
+
# Hash of instance variables (with '@')
|
22
|
+
def instvars; list_all :instance_variables; end
|
23
|
+
# Hash of class variables (with '@@')
|
24
|
+
def classvars; list_all :class_variables; end
|
25
|
+
# Hash of constants
|
26
|
+
def consts; list_all :constants; end
|
27
|
+
|
28
|
+
# Iterator over locals; base for Enumerable methods.
|
29
|
+
def each(&proc)
|
30
|
+
locals.each(&proc)
|
31
|
+
end
|
32
|
+
include Enumerable
|
33
|
+
|
34
|
+
# Return the value of the symbol in the binding.
|
35
|
+
def lookup(name)
|
36
|
+
begin
|
37
|
+
eval(sanitize(name));
|
38
|
+
rescue NameError
|
39
|
+
return nil
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
alias_method :[], :lookup
|
44
|
+
|
45
|
+
# Assign the variable within the binding
|
46
|
+
# * If your implementation supports ObjectSpace, this is relatively safe.
|
47
|
+
# This preserves object identity. (Copy by reference.)
|
48
|
+
# * If not, the right-hand-side gets it's string value eval'ed
|
49
|
+
def []=(name, value)
|
50
|
+
if defined?(ObjectSpace)
|
51
|
+
eval("#{sanitize(name)} = ObjectSpace._id2ref(#{value.object_id})")
|
52
|
+
else
|
53
|
+
eval("#{sanitize(name)} = #{value}")
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
def list_all(kind)
|
59
|
+
eval(kind.to_s).inject({}) {|hash, var|
|
60
|
+
hash[var.to_sym] = lookup(var)
|
61
|
+
hash
|
62
|
+
}
|
63
|
+
end
|
64
|
+
|
65
|
+
def sanitize(name)
|
66
|
+
name.to_s.gsub(/[^$@\w]/, '_')
|
67
|
+
end
|
68
|
+
end
|
data/script/console
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# File: script/console
|
3
|
+
irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
|
4
|
+
|
5
|
+
libs = " -r irb/completion"
|
6
|
+
# Perhaps use a console_lib to store any extra methods I may want available in the cosole
|
7
|
+
# libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
|
8
|
+
libs << " -r #{File.dirname(__FILE__) + '/../lib/bondage.rb'}"
|
9
|
+
puts "Loading bondage gem"
|
10
|
+
exec "#{irb} #{libs} --simple-prompt"
|
data/script/destroy
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/destroy'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Destroy.new.run(ARGV)
|
data/script/generate
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/generate'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Generate.new.run(ARGV)
|
@@ -0,0 +1,111 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper.rb'
|
2
|
+
|
3
|
+
describe "well behaved Bondage", :shared => true do
|
4
|
+
it "extends" do
|
5
|
+
@object.should be_kind_of(Bondage)
|
6
|
+
end
|
7
|
+
|
8
|
+
it "shows up in to_s" do
|
9
|
+
@object.to_s.should match(/Bondage/)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "look up locals" do
|
13
|
+
@object.locals[:x].should == 1
|
14
|
+
@object.locals[:y].should == 2
|
15
|
+
@object.locals[:z].should be_nil
|
16
|
+
end
|
17
|
+
|
18
|
+
it "look up globals" do
|
19
|
+
@object.globals[:$a].should == $a
|
20
|
+
@object.globals[:$b].should == $b
|
21
|
+
@object.globals[:$c].should be_nil
|
22
|
+
end
|
23
|
+
|
24
|
+
it "look up instance" do
|
25
|
+
@object.instvars[:@object].should == @object
|
26
|
+
end
|
27
|
+
|
28
|
+
it "look up class variables" do
|
29
|
+
b = class Blarg
|
30
|
+
@@blarg = :bleep
|
31
|
+
binding.extend(Bondage)
|
32
|
+
end
|
33
|
+
b.classvars[:@@blarg].should == :bleep
|
34
|
+
end
|
35
|
+
|
36
|
+
it "looks up constants" do
|
37
|
+
b = class Blarg
|
38
|
+
if !defined?(Bleep)
|
39
|
+
Bleep = 5
|
40
|
+
end
|
41
|
+
binding.extend(Bondage)
|
42
|
+
end
|
43
|
+
b.consts[:Bleep].should == 5
|
44
|
+
end
|
45
|
+
|
46
|
+
it "supports each on locals" do
|
47
|
+
seen = []
|
48
|
+
@object.each {|var, value| seen << var}
|
49
|
+
seen.should include(:x)
|
50
|
+
seen.should include(:y)
|
51
|
+
end
|
52
|
+
|
53
|
+
it "can enumerate" do
|
54
|
+
@object.find {|var, value| value == 1}.should == [:x, 1]
|
55
|
+
end
|
56
|
+
|
57
|
+
it "looks up names" do
|
58
|
+
@object[:x].should == 1
|
59
|
+
@object[$a].should == $a
|
60
|
+
@object[:z].should be_nil
|
61
|
+
end
|
62
|
+
|
63
|
+
it "sets names" do
|
64
|
+
@object[:w] = 5
|
65
|
+
eval("w", @object).should == 5
|
66
|
+
end
|
67
|
+
|
68
|
+
it "sets complex values" do
|
69
|
+
a = [1, 2, 3]
|
70
|
+
@object[:u] = a
|
71
|
+
@object[:u].object_id.should == a.object_id
|
72
|
+
end
|
73
|
+
|
74
|
+
it "avoids identifier injection" do
|
75
|
+
@object[:"raise 'gotcha'; x"].should == nil
|
76
|
+
end
|
77
|
+
|
78
|
+
it "avoids identifier injection on assignment" do
|
79
|
+
@object[:"raise 'gotcha'; v"] = 6
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe "extended" do
|
84
|
+
before do
|
85
|
+
x = 1
|
86
|
+
y = 2
|
87
|
+
$a = 3
|
88
|
+
$b = 4
|
89
|
+
@object = binding.extend(Bondage)
|
90
|
+
end
|
91
|
+
|
92
|
+
it_should_behave_like "well behaved Bondage"
|
93
|
+
end
|
94
|
+
|
95
|
+
describe "monkey patched" do
|
96
|
+
before :all do
|
97
|
+
class Binding
|
98
|
+
include Bondage
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
before do
|
103
|
+
x = 1
|
104
|
+
y = 2
|
105
|
+
$a = 3
|
106
|
+
$b = 4
|
107
|
+
@object = binding
|
108
|
+
end
|
109
|
+
|
110
|
+
it_should_behave_like "well behaved Bondage"
|
111
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour
|
data/spec/spec_helper.rb
ADDED
data/synopsis.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'lib/bondage'
|
2
|
+
|
3
|
+
x = 1
|
4
|
+
y = 2
|
5
|
+
|
6
|
+
b = binding.extend(Bondage)
|
7
|
+
|
8
|
+
# return variable hashes
|
9
|
+
p b.locals # => {:x=>1, :y=>2, :b=>#<Binding:0xxxxxxx>}
|
10
|
+
p b.globals[:$PROGRAM_NAME] # => "synopis.rb"
|
11
|
+
|
12
|
+
#concerned about the environement?
|
13
|
+
p b.globals # => {:#!=>nil, :$SAFE=>0, ...}
|
14
|
+
|
15
|
+
# set and retrieve
|
16
|
+
b[:z] = ["one", "two", "three"]
|
17
|
+
p b[:$LOAD_PATH] # => ["./lib", ...]
|
18
|
+
p b[:@pizza] # => nil
|
19
|
+
|
20
|
+
# supports Enumerable over locals
|
21
|
+
p b.find {|var, value| value == 1 } # => [:x, 1]
|
22
|
+
|
23
|
+
# Or if you're feeling really kinky
|
24
|
+
class Binding
|
25
|
+
include Bondage
|
26
|
+
end
|
data/tasks/readme.rake
ADDED
data/tasks/rspec.rake
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
begin
|
2
|
+
require 'spec'
|
3
|
+
rescue LoadError
|
4
|
+
require 'rubygems' unless ENV['NO_RUBYGEMS']
|
5
|
+
require 'spec'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'spec/rake/spectask'
|
9
|
+
rescue LoadError
|
10
|
+
puts <<-EOS
|
11
|
+
To use rspec for testing you must install rspec gem:
|
12
|
+
gem install rspec
|
13
|
+
EOS
|
14
|
+
exit(0)
|
15
|
+
end
|
16
|
+
|
17
|
+
desc "Run the specs under spec/models"
|
18
|
+
Spec::Rake::SpecTask.new do |t|
|
19
|
+
t.spec_opts = ['--options', "spec/spec.opts"]
|
20
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bondage
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Justin Love
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-11-08 00:00:00 -06:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: hoe
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 2.3.3
|
24
|
+
version:
|
25
|
+
description: class Binding stinks, module Bondage stinks less. Bondage provides hashes of local, global, instance, etc. variables, and provides the Enumerable interface for locals directly. It also provides [] syntax to get/set binding variables directly.
|
26
|
+
email:
|
27
|
+
- git@JustinLove.name
|
28
|
+
executables: []
|
29
|
+
|
30
|
+
extensions: []
|
31
|
+
|
32
|
+
extra_rdoc_files:
|
33
|
+
- History.txt
|
34
|
+
- Manifest.txt
|
35
|
+
files:
|
36
|
+
- History.txt
|
37
|
+
- Manifest.txt
|
38
|
+
- README.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- description.rdoc
|
41
|
+
- lib/bondage.rb
|
42
|
+
- script/console
|
43
|
+
- script/destroy
|
44
|
+
- script/generate
|
45
|
+
- spec/bondage_spec.rb
|
46
|
+
- spec/spec.opts
|
47
|
+
- spec/spec_helper.rb
|
48
|
+
- synopsis.rb
|
49
|
+
- tasks/readme.rake
|
50
|
+
- tasks/rspec.rake
|
51
|
+
has_rdoc: true
|
52
|
+
homepage: http://github.com/JustinLove/bondage
|
53
|
+
post_install_message:
|
54
|
+
rdoc_options:
|
55
|
+
- --main
|
56
|
+
- README.rdoc
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: "0"
|
64
|
+
version:
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: "0"
|
70
|
+
version:
|
71
|
+
requirements: []
|
72
|
+
|
73
|
+
rubyforge_project: bondage
|
74
|
+
rubygems_version: 1.3.1
|
75
|
+
signing_key:
|
76
|
+
specification_version: 2
|
77
|
+
summary: class Binding stinks, module Bondage stinks less
|
78
|
+
test_files: []
|
79
|
+
|