funswap 0.0.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.
- checksums.yaml +7 -0
- data/LICENSE +22 -0
- data/README.md +1 -0
- data/Rakefile +9 -0
- data/lib/funswap.rb +25 -0
- data/test/test_funswap.rb +77 -0
- metadata +48 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 31c687f4eb556780fb31955fce158b36b72cab30
|
|
4
|
+
data.tar.gz: 77eb45e1aed810face881be0326484f2cbed8129
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 7ec4fe5fa78218940d6e74d6b1a36835b1efb468ee0f5024ff0863c22224846b7e0eb081c586fa3da7be5ed591f0073b8823c4770d7b3fd457264e29b0e862c0
|
|
7
|
+
data.tar.gz: 7ecdcc479e7bc045b081f0805c6cb5657d29551e7cd3ab314c3e34bdf82aff232758a376f62182d466571d5bbc50672cfc58e70d8734f45762b8e2b434017f7c
|
data/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2015 Hitesh Jasani
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
data/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# funswap
|
data/Rakefile
ADDED
data/lib/funswap.rb
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
|
|
2
|
+
module FunSwap
|
|
3
|
+
# Swap module function in method and return the old function.
|
|
4
|
+
#
|
|
5
|
+
# mod:module -> fn_name:symbol -> fn:proc -> old_fn:proc
|
|
6
|
+
#
|
|
7
|
+
def self.swap(mod, fn_name, fn)
|
|
8
|
+
old_fn = mod.method(fn_name).to_proc
|
|
9
|
+
begin
|
|
10
|
+
eigenclass = class << mod; self; end
|
|
11
|
+
eigenclass.class_eval { remove_method fn_name }
|
|
12
|
+
rescue NameError
|
|
13
|
+
end
|
|
14
|
+
mod.class.send(:define_method, fn_name, fn)
|
|
15
|
+
old_fn
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def self.with_fn(mod, fn_name, fn, &blk)
|
|
19
|
+
old_fn = swap(mod, fn_name, fn)
|
|
20
|
+
blk.call()
|
|
21
|
+
ensure
|
|
22
|
+
swap(mod, fn_name, old_fn)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
require "minitest/autorun"
|
|
2
|
+
require "funswap"
|
|
3
|
+
|
|
4
|
+
module XYZ
|
|
5
|
+
def self.add2(x)
|
|
6
|
+
x + 2
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
module_function
|
|
10
|
+
def self.mul2(x)
|
|
11
|
+
x * 2
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
module ABC
|
|
16
|
+
module DEF
|
|
17
|
+
def self.add7(x)
|
|
18
|
+
x + 7
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
module_function
|
|
22
|
+
def self.mul5(x)
|
|
23
|
+
x * 5
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
class FunSwapTest < Minitest::Test
|
|
29
|
+
def test_xyz_add2_normal
|
|
30
|
+
assert_equal(4, XYZ.add2(2))
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def test_xyz_add2_swap_add3
|
|
34
|
+
assert_equal(4, XYZ.add2(2))
|
|
35
|
+
FunSwap.with_fn(XYZ, :add2, ->(x) { x + 3 }) do
|
|
36
|
+
assert_equal(5, XYZ.add2(2))
|
|
37
|
+
end
|
|
38
|
+
assert_equal(4, XYZ.add2(2))
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def test_xyz_mul2_normal
|
|
42
|
+
assert_equal(6, XYZ.mul2(3))
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def test_xyz_mul2_swap_mul3
|
|
46
|
+
assert_equal(6, XYZ.mul2(3))
|
|
47
|
+
FunSwap.with_fn(XYZ, :mul2, ->(x) { x * 3 }) do
|
|
48
|
+
assert_equal(9, XYZ.mul2(3))
|
|
49
|
+
end
|
|
50
|
+
assert_equal(6, XYZ.mul2(3))
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def test_abcdef_add7_normal
|
|
54
|
+
assert_equal(9, ABC::DEF.add7(2))
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def test_abcdef_add7_swap_add12
|
|
58
|
+
assert_equal(9, ABC::DEF.add7(2))
|
|
59
|
+
FunSwap.with_fn(ABC::DEF, :add7, ->(x) { x + 12 }) do
|
|
60
|
+
assert_equal(14, ABC::DEF.add7(2))
|
|
61
|
+
end
|
|
62
|
+
assert_equal(9, ABC::DEF.add7(2))
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def test_abcdef_mul5_normal
|
|
66
|
+
assert_equal(15, ABC::DEF.mul5(3))
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def test_abcdef_mul5_swap_mul9
|
|
70
|
+
assert_equal(15, ABC::DEF.mul5(3))
|
|
71
|
+
FunSwap.with_fn(ABC::DEF, :mul5, ->(x) { x * 9 }) do
|
|
72
|
+
assert_equal(27, ABC::DEF.mul5(3))
|
|
73
|
+
end
|
|
74
|
+
assert_equal(15, ABC::DEF.mul5(3))
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
metadata
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: funswap
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Hitesh Jasani
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2015-05-08 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Swapping module functions for fun and profit
|
|
14
|
+
email: hitesh DoT jasani aT gmail dot com
|
|
15
|
+
executables: []
|
|
16
|
+
extensions: []
|
|
17
|
+
extra_rdoc_files: []
|
|
18
|
+
files:
|
|
19
|
+
- LICENSE
|
|
20
|
+
- README.md
|
|
21
|
+
- Rakefile
|
|
22
|
+
- lib/funswap.rb
|
|
23
|
+
- test/test_funswap.rb
|
|
24
|
+
homepage: https://github.com/hiteshjasani/funswap
|
|
25
|
+
licenses:
|
|
26
|
+
- MIT
|
|
27
|
+
metadata: {}
|
|
28
|
+
post_install_message:
|
|
29
|
+
rdoc_options: []
|
|
30
|
+
require_paths:
|
|
31
|
+
- lib
|
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
33
|
+
requirements:
|
|
34
|
+
- - ">="
|
|
35
|
+
- !ruby/object:Gem::Version
|
|
36
|
+
version: '0'
|
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
38
|
+
requirements:
|
|
39
|
+
- - ">="
|
|
40
|
+
- !ruby/object:Gem::Version
|
|
41
|
+
version: '0'
|
|
42
|
+
requirements: []
|
|
43
|
+
rubyforge_project:
|
|
44
|
+
rubygems_version: 2.4.5
|
|
45
|
+
signing_key:
|
|
46
|
+
specification_version: 4
|
|
47
|
+
summary: Function swapping
|
|
48
|
+
test_files: []
|