proxies 0.1.1 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,21 +1,3 @@
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
1
  pkg
20
-
21
- ## PROJECT::SPECIFIC
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :gemcutter
2
+
3
+ gemspec
@@ -0,0 +1,21 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ proxies (0.2.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ mocha (0.9.8)
10
+ rake
11
+ rake (0.8.7)
12
+ test-unit (2.1.1)
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ bundler (>= 1.0.0)
19
+ mocha
20
+ proxies!
21
+ test-unit (>= 2)
@@ -6,37 +6,27 @@ Proxies can even proxy other proxies. The gem is Ruby 1.8 and 1.9 compatible (on
6
6
 
7
7
  gem install proxies
8
8
 
9
- == MethodProxy Usage
9
+ == Usage
10
10
 
11
- Proxies::MethodProxy.new(self, :my_method) do
11
+ Proxies::Proxy.new(lambda { my_method }, :owner => self) do
12
12
  def extension_method
13
13
  proxy_target.length * 500
14
14
  end
15
15
  end
16
16
 
17
- Proxies::MethodProxy.new(self, :my_method, :extend => MyExtension) do
17
+ Proxies::Proxy.new(lambda { my_method }, :owner => self, :extend => MyExtension) do
18
18
  def add_owner_and_target_values
19
19
  proxy_target.value + proxy_owner.value
20
20
  end
21
21
  end
22
22
 
23
- Proxies::MethodProxy.new(self, :other_method, :extend => [FirstExtension, SecondExtension])
24
-
25
- == ObjectProxy Usage
26
-
27
- Proxies::ObjectProxy.new(target_object) do
23
+ Proxies::Proxy.new(lambda { target_object }) do
28
24
  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
25
+ proxy_target.length * 500
36
26
  end
37
27
  end
38
28
 
39
- Proxies::ObjectProxy.new(target_object, :extend => [FirstExtension, SecondExtension])
29
+ Proxies::Proxy.new(lambda { my_method }, :owner => self, :extend => [FirstExtension, SecondExtension])
40
30
 
41
31
  == Copyright
42
32
 
data/Rakefile CHANGED
@@ -1,56 +1,12 @@
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
1
  require "rake/testtask"
2
+ require "bundler"
3
+ Bundler::GemHelper.install_tasks
4
+ Bundler.setup
5
+
25
6
  Rake::TestTask.new(:test) do |test|
26
7
  test.libs << "lib" << "test"
27
8
  test.pattern = "test/**/test_*.rb"
28
9
  test.verbose = true
29
10
  end
30
11
 
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
12
  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
@@ -1,2 +1,5 @@
1
- require "proxies/object_proxy"
2
- require "proxies/method_proxy"
1
+ require "proxies/proxy"
2
+
3
+ module Proxies
4
+
5
+ end
@@ -0,0 +1,64 @@
1
+ module Proxies
2
+ module ProxyOwner
3
+ def proxy_owner; @owner end
4
+ end
5
+
6
+ module ProxyRespondTo
7
+ def proxy_respond_to?(method)
8
+ this = self
9
+ !!::Kernel.eval("defined?(this.#{method})")
10
+ end
11
+ end
12
+
13
+ class Proxy < defined?(BasicObject) ? BasicObject : Object
14
+ instance_methods.each { |m| undef_method(m) if m.to_s !~ /^__/ }
15
+
16
+ # Creates a new proxy for target. You can pass a block (anonymous
17
+ # module) to extend the proxy object "inline".
18
+ #
19
+ # ==== Options
20
+ # * <tt>:owner</tt> - Optional owner object
21
+ # * <tt>:extend</tt> - Module or array of modules used to extend
22
+ # * the newly created proxy object
23
+ #
24
+ # ==== Examples
25
+ # Proxy.new(lambda { [1, 2, 3] }) do
26
+ # def extension_method
27
+ # proxy_target.length * 500
28
+ # end
29
+ # end
30
+ #
31
+ # Proxy.new(lambda { my_method }, :extend => MyExtension) do
32
+ # def add_owner_and_target_values
33
+ # proxy_target.value + proxy_owner.value
34
+ # end
35
+ # end
36
+ #
37
+ # Proxy.new(lambda { my_method }, :owner => self) do
38
+ # def do_something
39
+ # ...
40
+ # end
41
+ # end
42
+ #
43
+ # Proxy.new(lambda { something }, :extend => [FirstExtension, SecondExtension])
44
+
45
+ def initialize(lazy_target, options = {}, &block)
46
+ @lazy_target = lazy_target
47
+ @owner = options[:owner] if options.key?(:owner)
48
+
49
+ extends = ::Kernel.Array(options[:extend])
50
+ extends << ::Module.new(&block)
51
+ extends << ::Proxies::ProxyOwner if defined?(@owner)
52
+ extends << ::Proxies::ProxyRespondTo
53
+ extends.each { |m| m.send(:extend_object, self) }
54
+ end
55
+
56
+ def proxy_target
57
+ defined?(@target) ? @target : @target = @lazy_target.call
58
+ end
59
+
60
+ def method_missing(name, *args, &block)
61
+ proxy_target.send(name, *args, &block)
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,3 @@
1
+ module Proxies
2
+ VERSION = "0.2.1"
3
+ end
@@ -1,69 +1,24 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
1
  # -*- encoding: utf-8 -*-
2
+ require File.expand_path("../lib/proxies/version", __FILE__)
5
3
 
6
4
  Gem::Specification.new do |s|
7
- s.name = %q{proxies}
8
- s.version = "0.1.1"
5
+ s.name = "proxies"
6
+ s.version = Proxies::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Jakub Kuźma"]
9
+ s.email = ["qoobaa@gmail.com"]
10
+ s.homepage = "http://rubygems.org/gems/proxies"
11
+ s.summary = "Proxies can even proxy other proxies"
12
+ s.description = "Proxies can even proxy other proxies"
9
13
 
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
- ]
14
+ s.required_rubygems_version = ">= 1.3.6"
15
+ s.rubyforge_project = "proxies"
52
16
 
53
- if s.respond_to? :specification_version then
54
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
55
- s.specification_version = 3
17
+ s.add_development_dependency "bundler", ">= 1.0.0"
18
+ s.add_development_dependency "test-unit", ">= 2"
19
+ s.add_development_dependency "mocha"
56
20
 
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
21
+ s.files = `git ls-files`.split("\n")
22
+ s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
23
+ s.require_path = 'lib'
68
24
  end
69
-
@@ -1,12 +1,7 @@
1
- require "rubygems"
2
- gem "test-unit"
3
1
  require "test/unit"
4
2
  require "mocha"
5
-
6
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
7
- $LOAD_PATH.unshift(File.dirname(__FILE__))
8
3
  require "proxies"
9
4
 
10
- class Test::Unit::TestCase
5
+ class Proxies::TestCase < Test::Unit::TestCase
11
6
  include Proxies
12
7
  end
@@ -12,7 +12,7 @@ module Three
12
12
  def three; 3 end
13
13
  end
14
14
 
15
- class TestMethodProxyExtend < Test::Unit::TestCase
15
+ class TestProxyExtend < Proxies::TestCase
16
16
  def setup
17
17
  @target = "target"
18
18
  @object = mock
@@ -20,14 +20,14 @@ class TestMethodProxyExtend < Test::Unit::TestCase
20
20
  end
21
21
 
22
22
  test "extend works properly with array" do
23
- proxy = MethodProxy.new(@object, :target_method, :extend => [One, Two, Three])
23
+ proxy = Proxy.new(lambda { @object.target_method }, :extend => [One, Two, Three])
24
24
  assert_equal 1, proxy.one
25
25
  assert_equal 2, proxy.two
26
26
  assert_equal 3, proxy.three
27
27
  end
28
28
 
29
29
  test "extend works with single value" do
30
- proxy = MethodProxy.new(@object, :target_method, :extend => Two)
30
+ proxy = Proxy.new(lambda { @object.target_method }, :extend => Two)
31
31
  assert_equal 2, proxy.two
32
32
  end
33
33
  end
@@ -0,0 +1,24 @@
1
+ require "helper"
2
+
3
+ class TestProxyOwner < Proxies::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 = Proxy.new(lambda { @object.target_method }, :owner => @object)
12
+ assert_equal @object, proxy.proxy_owner
13
+ end
14
+
15
+ test "proxy responds to proxy_owner" do
16
+ proxy = Proxy.new(lambda { @object.target_method }, :owner => @object)
17
+ assert proxy.proxy_respond_to?(:proxy_owner)
18
+ end
19
+
20
+ test "proxy does not respond to non_existing_method" do
21
+ proxy = Proxy.new(lambda { @object.target_method }, :owner => @object)
22
+ assert_false proxy.proxy_respond_to?(:non_existing_method)
23
+ end
24
+ end
@@ -0,0 +1,19 @@
1
+ require "helper"
2
+
3
+ class TestProxyRespondTo < Proxies::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_respond_to? returns true on existing method" do
11
+ proxy = Proxy.new(lambda { @object.target_method }, :owner => @object)
12
+ assert proxy.proxy_respond_to?(:proxy_owner)
13
+ end
14
+
15
+ test "proxy_respond_to? returns false on non-existing method" do
16
+ proxy = Proxy.new(lambda { @object.target_method }, :owner => @object)
17
+ assert_false proxy.proxy_respond_to?(:non_existing_method)
18
+ end
19
+ end
@@ -0,0 +1,61 @@
1
+ require "helper"
2
+
3
+ class TestProxyTarget < Proxies::TestCase
4
+ def setup
5
+ @target = "target"
6
+ @object = mock
7
+ end
8
+
9
+ test "non-existing proxy method call is passed to the target" do
10
+ @object.expects(:target_method).once.returns(@target)
11
+ proxy = Proxy.new(lambda { @object.target_method })
12
+ assert_equal @target.length, proxy.length
13
+ end
14
+
15
+ test "proxy method returns correct result" do
16
+ @object.expects(:target_method).once.returns(@target)
17
+ proxy = Proxy.new(lambda { @object.target_method }) do
18
+ def length_plus_one
19
+ proxy_target.length + 1
20
+ end
21
+ end
22
+ assert_equal @target.length + 1, proxy.length_plus_one
23
+ end
24
+
25
+ test "object_id method call is passed to proxy target" do
26
+ @object.expects(:target_method).once.returns(@target)
27
+ proxy = Proxy.new(lambda { @object.target_method })
28
+ assert_equal @target.object_id, proxy.object_id
29
+ end
30
+
31
+ test "send method call is passed to proxy target" do
32
+ @object.expects(:target_method).once.returns(@target)
33
+ proxy = Proxy.new(lambda { @object.target_method })
34
+ assert_equal @target.send(:length), proxy.send(:length)
35
+ end
36
+
37
+ test "== method is passed to proxy_target" do
38
+ @object.expects(:target_method).once.returns(@target)
39
+ proxy = Proxy.new(lambda { @object.target_method })
40
+ assert @target == proxy
41
+ assert proxy == @target
42
+ end
43
+
44
+ test "equal? method is passed to proxy_target" do
45
+ @object.expects(:target_method).once.returns(@target)
46
+ proxy = Proxy.new(lambda { @object.target_method })
47
+ assert proxy.equal?(@target)
48
+ end
49
+
50
+ test "target method is not called if not needed" do
51
+ proxy = Proxy.new(lambda { @object.target_method }, :owner => @object)
52
+ assert_equal @object, proxy.proxy_owner
53
+ end
54
+
55
+ test "lazy target is called once even if false returned" do
56
+ proxy = Proxy.new(lambda { @object.target_method }, :owner => @object)
57
+ @object.expects(:target_method).once.returns(false)
58
+ assert_equal "false", proxy.to_s
59
+ assert_equal "false", proxy.to_s
60
+ end
61
+ end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
+ - 2
7
8
  - 1
8
- - 1
9
- version: 0.1.1
9
+ version: 0.2.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - "Jakub Ku\xC5\xBAma"
@@ -14,13 +14,28 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-01 00:00:00 +02:00
17
+ date: 2010-09-25 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
- name: test-unit
22
- prerelease: false
21
+ name: bundler
23
22
  requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 0
30
+ - 0
31
+ version: 1.0.0
32
+ type: :development
33
+ prerelease: false
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: test-unit
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
24
39
  requirements:
25
40
  - - ">="
26
41
  - !ruby/object:Gem::Version
@@ -28,11 +43,12 @@ dependencies:
28
43
  - 2
29
44
  version: "2"
30
45
  type: :development
31
- version_requirements: *id001
46
+ prerelease: false
47
+ version_requirements: *id002
32
48
  - !ruby/object:Gem::Dependency
33
49
  name: mocha
34
- prerelease: false
35
- requirement: &id002 !ruby/object:Gem::Requirement
50
+ requirement: &id003 !ruby/object:Gem::Requirement
51
+ none: false
36
52
  requirements:
37
53
  - - ">="
38
54
  - !ruby/object:Gem::Version
@@ -40,69 +56,67 @@ dependencies:
40
56
  - 0
41
57
  version: "0"
42
58
  type: :development
43
- version_requirements: *id002
59
+ prerelease: false
60
+ version_requirements: *id003
44
61
  description: Proxies can even proxy other proxies
45
- email: qoobaa@gmail.com
62
+ email:
63
+ - qoobaa@gmail.com
46
64
  executables: []
47
65
 
48
66
  extensions: []
49
67
 
50
- extra_rdoc_files:
51
- - LICENSE
52
- - README.rdoc
68
+ extra_rdoc_files: []
69
+
53
70
  files:
54
- - .document
55
71
  - .gitignore
72
+ - Gemfile
73
+ - Gemfile.lock
56
74
  - LICENSE
57
75
  - README.rdoc
58
76
  - Rakefile
59
- - VERSION
60
77
  - lib/proxies.rb
61
- - lib/proxies/method_proxy.rb
62
- - lib/proxies/object_proxy.rb
78
+ - lib/proxies/proxy.rb
79
+ - lib/proxies/version.rb
63
80
  - proxies.gemspec
64
81
  - 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
82
+ - test/test_proxy_extend.rb
83
+ - test/test_proxy_owner.rb
84
+ - test/test_proxy_respond_to.rb
85
+ - test/test_proxy_target.rb
71
86
  has_rdoc: true
72
- homepage: http://github.com/qoobaa/proxies
87
+ homepage: http://rubygems.org/gems/proxies
73
88
  licenses: []
74
89
 
75
90
  post_install_message:
76
- rdoc_options:
77
- - --charset=UTF-8
91
+ rdoc_options: []
92
+
78
93
  require_paths:
79
94
  - lib
80
95
  required_ruby_version: !ruby/object:Gem::Requirement
96
+ none: false
81
97
  requirements:
82
98
  - - ">="
83
99
  - !ruby/object:Gem::Version
100
+ hash: 3699584780831755527
84
101
  segments:
85
102
  - 0
86
103
  version: "0"
87
104
  required_rubygems_version: !ruby/object:Gem::Requirement
105
+ none: false
88
106
  requirements:
89
107
  - - ">="
90
108
  - !ruby/object:Gem::Version
91
109
  segments:
92
- - 0
93
- version: "0"
110
+ - 1
111
+ - 3
112
+ - 6
113
+ version: 1.3.6
94
114
  requirements: []
95
115
 
96
- rubyforge_project:
97
- rubygems_version: 1.3.6
116
+ rubyforge_project: proxies
117
+ rubygems_version: 1.3.7
98
118
  signing_key:
99
119
  specification_version: 3
100
120
  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
121
+ test_files: []
122
+
data/.document DELETED
@@ -1,5 +0,0 @@
1
- README.rdoc
2
- lib/**/*.rb
3
- bin/*
4
- features/**/*.feature
5
- LICENSE
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.1.1
@@ -1,50 +0,0 @@
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 = ::Kernel.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
@@ -1,50 +0,0 @@
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 = ::Kernel.Array(options[:extend])
37
- extends << ::Module.new(&block)
38
- extends << ::Proxies::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
@@ -1,15 +0,0 @@
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
@@ -1,54 +0,0 @@
1
- require "helper"
2
-
3
- class TestMethodProxyTarget < Test::Unit::TestCase
4
- def setup
5
- @target = "target"
6
- @object = mock
7
- end
8
-
9
- test "non-existing proxy method call is passed to the target" do
10
- @object.expects(:target_method).once.returns(@target)
11
- @proxy = MethodProxy.new(@object, :target_method)
12
- assert_equal @target.length, @proxy.length
13
- end
14
-
15
- test "proxy method returns correct result" do
16
- @object.expects(:target_method).once.returns(@target)
17
- @proxy = MethodProxy.new(@object, :target_method) do
18
- def length_plus_one
19
- proxy_target.length + 1
20
- end
21
- end
22
- assert_equal @target.length + 1, @proxy.length_plus_one
23
- end
24
-
25
- test "object_id method call is passed to proxy target" do
26
- @object.expects(:target_method).once.returns(@target)
27
- @proxy = MethodProxy.new(@object, :target_method)
28
- assert_equal @target.object_id, @proxy.object_id
29
- end
30
-
31
- test "send method call is passed to proxy target" do
32
- @object.expects(:target_method).once.returns(@target)
33
- @proxy = MethodProxy.new(@object, :target_method)
34
- assert_equal @target.send(:length), @proxy.send(:length)
35
- end
36
-
37
- test "== method is passed to proxy_target" do
38
- @object.expects(:target_method).once.returns(@target)
39
- @proxy = MethodProxy.new(@object, :target_method)
40
- assert @target == @proxy
41
- assert @proxy == @target
42
- end
43
-
44
- test "equal? method is passed to proxy_target" do
45
- @object.expects(:target_method).once.returns(@target)
46
- @proxy = MethodProxy.new(@object, :target_method)
47
- assert @proxy.equal?(@target)
48
- end
49
-
50
- test "target_method is not called if not needed" do
51
- @proxy = MethodProxy.new(@object, :target_method)
52
- assert_equal @object, @proxy.proxy_owner
53
- end
54
- end
@@ -1,27 +0,0 @@
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
@@ -1,35 +0,0 @@
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
@@ -1,37 +0,0 @@
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