method_watcher 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.travis.yml +6 -0
- data/README.md +30 -2
- data/Rakefile +4 -0
- data/lib/method_watcher.rb +24 -7
- data/lib/method_watcher/version.rb +1 -1
- data/spec/method_watcher_spec.rb +14 -3
- metadata +12 -19
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 94ffebd1dd2af6d564b2333d52438e7501b68fbe
|
4
|
+
data.tar.gz: f420c4f3d4f52cc15d342429e40c5b1ba78b5fd5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9ca24c51345a35a4ea80651d301460f7fe211d80c5e3b2fb19641dd868573d73dc0a3f806898bcc222ba94a276010b9f4b0d7d078b649d5918ef0f81a0c0c22e
|
7
|
+
data.tar.gz: 63fb3299fefb13414a3ea8aa1a80d5c53cf401b1ba5c7bc7729efdfa54a5750993947b1a1e9615d6990a5cf07c7a8855eeec163af77055349d65a3e813f4c500
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
# MethodWatcher
|
2
2
|
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/method_watcher.png)](http://badge.fury.io/rb/method_watcher)
|
4
|
+
[![Build Status](https://travis-ci.org/jjyr/method_watcher.png?branch=master)](https://travis-ci.org/jjyr/method_watcher)
|
5
|
+
|
3
6
|
Provide method\_overriding callback.
|
4
7
|
|
5
8
|
## Installation
|
@@ -30,6 +33,7 @@ class A
|
|
30
33
|
end
|
31
34
|
|
32
35
|
watch_methods :foo, :bar
|
36
|
+
unwatch_methods :bar
|
33
37
|
end
|
34
38
|
|
35
39
|
class B < A
|
@@ -38,12 +42,36 @@ class B < A
|
|
38
42
|
end
|
39
43
|
|
40
44
|
#you will get a warning:
|
41
|
-
#method A#foo is overridden
|
45
|
+
# => method A#foo is overridden
|
42
46
|
|
43
47
|
#you can define you own behavior when method overriding.
|
44
48
|
#in class A:
|
45
49
|
def self.method_overriding method
|
46
|
-
|
50
|
+
raise "OMG, method #{method} is overridden!!"
|
51
|
+
end
|
52
|
+
|
53
|
+
#you can also use it like ruby core methods private, protect and public
|
54
|
+
require 'method_watcher'
|
55
|
+
|
56
|
+
class A
|
57
|
+
include MethodWatcher
|
58
|
+
|
59
|
+
# if you pass no arguments, all methods below it will be watched
|
60
|
+
watch_methods
|
61
|
+
|
62
|
+
def foo
|
63
|
+
end
|
64
|
+
|
65
|
+
def foo2
|
66
|
+
end
|
67
|
+
|
68
|
+
# if you pass no arguments, it will disable watching, methods below it will not be watched
|
69
|
+
unwatch_methods
|
70
|
+
|
71
|
+
def bar
|
72
|
+
end
|
73
|
+
|
74
|
+
#in this example, methods foo, foo2 should be watched
|
47
75
|
end
|
48
76
|
```
|
49
77
|
|
data/Rakefile
CHANGED
data/lib/method_watcher.rb
CHANGED
@@ -2,17 +2,33 @@ require "method_watcher/version"
|
|
2
2
|
|
3
3
|
module MethodWatcher
|
4
4
|
class << self
|
5
|
+
def init_method_watcher mod
|
6
|
+
super_watch_methods = (mod.respond_to?(:superclass) && mod.superclass && mod.superclass.instance_variable_get("@_watch_methods")) || {}
|
7
|
+
mod.instance_variable_set "@_watch_methods", super_watch_methods.merge({})
|
8
|
+
mod.instance_variable_set "@_watch_method", false
|
9
|
+
end
|
10
|
+
|
5
11
|
def included mod
|
6
|
-
|
12
|
+
MethodWatcher.init_method_watcher mod
|
7
13
|
mod.singleton_class.class_eval do
|
8
14
|
def watch_methods *methods
|
9
|
-
methods.
|
10
|
-
|
15
|
+
if methods.empty?
|
16
|
+
@_watch_method = true
|
17
|
+
else
|
18
|
+
methods.each do |method|
|
19
|
+
@_watch_methods[method.to_sym] = self
|
20
|
+
end
|
11
21
|
end
|
12
22
|
end
|
13
23
|
|
14
|
-
def
|
15
|
-
|
24
|
+
def unwatch_methods *methods
|
25
|
+
if methods.empty?
|
26
|
+
@_watch_method = false
|
27
|
+
else
|
28
|
+
methods.each do |method|
|
29
|
+
@_watch_methods.delete method.to_sym
|
30
|
+
end
|
31
|
+
end
|
16
32
|
end
|
17
33
|
|
18
34
|
def method_added method
|
@@ -20,6 +36,8 @@ module MethodWatcher
|
|
20
36
|
if @_watch_methods.has_key?(method.to_sym)
|
21
37
|
@_watch_methods[method].method_overriding method
|
22
38
|
end
|
39
|
+
|
40
|
+
watch_methods method if @_watch_method
|
23
41
|
end
|
24
42
|
|
25
43
|
def method_overriding method
|
@@ -29,8 +47,7 @@ module MethodWatcher
|
|
29
47
|
|
30
48
|
def inherited mod
|
31
49
|
super
|
32
|
-
|
33
|
-
mod.instance_variable_set "@_watch_methods", super_watch_methods.merge({})
|
50
|
+
MethodWatcher.init_method_watcher mod
|
34
51
|
end
|
35
52
|
end
|
36
53
|
end
|
data/spec/method_watcher_spec.rb
CHANGED
@@ -28,12 +28,21 @@ class B < A
|
|
28
28
|
def foo3
|
29
29
|
end
|
30
30
|
|
31
|
+
watch_methods
|
32
|
+
def foo4
|
33
|
+
end
|
34
|
+
|
35
|
+
def foo5
|
36
|
+
end
|
37
|
+
unwatch_methods
|
38
|
+
|
31
39
|
def foo
|
32
40
|
end
|
33
41
|
|
34
42
|
@overridden_methods = []
|
35
43
|
|
36
|
-
|
44
|
+
watch_methods :foo2
|
45
|
+
unwatch_methods :foo3, :foo5
|
37
46
|
|
38
47
|
def self.method_overriding method
|
39
48
|
self.overridden_methods << method
|
@@ -42,13 +51,15 @@ class B < A
|
|
42
51
|
end
|
43
52
|
|
44
53
|
class C < B
|
45
|
-
|
54
|
+
(2..5).each do |i|
|
55
|
+
define_method "foo#{i}".to_sym do
|
56
|
+
end
|
46
57
|
end
|
47
58
|
end
|
48
59
|
|
49
60
|
describe MethodWatcher do
|
50
61
|
it 'should right!' do
|
51
62
|
A.overridden_methods.should == [:foo3, :foo]
|
52
|
-
B.overridden_methods.should == [:foo2, :test]
|
63
|
+
B.overridden_methods.should == [:foo2, :test, :foo4, :test]
|
53
64
|
end
|
54
65
|
end
|
metadata
CHANGED
@@ -1,20 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: method_watcher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.0.2
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- jjy
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2013-07-
|
11
|
+
date: 2013-07-25 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: bundler
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
17
|
- - ~>
|
20
18
|
- !ruby/object:Gem::Version
|
@@ -22,7 +20,6 @@ dependencies:
|
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
24
|
- - ~>
|
28
25
|
- !ruby/object:Gem::Version
|
@@ -30,33 +27,29 @@ dependencies:
|
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: rake
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
|
-
- -
|
31
|
+
- - '>='
|
36
32
|
- !ruby/object:Gem::Version
|
37
33
|
version: '0'
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
|
-
- -
|
38
|
+
- - '>='
|
44
39
|
- !ruby/object:Gem::Version
|
45
40
|
version: '0'
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
42
|
name: rspec
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
|
-
- -
|
45
|
+
- - '>='
|
52
46
|
- !ruby/object:Gem::Version
|
53
47
|
version: '0'
|
54
48
|
type: :development
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
|
-
- -
|
52
|
+
- - '>='
|
60
53
|
- !ruby/object:Gem::Version
|
61
54
|
version: '0'
|
62
55
|
description: Provide method_overriding callback
|
@@ -67,6 +60,7 @@ extensions: []
|
|
67
60
|
extra_rdoc_files: []
|
68
61
|
files:
|
69
62
|
- .gitignore
|
63
|
+
- .travis.yml
|
70
64
|
- Gemfile
|
71
65
|
- LICENSE.txt
|
72
66
|
- README.md
|
@@ -79,27 +73,26 @@ files:
|
|
79
73
|
homepage: https://github.com/jjyr/method_watcher
|
80
74
|
licenses:
|
81
75
|
- MIT
|
76
|
+
metadata: {}
|
82
77
|
post_install_message:
|
83
78
|
rdoc_options: []
|
84
79
|
require_paths:
|
85
80
|
- lib
|
86
81
|
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
-
none: false
|
88
82
|
requirements:
|
89
|
-
- -
|
83
|
+
- - '>='
|
90
84
|
- !ruby/object:Gem::Version
|
91
85
|
version: '0'
|
92
86
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
-
none: false
|
94
87
|
requirements:
|
95
|
-
- -
|
88
|
+
- - '>='
|
96
89
|
- !ruby/object:Gem::Version
|
97
90
|
version: '0'
|
98
91
|
requirements: []
|
99
92
|
rubyforge_project:
|
100
|
-
rubygems_version:
|
93
|
+
rubygems_version: 2.0.5
|
101
94
|
signing_key:
|
102
|
-
specification_version:
|
95
|
+
specification_version: 4
|
103
96
|
summary: You can watch some methods, and use method_overriding callback
|
104
97
|
test_files:
|
105
98
|
- spec/method_watcher_spec.rb
|