mynyml-swap 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright © 2009 Martin Aumont (mynyml)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7
+ of the Software, and to permit persons to whom the Software is furnished to do
8
+ so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ 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 THE
19
+ SOFTWARE.
data/README ADDED
@@ -0,0 +1,30 @@
1
+ ==== Summary
2
+
3
+ Swap allows dynamically replacing and restoring methods.
4
+ Useful as a stubbing device, as it allows unstubbing as well.
5
+
6
+ ==== Examples
7
+ # We can use ruby2ruby to inspect changes
8
+ require 'ruby2ruby'
9
+
10
+ class User
11
+ attr_accessor :name
12
+ end
13
+ user = User.new
14
+ user.name = 'martin'
15
+ puts user.name #=> 'martin'
16
+
17
+ puts Ruby2Ruby.translate(User, :name)
18
+
19
+ User.swap!(:name) { @name.reverse }
20
+ puts user.name #=> 'nitram'
21
+
22
+ puts Ruby2Ruby.translate(User, :name)
23
+
24
+ User.unswap!(:name)
25
+ puts user.name #=> 'martin'
26
+
27
+ puts Ruby2Ruby.translate(User, :name)
28
+
29
+
30
+ Calling #unswap! without argument will restore all swapped methods.
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ require 'rake/gempackagetask'
2
+ require 'pathname'
3
+ require 'yaml'
4
+
5
+ def gem
6
+ RUBY_1_9 ? 'gem19' : 'gem'
7
+ end
8
+
9
+ def all_except(paths)
10
+ Dir['**/*'] - paths.map {|path| path.strip.gsub(/^\//,'').gsub(/\/$/,'') }
11
+ end
12
+
13
+ spec = Gem::Specification.new do |s|
14
+ s.name = 'swap'
15
+ s.version = '0.1.1'
16
+ s.summary = "Ruby lib that allows dynamically replacing and restoring methods."
17
+ s.description = "Ruby lib that allows dynamically replacing and restoring methods. Organic stubbing/unstubbing."
18
+ s.author = "Martin Aumont"
19
+ s.email = 'mynyml@gmail.com'
20
+ s.homepage = ''
21
+ s.has_rdoc = true
22
+ s.require_path = "lib"
23
+ s.files = Dir['**/*']
24
+ end
25
+
26
+ Rake::GemPackageTask.new(spec) do |p|
27
+ p.gem_spec = spec
28
+ end
29
+
30
+
31
+ desc "Remove package products"
32
+ task :clean => :clobber_package
33
+
34
+ desc "Update the gemspec for GitHub's gem server"
35
+ task :gemspec do
36
+ Pathname("#{spec.name}.gemspec").open('w') {|f| f << YAML.dump(spec) }
37
+ end
38
+
39
+ desc "Install gem"
40
+ task :install => [:clobber, :package] do
41
+ sh "#{SUDO} #{gem} install pkg/#{spec.full_name}.gem"
42
+ end
43
+
44
+ desc "Uninstall gem"
45
+ task :uninstall => :clean do
46
+ sh "#{SUDO} #{gem} uninstall -v #{spec.version} -x #{spec.name}"
47
+ end
48
+
49
+
data/TODO ADDED
File without changes
@@ -0,0 +1,27 @@
1
+ require 'pathname'
2
+ require 'rubygems'
3
+ require 'ruby2ruby'
4
+ root = Pathname(__FILE__).dirname.parent
5
+ require root.join('lib/swap')
6
+
7
+ class User
8
+ attr_writer :name
9
+ def name
10
+ @name
11
+ end
12
+ end
13
+ user = User.new
14
+ user.name = 'martin'
15
+ puts user.name #=> 'martin'
16
+
17
+ puts Ruby2Ruby.translate(User, :name)
18
+
19
+ User.swap!(:name) { @name.reverse }
20
+ puts user.name #=> 'nitram'
21
+
22
+ puts Ruby2Ruby.translate(User, :name)
23
+
24
+ User.unswap!(:name)
25
+ puts user.name #=> 'martin'
26
+
27
+ puts Ruby2Ruby.translate(User, :name)
data/lib/swap.rb ADDED
@@ -0,0 +1,76 @@
1
+ class Class
2
+
3
+ # Swaps a method for another
4
+ #
5
+ # ==== Arguments
6
+ # name<Symbol>::
7
+ # Name of original method to swap
8
+ # code<String|Proc|UnboundMethod>::
9
+ # New code for method
10
+ #
11
+ # ==== Block
12
+ # Alternative way to pass method code
13
+ #
14
+ # ==== Returns
15
+ # self
16
+ #
17
+ # ==== Examples
18
+ # User.swap!(:name, "@name.reverse")
19
+ # User.swap!(:name, lambda { @name.reverse })
20
+ # User.swap!(:name) { @name.reverse }
21
+ # User.swap!(:name, User.instance_method(:first_name))
22
+ #
23
+ def swap!(name, code=nil, &block)
24
+ name = name.to_sym
25
+ meth = self.instance_method(name.to_sym)
26
+ @swapped_methods ||= {}
27
+ @swapped_methods[name] = meth
28
+ if block_given?
29
+ self.class_eval { define_method(name, &block) }
30
+ else
31
+ case code
32
+ when String
33
+ self.class_eval(%|def #{name}() #{code}; end|)
34
+ when Proc, UnboundMethod
35
+ self.class_eval { define_method(name, code) }
36
+ end
37
+ end
38
+ self
39
+ end
40
+
41
+ # Restores a swapped method
42
+ #
43
+ # ==== Arguments
44
+ # name<String>::
45
+ # Name of method to restore. If nil, all swapped methods for this class
46
+ # will be restored.
47
+ #
48
+ # ==== Returns
49
+ # self
50
+ #
51
+ # ==== Examples
52
+ # class User
53
+ # attr_accessor :name
54
+ # end
55
+ # user = User.new
56
+ # user.name = 'martin'
57
+ # puts user.name #=> 'martin'
58
+ #
59
+ # User.swap!(:name) { @name.reverse }
60
+ # puts user.name #=> 'nitram'
61
+ #
62
+ # User.unswap!(:name)
63
+ # puts user.name #=> 'martin'
64
+ #
65
+ def unswap!(name=nil)
66
+ if name
67
+ name = name.to_sym
68
+ self.class_eval { define_method(name, @swapped_methods[name]) }
69
+ else
70
+ @swapped_methods.each do |name, code|
71
+ self.class_eval { define_method(name, code) }
72
+ end
73
+ end
74
+ self
75
+ end
76
+ end
data/swap.gemspec ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: swap
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Martin Aumont
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-18 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Ruby lib that allows dynamically replacing and restoring methods. Organic stubbing/unstubbing.
17
+ email: mynyml@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib
26
+ - lib/swap.rb
27
+ - examples
28
+ - examples/simple.rb
29
+ - LICENSE
30
+ - README
31
+ - Rakefile
32
+ - test
33
+ - test/test_swap.rb
34
+ - test/test_helper.rb
35
+ - swap.gemspec
36
+ - TODO
37
+ has_rdoc: true
38
+ homepage: ""
39
+ post_install_message:
40
+ rdoc_options: []
41
+
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ requirements: []
57
+
58
+ rubyforge_project:
59
+ rubygems_version: 1.3.1
60
+ signing_key:
61
+ specification_version: 2
62
+ summary: Ruby lib that allows dynamically replacing and restoring methods.
63
+ test_files: []
64
+
@@ -0,0 +1,10 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'context'
4
+ require 'matchy'
5
+ begin
6
+ require 'ruby-debug'
7
+ require 'quietbacktrace'
8
+ rescue LoadError, RuntimeError
9
+ # pass
10
+ end
data/test/test_swap.rb ADDED
@@ -0,0 +1,59 @@
1
+ require 'pathname'
2
+ root = Pathname(__FILE__).dirname.parent
3
+ require root.join('test/test_helper')
4
+ require root.join('lib/swap')
5
+
6
+ class SwapTest < Test::Unit::TestCase
7
+ context "Swap" do
8
+ before do
9
+ class ::Kitty
10
+ def say() 'ohaie' end
11
+ end
12
+ end
13
+ after do
14
+ Object.class_eval { remove_const(:Kitty) }
15
+ defined?(::Kitty).should be(nil)
16
+ end
17
+ context "replacing instance method" do
18
+ test "accepts string" do
19
+ Kitty.swap!(:say, %( 'kthxbai'.reverse ))
20
+ Kitty.new.say.should be('kthxbai'.reverse)
21
+ end
22
+ test "accepts block" do
23
+ Kitty.swap!(:say) { 'kthxbai' }
24
+ Kitty.new.say.should be('kthxbai')
25
+ end
26
+ test "accepts proc" do
27
+ Kitty.swap!(:say, lambda { 'kthxbai' })
28
+ Kitty.new.say.should be('kthxbai')
29
+ end
30
+ test "accepts unbound method" do
31
+ class ::Kitty
32
+ def bai() 'kthxbai' end
33
+ end
34
+ Kitty.swap!(:say, Kitty.instance_method(:bai))
35
+ Kitty.new.say.should be('kthxbai')
36
+ end
37
+ end
38
+ context "reseting original methods" do
39
+ test "restores single method" do
40
+ Kitty.swap!(:say) { 'kthxbai' }
41
+ Kitty.new.say.should be('kthxbai')
42
+ Kitty.unswap!(:say)
43
+ Kitty.new.say.should be('ohaie')
44
+ end
45
+ test "restores all methods" do
46
+ class ::Kitty
47
+ def bai() 'kthxbai' end
48
+ end
49
+ Kitty.swap!(:say) { 'o.0' }
50
+ Kitty.swap!(:bai) { 'zZzz' }
51
+ Kitty.new.say.should be('o.0' )
52
+ Kitty.new.bai.should be('zZzz')
53
+ Kitty.unswap!
54
+ Kitty.new.say.should be('ohaie')
55
+ Kitty.new.bai.should be('kthxbai')
56
+ end
57
+ end
58
+ end
59
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mynyml-swap
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Martin Aumont
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-17 21:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Ruby lib that allows dynamically replacing and restoring methods. Organic stubbing/unstubbing.
17
+ email: mynyml@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib
26
+ - lib/swap.rb
27
+ - examples
28
+ - examples/simple.rb
29
+ - LICENSE
30
+ - README
31
+ - Rakefile
32
+ - test
33
+ - test/test_swap.rb
34
+ - test/test_helper.rb
35
+ - swap.gemspec
36
+ - TODO
37
+ has_rdoc: true
38
+ homepage: ""
39
+ post_install_message:
40
+ rdoc_options: []
41
+
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ requirements: []
57
+
58
+ rubyforge_project:
59
+ rubygems_version: 1.2.0
60
+ signing_key:
61
+ specification_version: 2
62
+ summary: Ruby lib that allows dynamically replacing and restoring methods.
63
+ test_files: []
64
+