bryanp-adviser 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 (5) hide show
  1. data/License.txt +20 -0
  2. data/README.rdoc +46 -0
  3. data/adviser.gemspec +17 -0
  4. data/lib/adviser.rb +70 -0
  5. metadata +56 -0
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Bryan Powell
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,46 @@
1
+ == Usage
2
+
3
+ Here's an incredibly goofy example of how one might use Adviser.
4
+
5
+ require 'adviser'
6
+
7
+ class Speaker
8
+ def initialize
9
+ puts @intro || "*awkward pause*"
10
+ end
11
+
12
+ def main_point
13
+ puts "Eh.. I seem to have misplaced my notes."
14
+ end
15
+ end
16
+
17
+ # Let's create a teleprompter for our speaker.
18
+
19
+ advise Speaker do
20
+ before :initialize do
21
+ @intro = "Hello!"
22
+ end
23
+
24
+ instead :main_point do
25
+ puts "Ruby is awesome."
26
+ end
27
+
28
+ after :main_point do
29
+ puts "Goodbye!"
30
+ end
31
+ end
32
+
33
+ s = Speaker.new
34
+ s.main_point
35
+
36
+
37
+ Output without adviser:
38
+
39
+ *awkward pause*
40
+ Eh.. I seem to have misplaced my notes.
41
+
42
+ Output with adviser:
43
+
44
+ Hello!
45
+ Ruby is awesome.
46
+ Goodbye!
@@ -0,0 +1,17 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "adviser"
3
+ s.version = "0.1.1"
4
+ s.date = "2009-05-14"
5
+ s.summary = "Simple AOP library for Ruby"
6
+ s.email = "bryan@metabahn.com"
7
+ s.homepage = "http://github.com/bryanp/adviser"
8
+ s.description = "Adviser allows you to give advice to another object."
9
+ s.has_rdoc = false
10
+ s.authors = ["Bryan Powell"]
11
+ s.files = [
12
+ "README.rdoc",
13
+ "adviser.gemspec",
14
+ "License.txt",
15
+ "lib/adviser.rb",
16
+ ]
17
+ end
@@ -0,0 +1,70 @@
1
+ class << self
2
+ def advise(object, &block)
3
+ @object = object
4
+ Proc.new(&block).call
5
+ end
6
+
7
+ def instead(method, &block)
8
+ @object.__create_instead(method, Proc.new(&block))
9
+ end
10
+
11
+ def before(method, &block)
12
+ @object.__create_before(method, Proc.new(&block))
13
+ end
14
+
15
+ def after(method, &block)
16
+ @object.__create_after(method, Proc.new(&block))
17
+ end
18
+ end
19
+
20
+ class Object
21
+ private
22
+
23
+ def self.__create_instead(method, proc)
24
+ id = __id(proc)
25
+
26
+ __alias(method, id)
27
+ define_method method, proc
28
+ end
29
+
30
+ def self.__create_before(method, proc)
31
+ args = __args(method)
32
+ id = __id(proc)
33
+
34
+ __alias(method, id)
35
+ define_method :"__before_#{method}_#{id}", proc
36
+ class_eval %{
37
+ def #{method}#{args}
38
+ __before_#{method}_#{id}#{args}
39
+ __previous_#{method}_#{id}#{args}
40
+ end
41
+ }
42
+ end
43
+
44
+ def self.__create_after(method, proc)
45
+ args = __args(method)
46
+ id = __id(proc)
47
+
48
+ __alias(method, id)
49
+ define_method :"__after_#{method}_#{id}", proc
50
+ class_eval %{
51
+ def #{method}#{args}
52
+ res = __previous_#{method}_#{id}#{args}
53
+ __after_#{method}_#{id}#{args}
54
+ res
55
+ end
56
+ }
57
+ end
58
+
59
+ def self.__alias(method, id)
60
+ alias_method :"__previous_#{method}_#{id}", method
61
+ end
62
+
63
+ def self.__args(method)
64
+ instance_method(method).arity == 0 ? '' : '(*args)'
65
+ end
66
+
67
+ def self.__id(proc)
68
+ "%04x" % proc.object_id
69
+ end
70
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bryanp-adviser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Bryan Powell
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-14 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Adviser allows you to give advice to another object.
17
+ email: bryan@metabahn.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README.rdoc
26
+ - adviser.gemspec
27
+ - License.txt
28
+ - lib/adviser.rb
29
+ has_rdoc: false
30
+ homepage: http://github.com/bryanp/adviser
31
+ post_install_message:
32
+ rdoc_options: []
33
+
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: "0"
41
+ version:
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ requirements: []
49
+
50
+ rubyforge_project:
51
+ rubygems_version: 1.2.0
52
+ signing_key:
53
+ specification_version: 2
54
+ summary: Simple AOP library for Ruby
55
+ test_files: []
56
+