object-proxy 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +22 -0
- data/LICENSE.txt +20 -0
- data/README.md +51 -0
- data/Rakefile +37 -0
- data/VERSION +1 -0
- data/lib/object-proxy.rb +90 -0
- data/object-proxy.gemspec +57 -0
- data/test +24 -0
- metadata +112 -0
data/.document
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
gem "hash-utils", ">= 0.12.0"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "bundler", "~> 1.0.0"
|
10
|
+
gem "jeweler", "~> 1.5.2"
|
11
|
+
gem "riot", ">= 0.12.1"
|
12
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
git (1.2.5)
|
5
|
+
hash-utils (0.12.0)
|
6
|
+
jeweler (1.5.2)
|
7
|
+
bundler (~> 1.0.0)
|
8
|
+
git (>= 1.2.5)
|
9
|
+
rake
|
10
|
+
rake (0.8.7)
|
11
|
+
riot (0.12.3)
|
12
|
+
rr
|
13
|
+
rr (1.0.2)
|
14
|
+
|
15
|
+
PLATFORMS
|
16
|
+
ruby
|
17
|
+
|
18
|
+
DEPENDENCIES
|
19
|
+
bundler (~> 1.0.0)
|
20
|
+
hash-utils (>= 0.12.0)
|
21
|
+
jeweler (~> 1.5.2)
|
22
|
+
riot (>= 0.12.1)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Martin Kozák
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
Object Proxy
|
2
|
+
============
|
3
|
+
|
4
|
+
**object-proxy** provides proxy objects intended for intercepting calls
|
5
|
+
to instance methods. Works as intermediate layer between caller and
|
6
|
+
called. Allows to invoke an handler both before method call and adjust
|
7
|
+
its arguments and after call and post-proccess result. Aimed as tool
|
8
|
+
for instant adapting the complex objects without complete deriving and
|
9
|
+
extending whole classes in cases, where isn't possible to derive them
|
10
|
+
as homogenic functional units or where it's simply impractical to
|
11
|
+
derive them.
|
12
|
+
|
13
|
+
See some slightly stupid example:
|
14
|
+
|
15
|
+
require "object-proxy"
|
16
|
+
|
17
|
+
|
18
|
+
# OP[...] is shortcut, it creates the proxy object instance
|
19
|
+
|
20
|
+
s = OP["1 2 3 4"]
|
21
|
+
s.before_split do |args|
|
22
|
+
args = [" "]
|
23
|
+
end
|
24
|
+
s.after_split do |result|
|
25
|
+
result.map { |i| i.to_i + 1 }
|
26
|
+
end
|
27
|
+
|
28
|
+
# This argument will be replaced in handler
|
29
|
+
out = s.split(",")
|
30
|
+
p out
|
31
|
+
|
32
|
+
Will print out `[2, 3, 4, 5]`.
|
33
|
+
|
34
|
+
Contributing
|
35
|
+
------------
|
36
|
+
|
37
|
+
1. Fork it.
|
38
|
+
2. Create a branch (`git checkout -b 20101220-my-change`).
|
39
|
+
3. Commit your changes (`git commit -am "Added something"`).
|
40
|
+
4. Push to the branch (`git push origin 20101220-my-change`).
|
41
|
+
5. Create an [Issue][3] with a link to your branch.
|
42
|
+
6. Enjoy a refreshing Diet Coke and wait.
|
43
|
+
|
44
|
+
Copyright
|
45
|
+
---------
|
46
|
+
|
47
|
+
Copyright © 2011 [Martin Kozák][4]. See `LICENSE.txt` for
|
48
|
+
further details.
|
49
|
+
|
50
|
+
[3]: http://github.com/martinkozak/object-proxy/issues
|
51
|
+
[4]: http://www.martinkozak.net/
|
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'rubygems'
|
3
|
+
require 'bundler'
|
4
|
+
begin
|
5
|
+
Bundler.setup(:default, :development)
|
6
|
+
rescue Bundler::BundlerError => e
|
7
|
+
$stderr.puts e.message
|
8
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
9
|
+
exit e.status_code
|
10
|
+
end
|
11
|
+
require 'rake'
|
12
|
+
|
13
|
+
require 'jeweler'
|
14
|
+
Jeweler::Tasks.new do |gem|
|
15
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
16
|
+
gem.name = "object-proxy"
|
17
|
+
gem.homepage = "http://github.com/martinkozak/object-proxy"
|
18
|
+
gem.license = "MIT"
|
19
|
+
gem.summary = "Provides proxy objects intended for intercepting calls to instance methods. Works as intermediate layer between caller and called. Allows to invoke an handler both before method call and adjust its arguments and after call and post-proccess result. Aimed as tool for instant adapting the complex objects without complete deriving and extending whole classes in cases, where isn\'t possible to derive them as homogenic functional units or where it's simply impractical to derive them."
|
20
|
+
gem.email = "martinkozak@martinkozak.net"
|
21
|
+
gem.authors = ["Martin Kozák"]
|
22
|
+
# Include your dependencies below. Runtime dependencies are required when using your gem,
|
23
|
+
# and development dependencies are only needed for development (ie running rake tasks, tests, etc)
|
24
|
+
# gem.add_runtime_dependency 'jabber4r', '> 0.1'
|
25
|
+
# gem.add_development_dependency 'rspec', '> 1.2.3'
|
26
|
+
end
|
27
|
+
Jeweler::RubygemsDotOrgTasks.new
|
28
|
+
|
29
|
+
require 'rake/rdoctask'
|
30
|
+
Rake::RDocTask.new do |rdoc|
|
31
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
32
|
+
|
33
|
+
rdoc.rdoc_dir = 'rdoc'
|
34
|
+
rdoc.title = "hash-utils #{version}"
|
35
|
+
rdoc.rdoc_files.include('README*')
|
36
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
37
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lib/object-proxy.rb
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
|
3
|
+
|
4
|
+
require "hash-utils/symbol"
|
5
|
+
require "hash-utils/object"
|
6
|
+
|
7
|
+
##
|
8
|
+
# Main ObjectProxy class.
|
9
|
+
#
|
10
|
+
|
11
|
+
module ObjectProxy
|
12
|
+
|
13
|
+
##
|
14
|
+
# Creates proxy object.
|
15
|
+
#
|
16
|
+
# @param [Object] object proxied object
|
17
|
+
# @return [Class] anonymous proxy class with before and after
|
18
|
+
# handlers functionality
|
19
|
+
#
|
20
|
+
|
21
|
+
def self.create(object)
|
22
|
+
cls = Class::new(object.class)
|
23
|
+
cls.instance_eval do
|
24
|
+
|
25
|
+
# Eviscerates instances methods and replace them by
|
26
|
+
public_instance_methods.each do |method|
|
27
|
+
if not method.in? [:object_id, :__send__, :initialize]
|
28
|
+
define_method method do |*args, &block|
|
29
|
+
before = ("before_" << method.to_s).to_sym
|
30
|
+
after = ("after_" << method.to_s).to_sym
|
31
|
+
|
32
|
+
# before handler
|
33
|
+
if @handlers.include? before
|
34
|
+
args, block = @handlers[before].call(args, block)
|
35
|
+
end
|
36
|
+
|
37
|
+
# call
|
38
|
+
result = @wrapped.send(method, *args, &block)
|
39
|
+
|
40
|
+
# after handler
|
41
|
+
if @handlers.include? after
|
42
|
+
result = @handlers[after].call(result)
|
43
|
+
end
|
44
|
+
|
45
|
+
return result
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# Adds constructor
|
51
|
+
define_method :initialize do |wrapped|
|
52
|
+
@wrapped = wrapped
|
53
|
+
@handlers = { }
|
54
|
+
end
|
55
|
+
|
56
|
+
# Event handlers assigning interceptor
|
57
|
+
define_method :method_missing do |name, *args, &block|
|
58
|
+
if name.start_with? "before_", "after_"
|
59
|
+
self.register_handler(name, block)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
# Assigns event handler
|
64
|
+
define_method :register_handler do |name, &block|
|
65
|
+
@handlers[name] = block
|
66
|
+
end
|
67
|
+
|
68
|
+
attr_accessor :wrapped
|
69
|
+
end
|
70
|
+
|
71
|
+
return cls::new(object)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
##
|
76
|
+
# Shortcut module to [ObjectProxy].
|
77
|
+
#
|
78
|
+
|
79
|
+
module OP
|
80
|
+
|
81
|
+
##
|
82
|
+
# Alias for +ObjectProxy#create+.
|
83
|
+
# @see ObjectProxy
|
84
|
+
#
|
85
|
+
|
86
|
+
def self.[](object)
|
87
|
+
ObjectProxy::create(object)
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{object-proxy}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Martin Kozák"]
|
12
|
+
s.date = %q{2011-03-20}
|
13
|
+
s.email = %q{martinkozak@martinkozak.net}
|
14
|
+
s.extra_rdoc_files = [
|
15
|
+
"LICENSE.txt",
|
16
|
+
"README.md"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
".document",
|
20
|
+
"Gemfile",
|
21
|
+
"Gemfile.lock",
|
22
|
+
"LICENSE.txt",
|
23
|
+
"README.md",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"lib/object-proxy.rb",
|
27
|
+
"object-proxy.gemspec",
|
28
|
+
"test"
|
29
|
+
]
|
30
|
+
s.homepage = %q{http://github.com/martinkozak/object-proxy}
|
31
|
+
s.licenses = ["MIT"]
|
32
|
+
s.require_paths = ["lib"]
|
33
|
+
s.rubygems_version = %q{1.6.2}
|
34
|
+
s.summary = %q{Provides proxy objects intended for intercepting calls to instance methods. Works as intermediate layer between caller and called. Allows to invoke an handler both before method call and adjust its arguments and after call and post-proccess result. Aimed as tool for instant adapting the complex objects without complete deriving and extending whole classes in cases, where isn't possible to derive them as homogenic functional units or where it's simply impractical to derive them.}
|
35
|
+
|
36
|
+
if s.respond_to? :specification_version then
|
37
|
+
s.specification_version = 3
|
38
|
+
|
39
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
40
|
+
s.add_runtime_dependency(%q<hash-utils>, [">= 0.12.0"])
|
41
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
42
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
|
43
|
+
s.add_development_dependency(%q<riot>, [">= 0.12.1"])
|
44
|
+
else
|
45
|
+
s.add_dependency(%q<hash-utils>, [">= 0.12.0"])
|
46
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
47
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
48
|
+
s.add_dependency(%q<riot>, [">= 0.12.1"])
|
49
|
+
end
|
50
|
+
else
|
51
|
+
s.add_dependency(%q<hash-utils>, [">= 0.12.0"])
|
52
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
53
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
54
|
+
s.add_dependency(%q<riot>, [">= 0.12.1"])
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
data/test
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
# encoding: utf-8
|
3
|
+
# (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
|
4
|
+
|
5
|
+
$:.push("./lib")
|
6
|
+
require "object-proxy"
|
7
|
+
require "riot"
|
8
|
+
|
9
|
+
context "ObjectProxy" do
|
10
|
+
asserts("proxying") do
|
11
|
+
s = OP["a,b,c,d"]
|
12
|
+
|
13
|
+
s.register_handler(:"before_<<") do |args|
|
14
|
+
args = [",A,B,C"]
|
15
|
+
args
|
16
|
+
end
|
17
|
+
|
18
|
+
s.register_handler(:"after_<<") do |result|
|
19
|
+
"alfa beta"
|
20
|
+
end
|
21
|
+
|
22
|
+
(s << ",1,2,3" == "alfa beta") and (s == "a,b,c,d,A,B,C")
|
23
|
+
end
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: object-proxy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- "Martin Koz\xC3\xA1k"
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-03-20 00:00:00 +01:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: hash-utils
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.12.0
|
24
|
+
type: :runtime
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ~>
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 1.0.0
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: *id002
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: jeweler
|
40
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 1.5.2
|
46
|
+
type: :development
|
47
|
+
prerelease: false
|
48
|
+
version_requirements: *id003
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: riot
|
51
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: 0.12.1
|
57
|
+
type: :development
|
58
|
+
prerelease: false
|
59
|
+
version_requirements: *id004
|
60
|
+
description:
|
61
|
+
email: martinkozak@martinkozak.net
|
62
|
+
executables: []
|
63
|
+
|
64
|
+
extensions: []
|
65
|
+
|
66
|
+
extra_rdoc_files:
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
files:
|
70
|
+
- .document
|
71
|
+
- Gemfile
|
72
|
+
- Gemfile.lock
|
73
|
+
- LICENSE.txt
|
74
|
+
- README.md
|
75
|
+
- Rakefile
|
76
|
+
- VERSION
|
77
|
+
- lib/object-proxy.rb
|
78
|
+
- object-proxy.gemspec
|
79
|
+
- test
|
80
|
+
has_rdoc: true
|
81
|
+
homepage: http://github.com/martinkozak/object-proxy
|
82
|
+
licenses:
|
83
|
+
- MIT
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
hash: -571804892751824176
|
95
|
+
segments:
|
96
|
+
- 0
|
97
|
+
version: "0"
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
none: false
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: "0"
|
104
|
+
requirements: []
|
105
|
+
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 1.6.2
|
108
|
+
signing_key:
|
109
|
+
specification_version: 3
|
110
|
+
summary: Provides proxy objects intended for intercepting calls to instance methods. Works as intermediate layer between caller and called. Allows to invoke an handler both before method call and adjust its arguments and after call and post-proccess result. Aimed as tool for instant adapting the complex objects without complete deriving and extending whole classes in cases, where isn't possible to derive them as homogenic functional units or where it's simply impractical to derive them.
|
111
|
+
test_files: []
|
112
|
+
|