swizzle 1.0.0
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/.gitignore +9 -0
- data/.rubocop.yml +19 -0
- data/.travis.yml +14 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/README.md +92 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/swizzle.rb +84 -0
- data/lib/swizzle/version.rb +3 -0
- data/swizzle.gemspec +29 -0
- metadata +113 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: bf831765a4df0ad0eaa70ddc51ceb1a3d0ef1270
|
|
4
|
+
data.tar.gz: '079e3f02ebe6759fa72a6733643ccf768e94d20d'
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 21003cadbe2b1c9743c919962cd5ec731c5a203350ea0e6bcfa301b1a422b3638177c821046e2d8048428f29da31687d34e19440de6aeabe1590e39727e1aeff
|
|
7
|
+
data.tar.gz: 01c6df1e47416b4db29632d350712e31a340abc2701eac9a80f72c3a3f997f49aac17cddcad0730b975a63a07b16d8c60dba53490677e198995b6e1296eede91
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# This is the configuration used to check the rubocop source code.
|
|
2
|
+
|
|
3
|
+
AllCops:
|
|
4
|
+
TargetRubyVersion: 2.0
|
|
5
|
+
|
|
6
|
+
Style/StringLiterals:
|
|
7
|
+
EnforcedStyle: double_quotes
|
|
8
|
+
|
|
9
|
+
LineLength:
|
|
10
|
+
Max: 110
|
|
11
|
+
|
|
12
|
+
Documentation:
|
|
13
|
+
Enabled: false
|
|
14
|
+
|
|
15
|
+
Metrics/MethodLength:
|
|
16
|
+
Max: 20
|
|
17
|
+
|
|
18
|
+
Metrics/AbcSize:
|
|
19
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017 masato-hi
|
|
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.
|
data/README.md
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
|
|
2
|
+
# Swizzle
|
|
3
|
+
|
|
4
|
+
swizzle is a gem for easy to do method swizzling to class methods, instance methods.
|
|
5
|
+
|
|
6
|
+
[](https://travis-ci.org/masato-hi/swizzle)
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
Add this line to your application's Gemfile:
|
|
11
|
+
|
|
12
|
+
```ruby
|
|
13
|
+
gem 'swizzle'
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
And then execute:
|
|
17
|
+
|
|
18
|
+
$ bundle install
|
|
19
|
+
|
|
20
|
+
Or install it yourself as:
|
|
21
|
+
|
|
22
|
+
$ gem install swizzle
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
```ruby
|
|
27
|
+
# include Swizzle
|
|
28
|
+
class SwizzleKlass
|
|
29
|
+
include Swizzle
|
|
30
|
+
|
|
31
|
+
def self.klass_method
|
|
32
|
+
"klass_method"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def self.swizzle_klass_method
|
|
36
|
+
"swizzled_klass_method"
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def instance_method
|
|
40
|
+
"instance_method"
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def swizzle_instance_method
|
|
44
|
+
"swizzled_instance_method"
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# before method swizzling.
|
|
49
|
+
SwizzleKlass.klass_method #=> "klass_method"
|
|
50
|
+
SwizzleKlass.new.instance_method #=> "instance_method"
|
|
51
|
+
|
|
52
|
+
# do method swizzling.
|
|
53
|
+
SwizzleKlass.swizzle!
|
|
54
|
+
|
|
55
|
+
# after method swizzling.
|
|
56
|
+
SwizzleKlass.klass_method #=> "swizzled_klass_method"
|
|
57
|
+
SwizzleKlass.new.instance_method #=> "swizzled_instance_method"
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Example
|
|
61
|
+
```ruby
|
|
62
|
+
class CreditCardPaymentService
|
|
63
|
+
include Swizzle
|
|
64
|
+
|
|
65
|
+
def authorize!
|
|
66
|
+
# payment request for production.
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def swizzle_authorize!
|
|
70
|
+
# return dummy response.
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
CreditCardPaymentService.swizzle! unless Rails.env.production?
|
|
75
|
+
|
|
76
|
+
CreditCardPaymentService.authorize! #=> dummy response.
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Development
|
|
80
|
+
|
|
81
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
82
|
+
|
|
83
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
84
|
+
|
|
85
|
+
## Contributing
|
|
86
|
+
|
|
87
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/masato-hi/swizzle.
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
## License
|
|
91
|
+
|
|
92
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "swizzle"
|
|
5
|
+
|
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
+
|
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
+
# require "pry"
|
|
11
|
+
# Pry.start
|
|
12
|
+
|
|
13
|
+
require "irb"
|
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/swizzle.rb
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
require "swizzle/version"
|
|
2
|
+
|
|
3
|
+
module Swizzle
|
|
4
|
+
module ClassMethods
|
|
5
|
+
DEFAULT_SWIZZLE_PREFIX = "swizzle_".freeze
|
|
6
|
+
|
|
7
|
+
def swizzled_class_methods
|
|
8
|
+
@swizzled_class_methods ||= {}
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def swizzled_instance_methods
|
|
12
|
+
@swizzled_instance_methods ||= {}
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def swizzled?
|
|
16
|
+
@swizzled ||= false
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def swizzle!
|
|
20
|
+
swizzle_class_methods!
|
|
21
|
+
swizzle_instance_methods!
|
|
22
|
+
|
|
23
|
+
@swizzled = true
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
private
|
|
27
|
+
|
|
28
|
+
def swizzle_prefix(prefix = nil)
|
|
29
|
+
@swizzle_prefix ||= DEFAULT_SWIZZLE_PREFIX
|
|
30
|
+
@swizzle_prefix = prefix unless prefix.nil?
|
|
31
|
+
@swizzle_prefix
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def swizzle_prefix_regexp
|
|
35
|
+
Regexp.new("\\A#{swizzle_prefix}")
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def swizzle_class_methods!
|
|
39
|
+
@swizzled_class_methods ||= {}
|
|
40
|
+
|
|
41
|
+
swizzle_method_names = methods.select do |method_name|
|
|
42
|
+
method_name =~ swizzle_prefix_regexp
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
swizzle_method_names.each do |swizzle_method_name|
|
|
46
|
+
method_name = swizzle_method_name.to_s.sub(swizzle_prefix_regexp, "").to_sym
|
|
47
|
+
next if @swizzled_class_methods.keys.include?(method_name)
|
|
48
|
+
|
|
49
|
+
swizzle_method = method(swizzle_method_name)
|
|
50
|
+
next unless singleton_class.method_defined?(method_name)
|
|
51
|
+
singleton_class.send(:remove_method, method_name)
|
|
52
|
+
singleton_class.send(:define_method, method_name, swizzle_method)
|
|
53
|
+
@swizzled_class_methods[method_name] = swizzle_method_name
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def swizzle_instance_methods!
|
|
58
|
+
@swizzled_instance_methods ||= {}
|
|
59
|
+
|
|
60
|
+
swizzle_instance_method_names = instance_methods.select do |instance_method_name|
|
|
61
|
+
instance_method_name =~ swizzle_prefix_regexp
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
swizzle_instance_method_names.each do |swizzle_instance_method_name|
|
|
65
|
+
instance_method_name = swizzle_instance_method_name.to_s.sub(swizzle_prefix_regexp, "").to_sym
|
|
66
|
+
next if @swizzled_instance_methods.keys.include?(instance_method_name)
|
|
67
|
+
|
|
68
|
+
swizzle_instance_method = instance_method(swizzle_instance_method_name)
|
|
69
|
+
class_eval do
|
|
70
|
+
if method_defined?(instance_method_name)
|
|
71
|
+
remove_method(instance_method_name)
|
|
72
|
+
define_method(instance_method_name, swizzle_instance_method)
|
|
73
|
+
@swizzled_instance_methods[instance_method_name] = swizzle_instance_method_name
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
extend ClassMethods
|
|
80
|
+
|
|
81
|
+
def self.included(klass)
|
|
82
|
+
klass.extend ClassMethods
|
|
83
|
+
end
|
|
84
|
+
end
|
data/swizzle.gemspec
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
|
|
3
|
+
lib = File.expand_path("../lib", __FILE__)
|
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
5
|
+
require "swizzle/version"
|
|
6
|
+
|
|
7
|
+
Gem::Specification.new do |spec|
|
|
8
|
+
spec.name = "swizzle"
|
|
9
|
+
spec.version = Swizzle::VERSION
|
|
10
|
+
spec.authors = ["masato"]
|
|
11
|
+
spec.email = ["masato.hirahata@gmail.com"]
|
|
12
|
+
|
|
13
|
+
spec.summary = "swizzle is a gem for easy to do method swizzling."
|
|
14
|
+
spec.description = "swizzle is a gem for easy to do method swizzling to class methods, instance methods."
|
|
15
|
+
spec.homepage = "https://github.com/masato-hi/swizzle"
|
|
16
|
+
spec.license = "MIT"
|
|
17
|
+
|
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
|
19
|
+
f.match(%r{^(test|spec|features)/})
|
|
20
|
+
end
|
|
21
|
+
spec.bindir = "exe"
|
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
23
|
+
spec.require_paths = ["lib"]
|
|
24
|
+
|
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
27
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
|
28
|
+
spec.add_development_dependency "rubocop", "~> 0.49.1"
|
|
29
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: swizzle
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- masato
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2017-08-26 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.14'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.14'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rake
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '10.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '10.0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: minitest
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '5.0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '5.0'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: rubocop
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: 0.49.1
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: 0.49.1
|
|
69
|
+
description: swizzle is a gem for easy to do method swizzling to class methods, instance
|
|
70
|
+
methods.
|
|
71
|
+
email:
|
|
72
|
+
- masato.hirahata@gmail.com
|
|
73
|
+
executables: []
|
|
74
|
+
extensions: []
|
|
75
|
+
extra_rdoc_files: []
|
|
76
|
+
files:
|
|
77
|
+
- ".gitignore"
|
|
78
|
+
- ".rubocop.yml"
|
|
79
|
+
- ".travis.yml"
|
|
80
|
+
- Gemfile
|
|
81
|
+
- LICENSE
|
|
82
|
+
- README.md
|
|
83
|
+
- Rakefile
|
|
84
|
+
- bin/console
|
|
85
|
+
- bin/setup
|
|
86
|
+
- lib/swizzle.rb
|
|
87
|
+
- lib/swizzle/version.rb
|
|
88
|
+
- swizzle.gemspec
|
|
89
|
+
homepage: https://github.com/masato-hi/swizzle
|
|
90
|
+
licenses:
|
|
91
|
+
- MIT
|
|
92
|
+
metadata: {}
|
|
93
|
+
post_install_message:
|
|
94
|
+
rdoc_options: []
|
|
95
|
+
require_paths:
|
|
96
|
+
- lib
|
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
98
|
+
requirements:
|
|
99
|
+
- - ">="
|
|
100
|
+
- !ruby/object:Gem::Version
|
|
101
|
+
version: '0'
|
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
103
|
+
requirements:
|
|
104
|
+
- - ">="
|
|
105
|
+
- !ruby/object:Gem::Version
|
|
106
|
+
version: '0'
|
|
107
|
+
requirements: []
|
|
108
|
+
rubyforge_project:
|
|
109
|
+
rubygems_version: 2.6.11
|
|
110
|
+
signing_key:
|
|
111
|
+
specification_version: 4
|
|
112
|
+
summary: swizzle is a gem for easy to do method swizzling.
|
|
113
|
+
test_files: []
|