anchor 0.0.4 → 0.1.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 (3) hide show
  1. data/README.rdoc +65 -50
  2. data/lib/anchor/hooks.rb +6 -5
  3. metadata +3 -3
@@ -3,9 +3,11 @@
3
3
  I love the concepts of AOP, but I did not like different too bloated libraries,
4
4
  so I made my own.
5
5
 
6
- Anchor contains 91 lines of code and supports *before*, *after* and *around*.
6
+ Anchor contains 91 lines of code and supports *before*, *after* and *around* with its own DSL or adding methods to object.
7
7
 
8
- See example: https://github.com/tione/anchor/tree/master/lib/anchor/support/rails.rb
8
+ See examples:
9
+ * https://github.com/tione/anchor/tree/master/lib/anchor/support/rails.rb
10
+
9
11
 
10
12
 
11
13
  == Installation
@@ -20,67 +22,43 @@ Or run:
20
22
  gem install anchor
21
23
 
22
24
 
23
- == Naming recommendations
24
-
25
- You can name things as you want, but it is recommended to it so:
26
-
27
- === Example: Using *anchor* for *models* in Rails app
28
-
29
-
30
- Create app/anchors/your_model_anchor.rb (For autoload file's postfix has to be "_anchor.rb")
31
- anchor YourModel do
32
- before :save do
33
- puts "Before save #{self.inspect}"
34
- end
35
- end
36
-
37
-
38
- === Example: Using *anchor* in *models*
39
-
40
- You can also do in app/models/your_model.rb
41
-
42
- class YourModel
43
- include Anchor::Hooks
44
-
45
- before :save do
46
- puts "Before save #{self.inspect}"
47
- end
48
-
49
- end
50
-
51
25
 
52
26
 
53
27
  == Usage examples
54
28
 
55
29
  === Instance, singleton, class, object
56
-
57
- class Example
58
- def self.class_method
59
- "CLASS_METHOD"
30
+
31
+ anchor Array do
32
+ before singleton :[] do |*args|
33
+ puts "About to create an Array containing #{args}"
60
34
  end
61
- def instance_method
62
- "INSTANCE_METHOD"
35
+ after :push do |*args|
36
+ puts "Just pushed #{args} into Array #{self}"
63
37
  end
64
38
  end
65
39
 
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"
40
+ @b = Array.new
41
+
42
+ anchor @b do
43
+ after :push do
44
+ puts "This is only after @b called"
72
45
  end
73
46
  end
74
47
 
75
- @example = Example.new
76
- @other = Example.new
77
-
78
- anchor @example do
79
- before :instance_method do
80
- puts "before @example
48
+ @a = Array[1, 2, 3] # outputs "About to create an Array containing [1, 2, 3]"
49
+ @a.push(4, 5, 6) # outputs "Just pushed [4, 5, 6] into Array Array"
50
+ @b.push(1, 2, 3) # outputs "Just pushed [4, 5, 6] into Array Array" and "This is only after @b called"
51
+
52
+
53
+
54
+ === Getting called method's arguments and block
55
+
56
+ anchor Anchor::Hooks do
57
+ before singleton :included do |*args, &block|
58
+ puts "Just anchored on #{args[0]} with #{block}"
81
59
  end
82
60
  end
83
-
61
+ # To understand fully, see also: https://github.com/tione/anchor/blob/master/lib/anchor/hooks.rb#L67
84
62
 
85
63
 
86
64
  === Method regexp
@@ -95,4 +73,41 @@ You can also do in app/models/your_model.rb
95
73
 
96
74
  * https://github.com/tione/anchor/blob/master/test/test.rb
97
75
 
98
- TODO: add some examples more
76
+ TODO: add some examples more
77
+
78
+
79
+
80
+
81
+
82
+
83
+
84
+ == How to use recommendations
85
+
86
+ You can name things as you want, but it is recommended to it so:
87
+
88
+ === Example: Using *anchor* for *models* in Rails app
89
+
90
+
91
+ Create app/anchors/your_model_anchor.rb (For autoload in Rails file's postfix has to be "_anchor.rb")
92
+ anchor YourModel do
93
+ before :save do
94
+ puts "Before save #{self.inspect}"
95
+ end
96
+ end
97
+
98
+
99
+
100
+ === Example: Using *anchor* in *models*
101
+
102
+ You can also do in app/models/your_model.rb
103
+
104
+ class YourModel
105
+ include Anchor::Hooks
106
+
107
+ before :save do
108
+ puts "Before save #{self.inspect}"
109
+ end
110
+
111
+ end
112
+
113
+
@@ -30,11 +30,12 @@ module Anchor
30
30
  alias :add_hook :add_hook_for_methods
31
31
 
32
32
  def add_hook_for_method(type, method, &block)
33
+ puts "#{self} #{type} #{method} #{block} ----------"
33
34
  level, name = case method; when Hash; method.shift; when Symbol; [:instance, method]; end
34
35
  # set hooks on metaclass instance, not self, if method has to be class method or self is a Object
35
36
  in_scope = :metaclass if level == :singleton or not self.is_a?(Class)
36
37
  return Anchor.hook(class << self; self; end) do
37
- self.send(type, name, &block)
38
+ self.method(type).call(name, &block)
38
39
  end if in_scope == :metaclass
39
40
  # replace orginal method with hooked method
40
41
  instance_exec do
@@ -46,15 +47,15 @@ module Anchor
46
47
 
47
48
  unless @orginal_methods[name]
48
49
  orginal_methods[name] = self.instance_method(name)
49
- define_method name do
50
+ define_method name do |*args, &block|
50
51
  ihooks = hooks[name] # instance hooks
51
52
  orginal_method = orginal_methods[name].bind(self)
52
53
  # before
53
- ihooks[:before].each {|proc| proc.call} if ihooks[:before]
54
+ ihooks[:before].each {|proc| proc.call(*args, &block) } if ihooks[:before]
54
55
  # TODO: around
55
- retval=orginal_method.call
56
+ retval=orginal_method.call(*args, &block)
56
57
  # after
57
- ihooks[:after].each {|proc| proc.call} if ihooks[:after]
58
+ ihooks[:after].each {|proc| proc.call(*args, &block) } if ihooks[:after]
58
59
  retval
59
60
  end
60
61
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 0
8
- - 4
9
- version: 0.0.4
7
+ - 1
8
+ - 1
9
+ version: 0.1.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - "Margus P\xC3\xA4rt"