anchor 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +44 -19
- data/lib/anchor.rb +2 -2
- data/lib/anchor/hooks.rb +11 -7
- data/lib/{kernel.rb → anchor/support/kernel.rb} +1 -1
- data/lib/anchor/support/rails.rb +7 -0
- metadata +7 -6
data/README.rdoc
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
== Purpose
|
2
2
|
|
3
|
-
|
3
|
+
I love the concepts of AOP, but I did not like different too bloated libraries,
|
4
|
+
so I made my own.
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
* around (Not implemented yet)
|
6
|
+
Anchor contains 91 lines of code and supports *before*, *after* and *around*.
|
7
|
+
|
8
|
+
See example: https://github.com/tione/anchor/tree/master/lib/anchor/support/rails.rb
|
9
9
|
|
10
10
|
|
11
11
|
== Installation
|
12
12
|
|
13
13
|
Add to your Gemfile:
|
14
|
-
gem 'anchor'
|
14
|
+
gem 'anchor' # add this row after "gem 'rails'" if rails is used
|
15
15
|
|
16
16
|
And run:
|
17
17
|
bundle install
|
@@ -24,25 +24,18 @@ Or run:
|
|
24
24
|
|
25
25
|
You can name things as you want, but it is recommended to it so:
|
26
26
|
|
27
|
-
=== Example: Using *
|
27
|
+
=== Example: Using *anchor* for *models* in Rails app
|
28
28
|
|
29
|
-
Create app/hooks/model_name.rb
|
30
|
-
anchor ModelName do
|
31
29
|
|
30
|
+
Create app/anchors/your_model_anchor.rb (For autoload file's postfix has to be "_anchor.rb")
|
31
|
+
anchor YourModel do
|
32
32
|
before :save do
|
33
33
|
puts "Before save #{self.inspect}"
|
34
34
|
end
|
35
|
-
|
36
35
|
end
|
37
36
|
|
38
37
|
|
39
|
-
=== Example: Using *
|
40
|
-
|
41
|
-
Create app/hooks/model_name/what_for_hook_is_itended.rb
|
42
|
-
...
|
43
|
-
|
44
|
-
|
45
|
-
=== Example: Using *hooks* in *models*
|
38
|
+
=== Example: Using *anchor* in *models*
|
46
39
|
|
47
40
|
You can also do in app/models/your_model.rb
|
48
41
|
|
@@ -59,9 +52,35 @@ You can also do in app/models/your_model.rb
|
|
59
52
|
|
60
53
|
== Usage examples
|
61
54
|
|
62
|
-
===
|
55
|
+
=== Instance, singleton, class, object
|
63
56
|
|
64
|
-
|
57
|
+
class Example
|
58
|
+
def self.class_method
|
59
|
+
"CLASS_METHOD"
|
60
|
+
end
|
61
|
+
def instance_method
|
62
|
+
"INSTANCE_METHOD"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
anchor Example do
|
67
|
+
before singleton :class_method do
|
68
|
+
puts "before class_method"
|
69
|
+
end
|
70
|
+
after :instance_method do
|
71
|
+
puts "after instance_method"
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
@example = Example.new
|
76
|
+
@other = Example.new
|
77
|
+
|
78
|
+
anchor @example do
|
79
|
+
before :instance_method do
|
80
|
+
puts "before @example
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
65
84
|
|
66
85
|
|
67
86
|
=== Method regexp
|
@@ -71,3 +90,9 @@ You can also do in app/models/your_model.rb
|
|
71
90
|
puts
|
72
91
|
end
|
73
92
|
end
|
93
|
+
|
94
|
+
=== You can find some examples from:
|
95
|
+
|
96
|
+
* https://github.com/tione/anchor/blob/master/test/test.rb
|
97
|
+
|
98
|
+
TODO: add some examples more
|
data/lib/anchor.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
require 'kernel'
|
1
|
+
require 'anchor/support/kernel'
|
2
2
|
require 'anchor/hooks'
|
3
|
-
|
3
|
+
require 'anchor/support/rails'
|
data/lib/anchor/hooks.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# = Anchor
|
2
2
|
module Anchor
|
3
|
-
def self.hook(*objs, &block)
|
3
|
+
def self.hook(*objs, &block) # :nodoc:
|
4
4
|
objs.each do |obj|
|
5
5
|
#puts "Trying to hook: #{obj} #{block.inspect}"
|
6
6
|
Hooks.included(obj)
|
@@ -45,11 +45,16 @@ module Anchor
|
|
45
45
|
orginal_methods = @orginal_methods ||= {}
|
46
46
|
|
47
47
|
unless @orginal_methods[name]
|
48
|
-
|
49
|
-
define_method name do
|
50
|
-
hooks[name]
|
51
|
-
|
52
|
-
|
48
|
+
orginal_methods[name] = self.instance_method(name)
|
49
|
+
define_method name do
|
50
|
+
ihooks = hooks[name] # instance hooks
|
51
|
+
orginal_method = orginal_methods[name].bind(self)
|
52
|
+
# before
|
53
|
+
ihooks[:before].each {|proc| proc.call} if ihooks[:before]
|
54
|
+
# TODO: around
|
55
|
+
retval=orginal_method.call
|
56
|
+
# after
|
57
|
+
ihooks[:after].each {|proc| proc.call} if ihooks[:after]
|
53
58
|
retval
|
54
59
|
end
|
55
60
|
end
|
@@ -68,5 +73,4 @@ module Anchor
|
|
68
73
|
include Anchor::Hooks::Hooked
|
69
74
|
end
|
70
75
|
end
|
71
|
-
|
72
76
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 4
|
9
|
+
version: 0.0.4
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- "Margus P\xC3\xA4rt"
|
@@ -14,11 +14,11 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2012-01-
|
17
|
+
date: 2012-01-04 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
21
|
-
description:
|
21
|
+
description: Before, after and around method calls.
|
22
22
|
email: margus@tione.eu
|
23
23
|
executables: []
|
24
24
|
|
@@ -29,7 +29,8 @@ extra_rdoc_files: []
|
|
29
29
|
files:
|
30
30
|
- README.rdoc
|
31
31
|
- lib/anchor/hooks.rb
|
32
|
-
- lib/
|
32
|
+
- lib/anchor/support/rails.rb
|
33
|
+
- lib/anchor/support/kernel.rb
|
33
34
|
- lib/anchor.rb
|
34
35
|
has_rdoc: true
|
35
36
|
homepage: https://github.com/tione/anchor
|
@@ -64,6 +65,6 @@ rubyforge_project:
|
|
64
65
|
rubygems_version: 1.3.7
|
65
66
|
signing_key:
|
66
67
|
specification_version: 3
|
67
|
-
summary:
|
68
|
+
summary: Inspired by AOP.
|
68
69
|
test_files: []
|
69
70
|
|