easy-hook 0.0.3

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 (2) hide show
  1. data/lib/easy-hook.rb +61 -0
  2. metadata +47 -0
@@ -0,0 +1,61 @@
1
+ # Contains methods to hook method calls
2
+ module EasyHook
3
+
4
+ module ClassMethods
5
+
6
+ private
7
+
8
+ # Hook the provided instance methods so that the block
9
+ # is executed directly after the specified methods have
10
+ # been invoked.
11
+ #
12
+
13
+ def hook_before(*syms, &block)
14
+ syms.each do |sym| # For each symbol
15
+ str_id = "__#{sym}__leading__"
16
+ unless private_instance_methods.include?(str_id)
17
+ alias_method str_id, sym
18
+ private str_id
19
+ define_method sym do |*args|
20
+ block.call(self,{
21
+ :method => sym,
22
+ :args => args
23
+ })
24
+ result = __send__ str_id, *args
25
+ result
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ def hook_after(*syms, &block)
32
+ syms.each do |sym| # For each symbol
33
+ str_id = "__#{sym}__following__"
34
+ unless private_instance_methods.include?(str_id)
35
+ alias_method str_id, sym
36
+ private str_id
37
+ define_method sym do |*args|
38
+ result = __send__ str_id, *args
39
+ block.call(self,{
40
+ :method => sym,
41
+ :args => args,
42
+ :return => result
43
+ })
44
+ result
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ alias_method :hooks_after, :hook_after
51
+ alias_method :hooks_before, :hook_before
52
+
53
+ end
54
+
55
+ # On inclusion, we extend the receiver by
56
+ # the defined class-methods. This is an ruby
57
+ # idiom for defining class methods within a module.
58
+ def EasyHook.included(base)
59
+ base.extend(ClassMethods)
60
+ end
61
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easy-hook
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jeff Wells
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-04 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Quickly create before and after callbacks around ruby methods using blocks.
15
+ Add them to multiple methods at once to track or modify calls.
16
+ email:
17
+ - jeff.wells@gmx.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - lib/easy-hook.rb
23
+ homepage: http://jeffrwells.com
24
+ licenses: []
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 1.8.23
44
+ signing_key:
45
+ specification_version: 3
46
+ summary: Easily add before and after callbacks to ruby methods with blocks.
47
+ test_files: []