invisible 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +28 -3
- data/lib/invisible/CHANGELOG.md +9 -0
- data/lib/invisible/version.rb +1 -1
- data/lib/invisible.rb +22 -5
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ec6e3dc7bce460129f162a4d86d209e6bcb60465cbed9a27cdd72be8ab39a153
|
4
|
+
data.tar.gz: bb55e63585517a844219039697c213e419d408a6d38faee9edcd9fd164824d52
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c7c27b7f1ac6b9f10f0c62129fa61361c526f07a780263dc5e0d2b64579baa025542002354094b405ad3913dad07aba26809f188619ecf307a3d6cdbe962e52f
|
7
|
+
data.tar.gz: a0fc24dd164097118d0a214e5004ec84d26ae5686ad5cd465f5b00ccb2911f9232890550e7d8b5d4c6e16404f0a64d7080302439ed0f50f36f365cf8a0b3d84b
|
data/README.md
CHANGED
@@ -1,10 +1,18 @@
|
|
1
1
|
# Invisible
|
2
2
|
|
3
|
-
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/invisible.svg)][gem]
|
4
|
+
[![Build Status](https://travis-ci.com/shioyama/invisible.svg?branch=master)][travis]
|
5
|
+
|
6
|
+
[gem]: https://rubygems.org/gems/invisible
|
7
|
+
[travis]: https://travis-ci.com/shioyama/invisible
|
8
|
+
|
9
|
+
Public? Private? Protected? Who cares! I just want to monkey patch that shit!
|
10
|
+
|
11
|
+
No fear: Invisible has your back! This little ten-line gem does away with the problem of maintaining original method visibility, so you can get on with your monkey-patching mayhem.
|
4
12
|
|
5
13
|
## Usage
|
6
14
|
|
7
|
-
Suppose you are defining a module which will override a bunch of methods from some class (or module). Simply `extend Invisible` and you can ignore checking whether those methods are public, protected or private -- Invisible will take care of that for you
|
15
|
+
Suppose you are defining a module which will override a bunch of methods from some class (or module). Simply `extend Invisible` and you can ignore checking whether those methods are public, protected or private -- Invisible will take care of that for you.
|
8
16
|
|
9
17
|
Suppose this is the class we are overriding:
|
10
18
|
|
@@ -48,7 +56,7 @@ module Foo
|
|
48
56
|
end
|
49
57
|
```
|
50
58
|
|
51
|
-
Normally, without Invisible, we would have just made methods that were previously `private` become `public`. But Invisible checks the original visibility and ensures that when the module is included, methods which were originally private or protected stay that way.
|
59
|
+
Normally, without Invisible, we would have just made methods that were previously `private` or `protected` become `public`. But Invisible checks the original visibility and ensures that when the module is included, methods which were originally private or protected stay that way.
|
52
60
|
|
53
61
|
```ruby
|
54
62
|
class MyClass < Base
|
@@ -69,6 +77,23 @@ instance.private_method # raises NoMethodError
|
|
69
77
|
instance.send(:private_method) #=> 'privatefoo'
|
70
78
|
```
|
71
79
|
|
80
|
+
Also works with `prepend`:
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
class MyClass < Base
|
84
|
+
prepend Foo
|
85
|
+
|
86
|
+
private
|
87
|
+
def private_method
|
88
|
+
super + 'bar'
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
instance = MyClass.new
|
93
|
+
instance.private_method # raises NoMethodError
|
94
|
+
instance.send(:private_method) #=> 'privatebarfoo'
|
95
|
+
```
|
96
|
+
|
72
97
|
## License
|
73
98
|
|
74
99
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/lib/invisible/version.rb
CHANGED
data/lib/invisible.rb
CHANGED
@@ -60,12 +60,29 @@ maintain their original visibility.
|
|
60
60
|
|
61
61
|
=end
|
62
62
|
def append_features(base)
|
63
|
-
|
64
|
-
|
63
|
+
sync_visibility(base) { super }
|
64
|
+
end
|
65
|
+
|
66
|
+
def prepend_features(base)
|
67
|
+
return super if synced
|
68
|
+
|
69
|
+
sync_visibility(base, mod = dup)
|
70
|
+
mod.synced = true
|
71
|
+
base.prepend mod
|
72
|
+
end
|
73
|
+
|
74
|
+
protected
|
75
|
+
attr_accessor :synced
|
76
|
+
|
77
|
+
private
|
78
|
+
|
79
|
+
def sync_visibility(source, target = source)
|
80
|
+
private_methods = instance_methods.select { |m| source.private_method_defined? m }
|
81
|
+
protected_methods = instance_methods.select { |m| source.protected_method_defined? m }
|
65
82
|
|
66
|
-
|
83
|
+
yield if block_given?
|
67
84
|
|
68
|
-
private_methods.each { |method_name|
|
69
|
-
protected_methods.each { |method_name|
|
85
|
+
private_methods.each { |method_name| target.class_eval { private method_name } }
|
86
|
+
protected_methods.each { |method_name| target.class_eval { protected method_name } }
|
70
87
|
end
|
71
88
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: invisible
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Salzberg
|
@@ -52,6 +52,7 @@ files:
|
|
52
52
|
- README.md
|
53
53
|
- Rakefile
|
54
54
|
- lib/invisible.rb
|
55
|
+
- lib/invisible/CHANGELOG.md
|
55
56
|
- lib/invisible/version.rb
|
56
57
|
homepage: https://github.com/shioyama/invisible
|
57
58
|
licenses:
|
@@ -74,8 +75,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
74
75
|
- !ruby/object:Gem::Version
|
75
76
|
version: '0'
|
76
77
|
requirements: []
|
77
|
-
|
78
|
-
rubygems_version: 2.7.6.2
|
78
|
+
rubygems_version: 3.0.6
|
79
79
|
signing_key:
|
80
80
|
specification_version: 4
|
81
81
|
summary: Override methods while maintaining their original visibility.
|