hook_method 1.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.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +22 -0
  3. data/README.md +73 -0
  4. data/lib/method_hooker.rb +27 -0
  5. metadata +47 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 284c42e9af9c587f51f27d011482b120f5777f94
4
+ data.tar.gz: 9285584d415c83dd0faf5073c57f1666bc12d58b
5
+ SHA512:
6
+ metadata.gz: 1af7ef10657c764e95257a174d4d95b041a1a1cb06166750c8de5282e5ad7e1e84a03e158ad7ca903f26d63f9159f4081dac66ba899d52145bdf1412eb67b6cb
7
+ data.tar.gz: 1b6c636dabf21f352ad8a3c85ad91a480e10385ae9a9d86c67040245da690bc8025e793705e1518c4425fd3942229175033d6073d067b384c6b224c72811f122
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Kazuya Hotta
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # method_hooker
2
+ Hook the method call, You can specify the methods, which must be executed before or after the execution of the method.
3
+ This library expand Class-class.
4
+
5
+ Version: 1.0.0
6
+ Compliant Rubys Version: 1.9.3, 2.0.0, 2.1.0 (for Linux)
7
+ License: MIT
8
+
9
+ ## Example Code
10
+ <pre>
11
+ require 'method_hooker'
12
+
13
+ class Greet
14
+ @@name = "babe"
15
+
16
+ def hello
17
+ puts "Hello, #{@@name}\n\n"
18
+ end
19
+
20
+ def goodmorning
21
+ puts "Good Morning, #{@@name}\n\n"
22
+ end
23
+
24
+ def goodnight
25
+ puts "Good Night, #{@@name}\n\n"
26
+ end
27
+
28
+ def pre
29
+ puts "execute first"
30
+ end
31
+
32
+ def post
33
+ puts "execute after"
34
+ end
35
+
36
+ pre_execute_method :pre # Regist the method to be executed first
37
+
38
+ #post_execute_method :post # Regist the method to be executed after
39
+
40
+ #around_execute_method :pre, :post # Regist the method to be executed first and after
41
+ end
42
+
43
+ obj = Greet.new
44
+
45
+ obj.hello
46
+ obj.goodmorning
47
+ obj.goodnight
48
+
49
+ puts "\n"
50
+ puts "* Is set the method first run, when execute method?: #{Greet::has_pre_method}"
51
+ puts " => #{Greet::pre_method_name}" if Greet::has_pre_method
52
+ puts "\n"
53
+ puts "* Is set the method after run, when execute method?: #{Greet::has_post_method}"
54
+ puts " => #{Greet::post_method_name}" if Greet::has_post_method
55
+ </pre>
56
+
57
+ ### Result
58
+ <pre>
59
+ execute first
60
+ Hello, babe
61
+
62
+ execute first
63
+ Good Morning, babe
64
+
65
+ execute first
66
+ Good Night, babe
67
+
68
+
69
+ * Is set the method first run, when execute method?: true
70
+ => pre
71
+
72
+ * Is set the method after run, when execute method?: false
73
+ </pre>
@@ -0,0 +1,27 @@
1
+ # method_hooker
2
+ # @autor: Kazuya Hotta
3
+
4
+ class Class
5
+ @@hooked = []
6
+ def pre_execute_method(execute_method_name, hook_method_name)
7
+ return if @@hooked.any?{|hook| hook.strategy == :pre && hook.execute_method_name == execute_method_name && hook.hook_method_name == hook_method_name}
8
+ @@hooked << OpenStruct.new(strategy: :pre, execute_method_name: execute_method_name, hook_method_name: hook_method_name)
9
+ pre_method = instance_method(execute_method_name.id2name)
10
+ hook_method = instance_method(hook_method_name.id2name)
11
+ define_method(hook_method_name) do |*args|
12
+ pre_method.bind(self).call
13
+ hook_method.bind(self).call(*args)
14
+ end
15
+ end
16
+
17
+ def post_execute_method(execute_method_name, hook_method_name)
18
+ return if @@hooked.any?{|hook| hook.strategy == :post && hook.execute_method_name == execute_method_name && hook.hook_method_name == hook_method_name}
19
+ @@hooked << OpenStruct.new(strategy: :post, execute_method_name: execute_method_name, hook_method_name: hook_method_name)
20
+ post_method = instance_method(execute_method_name.id2name)
21
+ hook_method = instance_method(hook_method_name.id2name)
22
+ define_method(hook_method_name) do |*args|
23
+ hook_method.bind(self).call(*args)
24
+ post_method.bind(self).call
25
+ end
26
+ end
27
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hook_method
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - nyaahara
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-09-28 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Hook the method call
14
+ email:
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - LICENSE.txt
20
+ - README.md
21
+ - lib/method_hooker.rb
22
+ homepage: https://github.com/nyaahara/hook_method
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message: "\n\e[5mThank you for installing! (^-^)\e[0m\nSee also \e[31m\e[47m
27
+ \e[4mhttps://github.com/nyaahara/hook_method \e[0m\n\n"
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 2.5.1
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: Hook the method call
47
+ test_files: []