JustinLove-bondage 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,13 @@
1
+ == 0.1.0 2009-02-15
2
+
3
+ * Added a to_s method which adds "+Bondage" to the class name.
4
+ * Specs were working from TextMate, but there was an array ordering mismatch from the command line.
5
+ * Redid readme so the file is constructed from pieces, to avoid some include kinks.
6
+
7
+ == 0.0.1 2009-02-14
8
+
9
+ * Initial release:
10
+ * Hashes of local, global, etc.
11
+ * Enumerable for locals
12
+ * [] syntax for getting/setting variables
13
+ * Sanitize identifier names
@@ -0,0 +1,22 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2009 Justin Love
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ 'Software'), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,16 @@
1
+ History.txt
2
+ MIT-LICENSE.txt
3
+ Manifest.txt
4
+ README.rdoc
5
+ Rakefile
6
+ description.rdoc
7
+ lib/bondage.rb
8
+ script/console
9
+ script/destroy
10
+ script/generate
11
+ spec/bondage_spec.rb
12
+ spec/spec.opts
13
+ spec/spec_helper.rb
14
+ synopsis.rb
15
+ tasks/readme.rake
16
+ tasks/rspec.rake
@@ -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.
@@ -0,0 +1,28 @@
1
+ %w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
2
+ require File.dirname(__FILE__) + '/lib/bondage'
3
+
4
+ # Generate all the Rake tasks
5
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
6
+ $hoe = Hoe.new('bondage', Bondage::VERSION) do |p|
7
+ p.developer('Justin Love', 'git@JustinLove.name')
8
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
9
+ #p.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
10
+ p.rubyforge_name = p.name # TODO this is default value
11
+ # p.extra_deps = [
12
+ # ['activesupport','>= 2.0.2'],
13
+ # ]
14
+ p.extra_dev_deps = [
15
+ ['newgem', ">= #{::Newgem::VERSION}"]
16
+ ]
17
+
18
+ p.clean_globs |= %w[**/.DS_Store tmp *.log]
19
+ path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
20
+ p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
21
+ p.rsync_args = '-av --delete --ignore-errors'
22
+ end
23
+
24
+ require 'newgem/tasks' # load /tasks/*.rake
25
+ Dir['tasks/**/*.rake'].each { |t| load t }
26
+
27
+ # TODO - want other tests/tasks run by default? Add them to the list
28
+ # task :default => [:spec, :features]
@@ -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.
@@ -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.0'
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
@@ -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"
@@ -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)
@@ -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
@@ -0,0 +1 @@
1
+ --colour
@@ -0,0 +1,10 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems'
5
+ gem 'rspec'
6
+ require 'spec'
7
+ end
8
+
9
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
10
+ require 'bondage'
@@ -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
@@ -0,0 +1,8 @@
1
+ file "README.rdoc" => %w{
2
+ description.rdoc
3
+ frag/synopsis_header.rdoc
4
+ synopsis.rb
5
+ frag/misc.rdoc
6
+ MIT-LICENSE.txt} do |t|
7
+ sh "cat #{t.prerequisites.join(' ')} > #{t.name}"
8
+ end
@@ -0,0 +1,21 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require '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,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: JustinLove-bondage
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Justin Love
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-15 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: newgem
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.2.3
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: hoe
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.8.0
34
+ version:
35
+ 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.
36
+ email:
37
+ - git@JustinLove.name
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files:
43
+ - History.txt
44
+ - MIT-LICENSE.txt
45
+ - Manifest.txt
46
+ - README.rdoc
47
+ - description.rdoc
48
+ files:
49
+ - History.txt
50
+ - MIT-LICENSE.txt
51
+ - Manifest.txt
52
+ - README.rdoc
53
+ - Rakefile
54
+ - description.rdoc
55
+ - lib/bondage.rb
56
+ - script/console
57
+ - script/destroy
58
+ - script/generate
59
+ - spec/bondage_spec.rb
60
+ - spec/spec.opts
61
+ - spec/spec_helper.rb
62
+ - synopsis.rb
63
+ - tasks/readme.rake
64
+ - tasks/rspec.rake
65
+ has_rdoc: true
66
+ homepage: http://github.com/JustinLove/bondage
67
+ post_install_message:
68
+ rdoc_options:
69
+ - --main
70
+ - README.rdoc
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: "0"
78
+ version:
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: "0"
84
+ version:
85
+ requirements: []
86
+
87
+ rubyforge_project: bondage
88
+ rubygems_version: 1.2.0
89
+ signing_key:
90
+ specification_version: 2
91
+ summary: class Binding stinks, module Bondage stinks less
92
+ test_files: []
93
+