mynyml-swap 0.1.1 → 0.2
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/README +14 -12
- data/Rakefile +22 -46
- data/examples/simple.rb +30 -10
- data/lib/swap.rb +26 -15
- data/specs.watchr +24 -0
- data/swap.gemspec +26 -64
- data/test/test_helper.rb +7 -1
- data/test/test_swap.rb +2 -4
- metadata +14 -15
data/README
CHANGED
@@ -4,27 +4,29 @@ Swap allows dynamically replacing and restoring methods.
|
|
4
4
|
Useful as a stubbing device, as it allows unstubbing as well.
|
5
5
|
|
6
6
|
==== Examples
|
7
|
-
|
8
|
-
require '
|
7
|
+
|
8
|
+
require 'swap'
|
9
9
|
|
10
10
|
class User
|
11
|
-
|
11
|
+
extend Swappable
|
12
|
+
|
13
|
+
attr_writer :name
|
14
|
+
def name
|
15
|
+
@name
|
16
|
+
end
|
12
17
|
end
|
18
|
+
|
13
19
|
user = User.new
|
14
20
|
user.name = 'martin'
|
15
|
-
|
16
|
-
|
17
|
-
puts Ruby2Ruby.translate(User, :name)
|
21
|
+
user.name #=> 'martin'
|
18
22
|
|
19
23
|
User.swap!(:name) { @name.reverse }
|
20
|
-
|
21
|
-
|
22
|
-
puts Ruby2Ruby.translate(User, :name)
|
24
|
+
user.name #=> 'nitram'
|
23
25
|
|
24
26
|
User.unswap!(:name)
|
25
|
-
|
27
|
+
user.name #=> 'martin'
|
26
28
|
|
27
|
-
|
29
|
+
Calling #unswap! without argument will restore all swapped methods.
|
28
30
|
|
31
|
+
See also examples/simple.rb
|
29
32
|
|
30
|
-
Calling #unswap! without argument will restore all swapped methods.
|
data/Rakefile
CHANGED
@@ -1,49 +1,25 @@
|
|
1
|
-
require 'rake/
|
2
|
-
|
3
|
-
require '
|
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['**/*']
|
1
|
+
require 'rake/rdoctask'
|
2
|
+
begin
|
3
|
+
require 'yard'
|
4
|
+
rescue LoadError, RuntimeError
|
24
5
|
end
|
25
6
|
|
26
|
-
|
27
|
-
|
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
|
28
25
|
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/examples/simple.rb
CHANGED
@@ -1,27 +1,47 @@
|
|
1
|
-
|
2
|
-
|
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'
|
3
10
|
require 'ruby2ruby'
|
4
|
-
root = Pathname(__FILE__).dirname.parent
|
5
|
-
require root.join('lib/swap')
|
6
11
|
|
7
12
|
class User
|
13
|
+
extend Swappable
|
8
14
|
attr_writer :name
|
9
15
|
def name
|
10
16
|
@name
|
11
17
|
end
|
12
18
|
end
|
19
|
+
|
13
20
|
user = User.new
|
14
21
|
user.name = 'martin'
|
15
|
-
puts user.name
|
22
|
+
puts user.name
|
23
|
+
#=> 'martin'
|
16
24
|
|
17
|
-
puts
|
25
|
+
puts user.method(:name).to_ruby
|
26
|
+
#=> def name
|
27
|
+
#=> @name
|
28
|
+
#=> end
|
18
29
|
|
19
30
|
User.swap!(:name) { @name.reverse }
|
20
|
-
puts user.name
|
31
|
+
puts user.name
|
32
|
+
#=> 'nitram'
|
21
33
|
|
22
|
-
puts
|
34
|
+
puts user.method(:name).to_ruby
|
35
|
+
#=> def name
|
36
|
+
#=> @name.reverse
|
37
|
+
#=> end
|
23
38
|
|
24
39
|
User.unswap!(:name)
|
25
|
-
puts user.name
|
40
|
+
puts user.name
|
41
|
+
#=> 'martin'
|
42
|
+
|
43
|
+
puts user.method(:name).to_ruby
|
44
|
+
#=> def name
|
45
|
+
#=> @name
|
46
|
+
#=> end
|
26
47
|
|
27
|
-
puts Ruby2Ruby.translate(User, :name)
|
data/lib/swap.rb
CHANGED
@@ -1,4 +1,13 @@
|
|
1
|
-
|
1
|
+
|
2
|
+
# Swappable mixin.
|
3
|
+
#
|
4
|
+
# ===== Examples
|
5
|
+
#
|
6
|
+
# Class User
|
7
|
+
# extend Swappable
|
8
|
+
# end
|
9
|
+
#
|
10
|
+
module Swappable
|
2
11
|
|
3
12
|
# Swaps a method for another
|
4
13
|
#
|
@@ -15,10 +24,11 @@ class Class
|
|
15
24
|
# self
|
16
25
|
#
|
17
26
|
# ==== Examples
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
21
|
-
#
|
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))
|
22
32
|
#
|
23
33
|
def swap!(name, code=nil, &block)
|
24
34
|
name = name.to_sym
|
@@ -49,18 +59,19 @@ class Class
|
|
49
59
|
# self
|
50
60
|
#
|
51
61
|
# ==== Examples
|
52
|
-
# class User
|
53
|
-
# attr_accessor :name
|
54
|
-
# end
|
55
|
-
# user = User.new
|
56
|
-
# user.name = 'martin'
|
57
|
-
# puts user.name #=> 'martin'
|
58
62
|
#
|
59
|
-
# User
|
60
|
-
#
|
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'
|
61
72
|
#
|
62
|
-
#
|
63
|
-
#
|
73
|
+
# User.unswap!(:name)
|
74
|
+
# puts user.name #=> 'martin'
|
64
75
|
#
|
65
76
|
def unswap!(name=nil)
|
66
77
|
if name
|
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
CHANGED
@@ -1,64 +1,26 @@
|
|
1
|
-
|
2
|
-
name
|
3
|
-
version
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
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
|
-
|
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
CHANGED
@@ -1,10 +1,16 @@
|
|
1
|
+
require 'pathname'
|
1
2
|
require 'test/unit'
|
2
|
-
|
3
|
+
|
3
4
|
require 'context'
|
4
5
|
require 'matchy'
|
5
6
|
begin
|
7
|
+
require 'redgreen'
|
6
8
|
require 'ruby-debug'
|
7
9
|
require 'quietbacktrace'
|
8
10
|
rescue LoadError, RuntimeError
|
9
11
|
# pass
|
10
12
|
end
|
13
|
+
|
14
|
+
$:.unshift Pathname(__FILE__).dirname.parent + 'lib'
|
15
|
+
|
16
|
+
require 'swap'
|
data/test/test_swap.rb
CHANGED
@@ -1,12 +1,10 @@
|
|
1
|
-
require '
|
2
|
-
root = Pathname(__FILE__).dirname.parent
|
3
|
-
require root.join('test/test_helper')
|
4
|
-
require root.join('lib/swap')
|
1
|
+
require 'test/test_helper'
|
5
2
|
|
6
3
|
class SwapTest < Test::Unit::TestCase
|
7
4
|
context "Swap" do
|
8
5
|
before do
|
9
6
|
class ::Kitty
|
7
|
+
extend Swappable
|
10
8
|
def say() 'ohaie' end
|
11
9
|
end
|
12
10
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mynyml-swap
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: "0.2"
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Martin Aumont
|
@@ -9,11 +9,11 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-05-16 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
16
|
-
description:
|
16
|
+
description: Organically replace/restore, stub/unstub methods.
|
17
17
|
email: mynyml@gmail.com
|
18
18
|
executables: []
|
19
19
|
|
@@ -22,20 +22,19 @@ extensions: []
|
|
22
22
|
extra_rdoc_files: []
|
23
23
|
|
24
24
|
files:
|
25
|
-
- lib
|
26
|
-
- lib/swap.rb
|
27
|
-
- examples
|
28
|
-
- examples/simple.rb
|
29
25
|
- LICENSE
|
30
26
|
- README
|
27
|
+
- TODO
|
31
28
|
- Rakefile
|
32
|
-
-
|
33
|
-
- test/test_swap.rb
|
29
|
+
- lib/swap.rb
|
34
30
|
- test/test_helper.rb
|
31
|
+
- test/test_swap.rb
|
32
|
+
- examples/simple.rb
|
33
|
+
- specs.watchr
|
35
34
|
- swap.gemspec
|
36
|
-
- TODO
|
37
35
|
has_rdoc: true
|
38
|
-
homepage:
|
36
|
+
homepage: http://mynyml.com
|
37
|
+
licenses:
|
39
38
|
post_install_message:
|
40
39
|
rdoc_options: []
|
41
40
|
|
@@ -56,9 +55,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
56
55
|
requirements: []
|
57
56
|
|
58
57
|
rubyforge_project:
|
59
|
-
rubygems_version: 1.
|
58
|
+
rubygems_version: 1.3.5
|
60
59
|
signing_key:
|
61
60
|
specification_version: 2
|
62
|
-
summary:
|
63
|
-
test_files:
|
64
|
-
|
61
|
+
summary: Organically replace/restore, stub/unstub methods
|
62
|
+
test_files:
|
63
|
+
- test/test_swap.rb
|