proxies 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Jakub Kuźma
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,43 @@
1
+ = Proxies
2
+
3
+ Proxies can even proxy other proxies. The gem is Ruby 1.8 and 1.9 compatible (on 1.9 uses BasicObject class).
4
+
5
+ == Installation
6
+
7
+ gem install proxies
8
+
9
+ == MethodProxy Usage
10
+
11
+ Proxies::MethodProxy.new(self, :my_method) do
12
+ def extension_method
13
+ proxy_target.length * 500
14
+ end
15
+ end
16
+
17
+ Proxies::MethodProxy.new(self, :my_method, :extend => MyExtension) do
18
+ def add_owner_and_target_values
19
+ proxy_target.value + proxy_owner.value
20
+ end
21
+ end
22
+
23
+ Proxies::MethodProxy.new(self, :other_method, :extend => [FirstExtension, SecondExtension])
24
+
25
+ == ObjectProxy Usage
26
+
27
+ Proxies::ObjectProxy.new(target_object) do
28
+ def extension_method
29
+ @target.length * 500
30
+ end
31
+ end
32
+
33
+ Proxies::ObjectProxy.new(target_object, :owner => self, :extend => MyExtension) do
34
+ def add_owner_and_target_values
35
+ @target.value + @owner.value
36
+ end
37
+ end
38
+
39
+ Proxies::ObjectProxy.new(target_object, :extend => [FirstExtension, SecondExtension])
40
+
41
+ == Copyright
42
+
43
+ Copyright (c) 2010 Jakub Kuźma. See LICENSE for details.
@@ -0,0 +1,56 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require "rubygems"
4
+ require "rake"
5
+
6
+ begin
7
+ require "jeweler"
8
+ Jeweler::Tasks.new do |gem|
9
+ gem.name = "proxies"
10
+ gem.summary = %Q{Proxies can even proxy other proxies}
11
+ gem.description = %Q{Proxies can even proxy other proxies}
12
+ gem.email = "qoobaa@gmail.com"
13
+ gem.homepage = "http://github.com/qoobaa/proxies"
14
+ gem.authors = ["Jakub Kuźma"]
15
+ gem.add_development_dependency "test-unit", ">= 2"
16
+ gem.add_development_dependency "mocha"
17
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
18
+ end
19
+ Jeweler::GemcutterTasks.new
20
+ rescue LoadError
21
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
22
+ end
23
+
24
+ require "rake/testtask"
25
+ Rake::TestTask.new(:test) do |test|
26
+ test.libs << "lib" << "test"
27
+ test.pattern = "test/**/test_*.rb"
28
+ test.verbose = true
29
+ end
30
+
31
+ begin
32
+ require "rcov/rcovtask"
33
+ Rcov::RcovTask.new do |test|
34
+ test.libs << "test"
35
+ test.pattern = "test/**/test_*.rb"
36
+ test.verbose = true
37
+ end
38
+ rescue LoadError
39
+ task :rcov do
40
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
41
+ end
42
+ end
43
+
44
+ task :test => :check_dependencies
45
+
46
+ task :default => :test
47
+
48
+ require "rake/rdoctask"
49
+ Rake::RDocTask.new do |rdoc|
50
+ version = File.exist?("VERSION") ? File.read("VERSION") : ""
51
+
52
+ rdoc.rdoc_dir = "rdoc"
53
+ rdoc.title = "proxies #{version}"
54
+ rdoc.rdoc_files.include("README*")
55
+ rdoc.rdoc_files.include("lib/**/*.rb")
56
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,2 @@
1
+ require "proxies/object_proxy"
2
+ require "proxies/method_proxy"
@@ -0,0 +1,50 @@
1
+ module Proxies
2
+ class MethodProxy < defined?(BasicObject) ? BasicObject : Object
3
+ instance_methods.each { |m| undef_method(m) if m.to_s !~ /^__/ }
4
+
5
+ # Creates a new proxy for target method of given object. You can
6
+ # pass a block (anonymous module) to extend the proxy object
7
+ # "inline".
8
+ #
9
+ # ==== Options
10
+ # * <tt>:extend</tt> - Module or array of modules used to extend
11
+ # * the newly created proxy object
12
+ #
13
+ # ==== Examples
14
+ # MethodProxy.new(self, :my_method) do
15
+ # def extension_method
16
+ # proxy_target.length * 500
17
+ # end
18
+ # end
19
+ #
20
+ # MethodProxy.new(self, :my_method, :extend => MyExtension) do
21
+ # def add_owner_and_target_values
22
+ # proxy_target.value + proxy_owner.value
23
+ # end
24
+ # end
25
+ #
26
+ # MethodProxy.new(self, :other_method, :extend => [FirstExtension, SecondExtension])
27
+
28
+ def initialize(owner, method_name, options = {}, &block)
29
+ @owner = owner
30
+ @method_name = method_name
31
+
32
+ extends = Array(options[:extend])
33
+ extends << ::Module.new(&block)
34
+ extends.each { |m| m.send(:extend_object, self) }
35
+ end
36
+
37
+ def proxy_target
38
+ @target = @owner.send(@method_name) unless defined?(@target)
39
+ @target
40
+ end
41
+
42
+ def proxy_owner
43
+ @owner
44
+ end
45
+
46
+ def method_missing(name, *args, &block)
47
+ proxy_target.send(name, *args, &block)
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,50 @@
1
+ module Proxies
2
+ module ObjectProxyOwner
3
+ def proxy_owner; @owner end
4
+ end
5
+
6
+ class ObjectProxy < defined?(BasicObject) ? BasicObject : Object
7
+ instance_methods.each { |m| undef_method(m) if m.to_s !~ /^__/ }
8
+
9
+ # Creates a new proxy for target object. You can pass a block
10
+ # (anonymous module) to extend the proxy object "inline".
11
+ #
12
+ # ==== Options
13
+ # * <tt>:owner</tt> - Optional owner object
14
+ # * <tt>:extend</tt> - Module or array of modules used to extend
15
+ # * the newly created proxy object
16
+ #
17
+ # ==== Examples
18
+ # ObjectProxy.new(target_object) do
19
+ # def extension_method
20
+ # proxy_target.length * 500
21
+ # end
22
+ # end
23
+ #
24
+ # ObjectProxy.new(target_object, :owner => self, :extend => MyExtension) do
25
+ # def add_owner_and_target_values
26
+ # proxy_target.value + @owner.value
27
+ # end
28
+ # end
29
+ #
30
+ # ObjectProxy.new(target_object, :extend => [FirstExtension, SecondExtension])
31
+
32
+ def initialize(target, options = {}, &block)
33
+ @target = target
34
+ @owner = options[:owner] if options.key?(:owner)
35
+
36
+ extends = Array(options[:extend])
37
+ extends << ::Module.new(&block)
38
+ extends << ObjectProxyOwner if defined?(@owner)
39
+ extends.each { |m| m.send(:extend_object, self) }
40
+ end
41
+
42
+ def proxy_target
43
+ @target
44
+ end
45
+
46
+ def method_missing(name, *args, &block)
47
+ proxy_target.send(name, *args, &block)
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,69 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{proxies}
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 = ["Jakub Kuźma"]
12
+ s.date = %q{2010-04-01}
13
+ s.description = %q{Proxies can even proxy other proxies}
14
+ s.email = %q{qoobaa@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/proxies.rb",
27
+ "lib/proxies/method_proxy.rb",
28
+ "lib/proxies/object_proxy.rb",
29
+ "proxies.gemspec",
30
+ "test/helper.rb",
31
+ "test/test_method_proxy_extend.rb",
32
+ "test/test_method_proxy_owner.rb",
33
+ "test/test_method_proxy_target.rb",
34
+ "test/test_object_proxy_extend.rb",
35
+ "test/test_object_proxy_owner.rb",
36
+ "test/test_object_proxy_target.rb"
37
+ ]
38
+ s.homepage = %q{http://github.com/qoobaa/proxies}
39
+ s.rdoc_options = ["--charset=UTF-8"]
40
+ s.require_paths = ["lib"]
41
+ s.rubygems_version = %q{1.3.6}
42
+ s.summary = %q{Proxies can even proxy other proxies}
43
+ s.test_files = [
44
+ "test/test_method_proxy_target.rb",
45
+ "test/test_object_proxy_extend.rb",
46
+ "test/test_method_proxy_extend.rb",
47
+ "test/test_object_proxy_owner.rb",
48
+ "test/test_method_proxy_owner.rb",
49
+ "test/test_object_proxy_target.rb",
50
+ "test/helper.rb"
51
+ ]
52
+
53
+ if s.respond_to? :specification_version then
54
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
55
+ s.specification_version = 3
56
+
57
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
58
+ s.add_development_dependency(%q<test-unit>, [">= 2"])
59
+ s.add_development_dependency(%q<mocha>, [">= 0"])
60
+ else
61
+ s.add_dependency(%q<test-unit>, [">= 2"])
62
+ s.add_dependency(%q<mocha>, [">= 0"])
63
+ end
64
+ else
65
+ s.add_dependency(%q<test-unit>, [">= 2"])
66
+ s.add_dependency(%q<mocha>, [">= 0"])
67
+ end
68
+ end
69
+
@@ -0,0 +1,12 @@
1
+ require "rubygems"
2
+ gem "test-unit"
3
+ require "test/unit"
4
+ require "mocha"
5
+
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
7
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
8
+ require "proxies"
9
+
10
+ class Test::Unit::TestCase
11
+ include Proxies
12
+ end
@@ -0,0 +1,33 @@
1
+ require "helper"
2
+
3
+ module One
4
+ def one; 1 end
5
+ end
6
+
7
+ module Two
8
+ def two; 2 end
9
+ end
10
+
11
+ module Three
12
+ def three; 3 end
13
+ end
14
+
15
+ class TestMethodProxyExtend < Test::Unit::TestCase
16
+ def setup
17
+ @target = "target"
18
+ @object = mock
19
+ @object.expects(:target_method).at_most_once.returns(@target)
20
+ end
21
+
22
+ test "extend works properly with array" do
23
+ proxy = MethodProxy.new(@object, :target_method, :extend => [One, Two, Three])
24
+ assert_equal 1, proxy.one
25
+ assert_equal 2, proxy.two
26
+ assert_equal 3, proxy.three
27
+ end
28
+
29
+ test "extend works with single value" do
30
+ proxy = MethodProxy.new(@object, :target_method, :extend => Two)
31
+ assert_equal 2, proxy.two
32
+ end
33
+ end
@@ -0,0 +1,15 @@
1
+ require "helper"
2
+
3
+ class TestMethodProxyOwner < Test::Unit::TestCase
4
+ def setup
5
+ @target = "target"
6
+ @object = mock
7
+ @object.expects(:target_method).at_most_once.returns(@target)
8
+ end
9
+
10
+ test "proxy_owner is defined" do
11
+ proxy = MethodProxy.new(@object, :target_method)
12
+
13
+ assert_equal @object, proxy.proxy_owner
14
+ end
15
+ end
@@ -0,0 +1,39 @@
1
+ require "helper"
2
+
3
+ class TestMethodProxyTarget < Test::Unit::TestCase
4
+ def setup
5
+ @target = "target"
6
+ @object = mock
7
+ @object.expects(:target_method).once.returns(@target)
8
+ @proxy = MethodProxy.new(@object, :target_method) do
9
+ def length_plus_one
10
+ proxy_target.length + 1
11
+ end
12
+ end
13
+ end
14
+
15
+ test "non-existing proxy method call is passed to the target" do
16
+ assert_equal @target.length, @proxy.length
17
+ end
18
+
19
+ test "proxy method returns correct result" do
20
+ assert_equal @target.length + 1, @proxy.length_plus_one
21
+ end
22
+
23
+ test "object_id method call is passed to proxy target" do
24
+ assert_equal @target.object_id, @proxy.object_id
25
+ end
26
+
27
+ test "send method call is passed to proxy target" do
28
+ assert_equal @target.send(:length), @proxy.send(:length)
29
+ end
30
+
31
+ test "== method is passed to proxy_target" do
32
+ assert @target == @proxy
33
+ assert @proxy == @target
34
+ end
35
+
36
+ test "equal? method is passed to proxy_target" do
37
+ assert @proxy.equal?(@target)
38
+ end
39
+ end
@@ -0,0 +1,27 @@
1
+ require "helper"
2
+
3
+ module One
4
+ def one; 1 end
5
+ end
6
+
7
+ module Two
8
+ def two; 2 end
9
+ end
10
+
11
+ module Three
12
+ def three; 3 end
13
+ end
14
+
15
+ class TestObjectProxyExtend < Test::Unit::TestCase
16
+ test "extend works properly with array" do
17
+ proxy = ObjectProxy.new("target", :extend => [One, Two, Three])
18
+ assert_equal 1, proxy.one
19
+ assert_equal 2, proxy.two
20
+ assert_equal 3, proxy.three
21
+ end
22
+
23
+ test "extend works with single value" do
24
+ proxy = ObjectProxy.new("target", :extend => Two)
25
+ assert_equal 2, proxy.two
26
+ end
27
+ end
@@ -0,0 +1,35 @@
1
+ require "helper"
2
+
3
+ class TestObjectProxyOwner < Test::Unit::TestCase
4
+ test "proxy_owner is defined if owner given" do
5
+ owner = "owner"
6
+
7
+ proxy = ObjectProxy.new("target", :owner => owner) do
8
+ def owner_length_plus_one
9
+ proxy_owner.length + 1
10
+ end
11
+ end
12
+
13
+ assert owner.length + 1, proxy.owner_length_plus_one
14
+ end
15
+
16
+ test "proxy_owner is not defined if owner not given" do
17
+ proxy = ObjectProxy.new("target") do
18
+ def owner_length_plus_one
19
+ proxy_owner.length + 1
20
+ end
21
+ end
22
+
23
+ assert_raises(NoMethodError) { proxy.owner_length_plus_one }
24
+ end
25
+
26
+ test "proxy_owner can be nil value" do
27
+ proxy = ObjectProxy.new("target", :owner => nil) do
28
+ def owner_nil?
29
+ proxy_owner.nil?
30
+ end
31
+ end
32
+
33
+ assert proxy.owner_nil?
34
+ end
35
+ end
@@ -0,0 +1,37 @@
1
+ require "helper"
2
+
3
+ class TestObjectProxyTarget < Test::Unit::TestCase
4
+ def setup
5
+ @target = "target"
6
+ @proxy = ObjectProxy.new(@target) do
7
+ def length_plus_one
8
+ proxy_target.length + 1
9
+ end
10
+ end
11
+ end
12
+
13
+ test "non-existing proxy method call is passed to the target" do
14
+ assert_equal @target.length, @proxy.length
15
+ end
16
+
17
+ test "proxy method returns correct result" do
18
+ assert_equal @target.length + 1, @proxy.length_plus_one
19
+ end
20
+
21
+ test "object_id method call is passed to proxy target" do
22
+ assert_equal @target.object_id, @proxy.object_id
23
+ end
24
+
25
+ test "send method call is passed to proxy target" do
26
+ assert_equal @target.send(:length), @proxy.send(:length)
27
+ end
28
+
29
+ test "== method is passed to proxy_target" do
30
+ assert @target == @proxy
31
+ assert @proxy == @target
32
+ end
33
+
34
+ test "equal? method is passed to proxy_target" do
35
+ assert @proxy.equal?(@target)
36
+ end
37
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: proxies
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - "Jakub Ku\xC5\xBAma"
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-01 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: test-unit
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ version: "2"
30
+ type: :development
31
+ version_requirements: *id001
32
+ - !ruby/object:Gem::Dependency
33
+ name: mocha
34
+ prerelease: false
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ segments:
40
+ - 0
41
+ version: "0"
42
+ type: :development
43
+ version_requirements: *id002
44
+ description: Proxies can even proxy other proxies
45
+ email: qoobaa@gmail.com
46
+ executables: []
47
+
48
+ extensions: []
49
+
50
+ extra_rdoc_files:
51
+ - LICENSE
52
+ - README.rdoc
53
+ files:
54
+ - .document
55
+ - .gitignore
56
+ - LICENSE
57
+ - README.rdoc
58
+ - Rakefile
59
+ - VERSION
60
+ - lib/proxies.rb
61
+ - lib/proxies/method_proxy.rb
62
+ - lib/proxies/object_proxy.rb
63
+ - proxies.gemspec
64
+ - test/helper.rb
65
+ - test/test_method_proxy_extend.rb
66
+ - test/test_method_proxy_owner.rb
67
+ - test/test_method_proxy_target.rb
68
+ - test/test_object_proxy_extend.rb
69
+ - test/test_object_proxy_owner.rb
70
+ - test/test_object_proxy_target.rb
71
+ has_rdoc: true
72
+ homepage: http://github.com/qoobaa/proxies
73
+ licenses: []
74
+
75
+ post_install_message:
76
+ rdoc_options:
77
+ - --charset=UTF-8
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ segments:
85
+ - 0
86
+ version: "0"
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ segments:
92
+ - 0
93
+ version: "0"
94
+ requirements: []
95
+
96
+ rubyforge_project:
97
+ rubygems_version: 1.3.6
98
+ signing_key:
99
+ specification_version: 3
100
+ summary: Proxies can even proxy other proxies
101
+ test_files:
102
+ - test/test_method_proxy_target.rb
103
+ - test/test_object_proxy_extend.rb
104
+ - test/test_method_proxy_extend.rb
105
+ - test/test_object_proxy_owner.rb
106
+ - test/test_method_proxy_owner.rb
107
+ - test/test_object_proxy_target.rb
108
+ - test/helper.rb