swap 0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +19 -0
- data/README +32 -0
- data/Rakefile +25 -0
- data/TODO +0 -0
- data/examples/simple.rb +47 -0
- data/lib/swap.rb +87 -0
- data/specs.watchr +24 -0
- data/swap.gemspec +26 -0
- data/test/test_helper.rb +16 -0
- data/test/test_swap.rb +57 -0
- metadata +64 -0
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,32 @@
|
|
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
|
+
|
8
|
+
require 'swap'
|
9
|
+
|
10
|
+
class User
|
11
|
+
extend Swappable
|
12
|
+
|
13
|
+
attr_writer :name
|
14
|
+
def name
|
15
|
+
@name
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
user = User.new
|
20
|
+
user.name = 'martin'
|
21
|
+
user.name #=> 'martin'
|
22
|
+
|
23
|
+
User.swap!(:name) { @name.reverse }
|
24
|
+
user.name #=> 'nitram'
|
25
|
+
|
26
|
+
User.unswap!(:name)
|
27
|
+
user.name #=> 'martin'
|
28
|
+
|
29
|
+
Calling #unswap! without argument will restore all swapped methods.
|
30
|
+
|
31
|
+
See also examples/simple.rb
|
32
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'rake/rdoctask'
|
2
|
+
begin
|
3
|
+
require 'yard'
|
4
|
+
rescue LoadError, RuntimeError
|
5
|
+
end
|
6
|
+
|
7
|
+
desc "Generate rdoc documentation."
|
8
|
+
Rake::RDocTask.new(:rdoc => 'rdoc', :clobber_rdoc => 'rdoc:clean', :rerdoc => 'rdoc:force') { |rdoc|
|
9
|
+
rdoc.rdoc_dir = 'doc/rdoc'
|
10
|
+
rdoc.title = "Swap"
|
11
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
12
|
+
rdoc.options << '--charset' << 'utf-8'
|
13
|
+
rdoc.main = 'README'
|
14
|
+
rdoc.rdoc_files.include('README')
|
15
|
+
rdoc.rdoc_files.include('TODO')
|
16
|
+
rdoc.rdoc_files.include('LICENSE')
|
17
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
18
|
+
}
|
19
|
+
|
20
|
+
if defined? YARD
|
21
|
+
YARD::Rake::YardocTask.new do |t|
|
22
|
+
t.files = %w( lib/**/*.rb )
|
23
|
+
t.options = %w( -o doc/yard --readme README --files LICENSE,TODO )
|
24
|
+
end
|
25
|
+
end
|
data/TODO
ADDED
File without changes
|
data/examples/simple.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# Run me with:
|
2
|
+
#
|
3
|
+
# ruby -rubygems examples/simple.rb
|
4
|
+
#
|
5
|
+
|
6
|
+
require 'swap'
|
7
|
+
|
8
|
+
# and let's spy on the source
|
9
|
+
require 'parse_tree_extensions'
|
10
|
+
require 'ruby2ruby'
|
11
|
+
|
12
|
+
class User
|
13
|
+
extend Swappable
|
14
|
+
attr_writer :name
|
15
|
+
def name
|
16
|
+
@name
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
user = User.new
|
21
|
+
user.name = 'martin'
|
22
|
+
puts user.name
|
23
|
+
#=> 'martin'
|
24
|
+
|
25
|
+
puts user.method(:name).to_ruby
|
26
|
+
#=> def name
|
27
|
+
#=> @name
|
28
|
+
#=> end
|
29
|
+
|
30
|
+
User.swap!(:name) { @name.reverse }
|
31
|
+
puts user.name
|
32
|
+
#=> 'nitram'
|
33
|
+
|
34
|
+
puts user.method(:name).to_ruby
|
35
|
+
#=> def name
|
36
|
+
#=> @name.reverse
|
37
|
+
#=> end
|
38
|
+
|
39
|
+
User.unswap!(:name)
|
40
|
+
puts user.name
|
41
|
+
#=> 'martin'
|
42
|
+
|
43
|
+
puts user.method(:name).to_ruby
|
44
|
+
#=> def name
|
45
|
+
#=> @name
|
46
|
+
#=> end
|
47
|
+
|
data/lib/swap.rb
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
|
2
|
+
# Swappable mixin.
|
3
|
+
#
|
4
|
+
# ===== Examples
|
5
|
+
#
|
6
|
+
# Class User
|
7
|
+
# extend Swappable
|
8
|
+
# end
|
9
|
+
#
|
10
|
+
module Swappable
|
11
|
+
|
12
|
+
# Swaps a method for another
|
13
|
+
#
|
14
|
+
# ==== Arguments
|
15
|
+
# name<Symbol>::
|
16
|
+
# Name of original method to swap
|
17
|
+
# code<String|Proc|UnboundMethod>::
|
18
|
+
# New code for method
|
19
|
+
#
|
20
|
+
# ==== Block
|
21
|
+
# Alternative way to pass method code
|
22
|
+
#
|
23
|
+
# ==== Returns
|
24
|
+
# self
|
25
|
+
#
|
26
|
+
# ==== Examples
|
27
|
+
#
|
28
|
+
# User.swap!(:name, "@name.reverse")
|
29
|
+
# User.swap!(:name, lambda { @name.reverse })
|
30
|
+
# User.swap!(:name) { @name.reverse }
|
31
|
+
# User.swap!(:name, User.instance_method(:first_name))
|
32
|
+
#
|
33
|
+
def swap!(name, code=nil, &block)
|
34
|
+
name = name.to_sym
|
35
|
+
meth = self.instance_method(name.to_sym)
|
36
|
+
@swapped_methods ||= {}
|
37
|
+
@swapped_methods[name] = meth
|
38
|
+
if block_given?
|
39
|
+
self.class_eval { define_method(name, &block) }
|
40
|
+
else
|
41
|
+
case code
|
42
|
+
when String
|
43
|
+
self.class_eval(%|def #{name}() #{code}; end|)
|
44
|
+
when Proc, UnboundMethod
|
45
|
+
self.class_eval { define_method(name, code) }
|
46
|
+
end
|
47
|
+
end
|
48
|
+
self
|
49
|
+
end
|
50
|
+
|
51
|
+
# Restores a swapped method
|
52
|
+
#
|
53
|
+
# ==== Arguments
|
54
|
+
# name<String>::
|
55
|
+
# Name of method to restore. If nil, all swapped methods for this class
|
56
|
+
# will be restored.
|
57
|
+
#
|
58
|
+
# ==== Returns
|
59
|
+
# self
|
60
|
+
#
|
61
|
+
# ==== Examples
|
62
|
+
#
|
63
|
+
# class User
|
64
|
+
# attr_accessor :name
|
65
|
+
# end
|
66
|
+
# user = User.new
|
67
|
+
# user.name = 'martin'
|
68
|
+
# puts user.name #=> 'martin'
|
69
|
+
#
|
70
|
+
# User.swap!(:name) { @name.reverse }
|
71
|
+
# puts user.name #=> 'nitram'
|
72
|
+
#
|
73
|
+
# User.unswap!(:name)
|
74
|
+
# puts user.name #=> 'martin'
|
75
|
+
#
|
76
|
+
def unswap!(name=nil)
|
77
|
+
if name
|
78
|
+
name = name.to_sym
|
79
|
+
self.class_eval { define_method(name, @swapped_methods[name]) }
|
80
|
+
else
|
81
|
+
@swapped_methods.each do |name, code|
|
82
|
+
self.class_eval { define_method(name, code) }
|
83
|
+
end
|
84
|
+
end
|
85
|
+
self
|
86
|
+
end
|
87
|
+
end
|
data/specs.watchr
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# Run me with:
|
2
|
+
#
|
3
|
+
# $ watchr specs.watchr
|
4
|
+
|
5
|
+
# --------------------------------------------------
|
6
|
+
# Watchr Rules
|
7
|
+
# --------------------------------------------------
|
8
|
+
watch( '^test/test_swap\.rb' ) { system "ruby -rubygems test/test_swap.rb" }
|
9
|
+
watch( '^lib/swap\.rb' ) { system "ruby -rubygems test/test_swap.rb" }
|
10
|
+
watch( '^test/test_helper\.rb' ) { system "ruby -rubygems test/test_swap.rb" }
|
11
|
+
watch( '^examples/simple\.rb' ) { system "ruby -rubygems examples/simple.rb"; puts '-'*30 }
|
12
|
+
|
13
|
+
# --------------------------------------------------
|
14
|
+
# Signal Handling
|
15
|
+
# --------------------------------------------------
|
16
|
+
# Ctrl-\
|
17
|
+
Signal.trap('QUIT') do
|
18
|
+
puts " --- Running all tests ---\n\n"
|
19
|
+
system "ruby -rubygems test/test_swap.rb"
|
20
|
+
end
|
21
|
+
|
22
|
+
# Ctrl-C
|
23
|
+
Signal.trap('INT') { abort("\n") }
|
24
|
+
|
data/swap.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
spec = Gem::Specification.new do |s|
|
2
|
+
s.name = 'swap'
|
3
|
+
s.version = '0.2'
|
4
|
+
s.summary = "Organically replace/restore, stub/unstub methods"
|
5
|
+
s.description = "Organically replace/restore, stub/unstub methods."
|
6
|
+
s.author = "Martin Aumont"
|
7
|
+
s.email = 'mynyml@gmail.com'
|
8
|
+
s.homepage = 'http://mynyml.com'
|
9
|
+
s.has_rdoc = true
|
10
|
+
s.require_path = "lib"
|
11
|
+
s.files = %w[
|
12
|
+
LICENSE
|
13
|
+
README
|
14
|
+
TODO
|
15
|
+
Rakefile
|
16
|
+
lib/swap.rb
|
17
|
+
test/test_helper.rb
|
18
|
+
test/test_swap.rb
|
19
|
+
examples/simple.rb
|
20
|
+
specs.watchr
|
21
|
+
swap.gemspec
|
22
|
+
]
|
23
|
+
s.test_files = %w[
|
24
|
+
test/test_swap.rb
|
25
|
+
]
|
26
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
require 'context'
|
5
|
+
require 'matchy'
|
6
|
+
begin
|
7
|
+
require 'redgreen'
|
8
|
+
require 'ruby-debug'
|
9
|
+
require 'quietbacktrace'
|
10
|
+
rescue LoadError, RuntimeError
|
11
|
+
# pass
|
12
|
+
end
|
13
|
+
|
14
|
+
$:.unshift Pathname(__FILE__).dirname.parent + 'lib'
|
15
|
+
|
16
|
+
require 'swap'
|
data/test/test_swap.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'test/test_helper'
|
2
|
+
|
3
|
+
class SwapTest < Test::Unit::TestCase
|
4
|
+
context "Swap" do
|
5
|
+
before do
|
6
|
+
class ::Kitty
|
7
|
+
extend Swappable
|
8
|
+
def say() 'ohaie' end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
after do
|
12
|
+
Object.class_eval { remove_const(:Kitty) }
|
13
|
+
defined?(::Kitty).should be(nil)
|
14
|
+
end
|
15
|
+
context "replacing instance method" do
|
16
|
+
test "accepts string" do
|
17
|
+
Kitty.swap!(:say, %( 'kthxbai'.reverse ))
|
18
|
+
Kitty.new.say.should be('kthxbai'.reverse)
|
19
|
+
end
|
20
|
+
test "accepts block" do
|
21
|
+
Kitty.swap!(:say) { 'kthxbai' }
|
22
|
+
Kitty.new.say.should be('kthxbai')
|
23
|
+
end
|
24
|
+
test "accepts proc" do
|
25
|
+
Kitty.swap!(:say, lambda { 'kthxbai' })
|
26
|
+
Kitty.new.say.should be('kthxbai')
|
27
|
+
end
|
28
|
+
test "accepts unbound method" do
|
29
|
+
class ::Kitty
|
30
|
+
def bai() 'kthxbai' end
|
31
|
+
end
|
32
|
+
Kitty.swap!(:say, Kitty.instance_method(:bai))
|
33
|
+
Kitty.new.say.should be('kthxbai')
|
34
|
+
end
|
35
|
+
end
|
36
|
+
context "reseting original methods" do
|
37
|
+
test "restores single method" do
|
38
|
+
Kitty.swap!(:say) { 'kthxbai' }
|
39
|
+
Kitty.new.say.should be('kthxbai')
|
40
|
+
Kitty.unswap!(:say)
|
41
|
+
Kitty.new.say.should be('ohaie')
|
42
|
+
end
|
43
|
+
test "restores all methods" do
|
44
|
+
class ::Kitty
|
45
|
+
def bai() 'kthxbai' end
|
46
|
+
end
|
47
|
+
Kitty.swap!(:say) { 'o.0' }
|
48
|
+
Kitty.swap!(:bai) { 'zZzz' }
|
49
|
+
Kitty.new.say.should be('o.0' )
|
50
|
+
Kitty.new.bai.should be('zZzz')
|
51
|
+
Kitty.unswap!
|
52
|
+
Kitty.new.say.should be('ohaie')
|
53
|
+
Kitty.new.bai.should be('kthxbai')
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: swap
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.2"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Martin Aumont
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-09-23 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Organically replace/restore, stub/unstub methods.
|
17
|
+
email: mynyml@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- LICENSE
|
26
|
+
- README
|
27
|
+
- TODO
|
28
|
+
- Rakefile
|
29
|
+
- lib/swap.rb
|
30
|
+
- test/test_helper.rb
|
31
|
+
- test/test_swap.rb
|
32
|
+
- examples/simple.rb
|
33
|
+
- specs.watchr
|
34
|
+
- swap.gemspec
|
35
|
+
has_rdoc: true
|
36
|
+
homepage: http://mynyml.com
|
37
|
+
licenses: []
|
38
|
+
|
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.5
|
60
|
+
signing_key:
|
61
|
+
specification_version: 3
|
62
|
+
summary: Organically replace/restore, stub/unstub methods
|
63
|
+
test_files:
|
64
|
+
- test/test_swap.rb
|