bzproxies 0.0.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.
- data/.gitignore +7 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/README.md +39 -0
- data/Rakefile +1 -0
- data/bzproxies.gemspec +24 -0
- data/lib/bzproxies/base.rb +32 -0
- data/lib/bzproxies/basic_object.rb +5 -0
- data/lib/bzproxies/core_ext.rb +5 -0
- data/lib/bzproxies/owner.rb +52 -0
- data/lib/bzproxies/stub.rb +16 -0
- data/lib/bzproxies/version.rb +3 -0
- data/lib/bzproxies.rb +5 -0
- data/spec/base_spec.rb +41 -0
- data/spec/basic_object_spec.rb +5 -0
- data/spec/fixtures/base.rb +7 -0
- data/spec/fixtures/owner.rb +30 -0
- data/spec/owner_spec.rb +63 -0
- data/spec/spec_helper.rb +13 -0
- metadata +93 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
Bz proxies
|
2
|
+
=======
|
3
|
+
|
4
|
+
Description
|
5
|
+
-----------
|
6
|
+
Simple proxy and it's holder class module Owner
|
7
|
+
|
8
|
+
Use
|
9
|
+
----
|
10
|
+
require 'bzproxies'
|
11
|
+
|
12
|
+
class MyProxy < Proxies::Base #alias Proxy
|
13
|
+
def additional_methods
|
14
|
+
"additional from proxy"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class SameFunctionalityClass
|
19
|
+
include Proxies::Stub
|
20
|
+
def additional_methods
|
21
|
+
"additional from stub"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class MyProxyOwner
|
26
|
+
include Proxies::Owner #alias Owner
|
27
|
+
|
28
|
+
proxy_accessor :test, :proxy => MyProxy
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
owner = MyProxyOwner.new
|
33
|
+
|
34
|
+
owner.test = "test"
|
35
|
+
owner.test.additional # => "additional from proxy"
|
36
|
+
owner.test.proxie? # => true
|
37
|
+
owner.test = SameFunctionalityClass.new
|
38
|
+
owner.test.additional # => "additional from stub"
|
39
|
+
owner.test.proxie? # => false
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
data/bzproxies.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "bzproxies/version"
|
4
|
+
require "base64"
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "bzproxies"
|
8
|
+
s.version = Bzproxies::VERSION
|
9
|
+
s.authors = ["Bombazook"]
|
10
|
+
s.email = Base64.decode64("Ym9tYmF6b29rQGdtYWlsLmNvbQ==\n")
|
11
|
+
s.homepage = "http://github.com/bombazook/bzproxies"
|
12
|
+
s.summary = "collection of reusable proxies and accessors"
|
13
|
+
s.description = s.summary + "etc."
|
14
|
+
|
15
|
+
s.rubyforge_project = "bzproxies"
|
16
|
+
|
17
|
+
s.add_runtime_dependency "bundler", "~> 1.0"
|
18
|
+
s.add_development_dependency "rspec", "~> 2.6"
|
19
|
+
|
20
|
+
s.files = `git ls-files`.split("\n")
|
21
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
22
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
23
|
+
s.require_paths = ["lib"]
|
24
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'bzproxies/stub'
|
2
|
+
require 'bzproxies/basic_object'
|
3
|
+
|
4
|
+
module Proxies
|
5
|
+
class Base < BasicObject
|
6
|
+
include Stub
|
7
|
+
|
8
|
+
attr_accessor :target
|
9
|
+
|
10
|
+
alias non_recursive_target target
|
11
|
+
|
12
|
+
def target
|
13
|
+
begin
|
14
|
+
@target.target
|
15
|
+
rescue
|
16
|
+
non_recursive_target
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def proxy?
|
21
|
+
true
|
22
|
+
end
|
23
|
+
|
24
|
+
def initialize entity = nil
|
25
|
+
@target = entity
|
26
|
+
end
|
27
|
+
|
28
|
+
def method_missing m, *args, &block
|
29
|
+
@target.send m, *args, &block unless @target.nil?
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require "bzproxies/base"
|
2
|
+
|
3
|
+
module Proxies
|
4
|
+
module Owner
|
5
|
+
|
6
|
+
def self.included(base)
|
7
|
+
base.send :extend, ClassMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def proxy_reader(*args)
|
12
|
+
(load_options args.pop) if args.last.kind_of? Hash
|
13
|
+
attr_reader *args
|
14
|
+
end
|
15
|
+
|
16
|
+
def proxy_writer(*args)
|
17
|
+
load_options :proxy => Proxy
|
18
|
+
(load_options args.pop) if args.last.kind_of? Hash
|
19
|
+
raise ArgumentError, "Unexpected proxy class. Expected 'Class'" unless @proxy.respond_to? :new
|
20
|
+
args.each do |name|
|
21
|
+
raise ArgumentError, "Unexpected argument. Expected String or Symbol" unless name.kind_of? String or name.kind_of? Symbol
|
22
|
+
define_method("#{name}=") do |v|
|
23
|
+
if v.stub?
|
24
|
+
val = v
|
25
|
+
else
|
26
|
+
val = (self.class.instance_variable_get :@proxy).send :new, v
|
27
|
+
end
|
28
|
+
instance_variable_set("@#{name}", val)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def proxy_accessor(*args)
|
34
|
+
proxy_reader(*args)
|
35
|
+
proxy_writer(*args)
|
36
|
+
end
|
37
|
+
|
38
|
+
protected
|
39
|
+
|
40
|
+
def load_options opts
|
41
|
+
opts.each_pair do |key, val|
|
42
|
+
metaclass = class << self; self; end
|
43
|
+
metaclass.send :attr_reader, key
|
44
|
+
instance_variable_set "@#{key}", val
|
45
|
+
end
|
46
|
+
self
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
data/lib/bzproxies.rb
ADDED
data/spec/base_spec.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Proxies::Base do
|
4
|
+
|
5
|
+
it "doesn't alter source object or it's class" do
|
6
|
+
a = "source"
|
7
|
+
meth = a.methods + a.class.methods
|
8
|
+
test = Proxy.new(a).target
|
9
|
+
((test.methods)+(test.class.methods)).should == meth
|
10
|
+
end
|
11
|
+
|
12
|
+
it "stub? methods returns true" do
|
13
|
+
Proxy.new("source").stub?.should be_true
|
14
|
+
end
|
15
|
+
|
16
|
+
it "used by inherited proxy and doesnt proxy its own methods" do
|
17
|
+
Inherited.new.proxy_method.should be_true
|
18
|
+
end
|
19
|
+
|
20
|
+
it "return proxy-contained object when target called" do
|
21
|
+
a = "source"
|
22
|
+
inherited = Inherited.new(a)
|
23
|
+
proxy = Proxy.new(a)
|
24
|
+
|
25
|
+
inherited.target.should be_equal(a)
|
26
|
+
proxy.target.should be_equal(a)
|
27
|
+
a.should be_equal(inherited.target)
|
28
|
+
a.should be_equal(proxy.target)
|
29
|
+
|
30
|
+
(inherited.equal? a).should be_false
|
31
|
+
(proxy.equal? a).should be_false
|
32
|
+
a.should_not be_equal(inherited)
|
33
|
+
a.should_not be_equal(proxy)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "return proxy-contained object of 2-level proxy" do
|
37
|
+
a = "source"
|
38
|
+
two_level = Inherited.new(Proxy.new(a))
|
39
|
+
a.should be_equal two_level.target
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'bzproxies'
|
2
|
+
|
3
|
+
class Ex1
|
4
|
+
include Proxies::Owner
|
5
|
+
proxy_reader :test, :proxy => Proxies::Base
|
6
|
+
def test= any
|
7
|
+
@test = any
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class Ex2
|
12
|
+
include Proxies::Owner
|
13
|
+
proxy_writer :test
|
14
|
+
def test
|
15
|
+
@test
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class Ex3
|
20
|
+
include Proxies::Owner
|
21
|
+
proxy_accessor :test
|
22
|
+
end
|
23
|
+
|
24
|
+
class Ex4
|
25
|
+
include Proxies::Owner
|
26
|
+
end
|
27
|
+
|
28
|
+
class Ex5
|
29
|
+
include Proxies::Stub
|
30
|
+
end
|
data/spec/owner_spec.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
|
4
|
+
describe Proxies::Owner do
|
5
|
+
|
6
|
+
it "works when options hash ommited" do
|
7
|
+
expect {Ex2.new}.not_to raise_error
|
8
|
+
end
|
9
|
+
|
10
|
+
it "creates singleton private class attr_reader and variables from hash" do
|
11
|
+
Ex4.send :load_options, :test => "test"
|
12
|
+
(Ex4.send :test).should == "test"
|
13
|
+
end
|
14
|
+
|
15
|
+
it "sets new value, when variable exists" do
|
16
|
+
a = "test"
|
17
|
+
Ex4.send :load_options, :test => "test"
|
18
|
+
(Ex4.send :test).should == "test"
|
19
|
+
Ex4.send :load_options, :test => "test2"
|
20
|
+
(Ex4.send :test).should == "test2"
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "proxy_reader" do
|
24
|
+
it "creates proxy object attr_accessor" do
|
25
|
+
a = "source"
|
26
|
+
owner = Ex1.new
|
27
|
+
owner.methods.should include :test
|
28
|
+
owner.test = Proxy.new(a)
|
29
|
+
owner.test.proxy?.should be_true #testing method
|
30
|
+
end
|
31
|
+
|
32
|
+
it "gets options hash" do
|
33
|
+
(Ex1.send :proxy).should == Proxies::Base
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "proxy_writer" do
|
38
|
+
it "creates attr writes method" do
|
39
|
+
owner = Ex2.new
|
40
|
+
owner.methods.should include :test=
|
41
|
+
end
|
42
|
+
|
43
|
+
it "covers non-proxy object with proxy" do
|
44
|
+
a = "source"
|
45
|
+
owner = Ex2.new
|
46
|
+
owner.test = a
|
47
|
+
owner.test.proxy?.should be_true
|
48
|
+
end
|
49
|
+
|
50
|
+
it "doesnt cover proxy objects and stubs" do
|
51
|
+
a = Proxy.new("source")
|
52
|
+
b = Object.new.extend Proxies::Stub
|
53
|
+
owner = Ex2.new
|
54
|
+
owner.test = a
|
55
|
+
owner.test.equal?(a).should be_true
|
56
|
+
owner.test = b
|
57
|
+
owner.test.equal?(b).should be_true
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
$:.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
|
2
|
+
require 'bzproxies'
|
3
|
+
require 'pathname'
|
4
|
+
|
5
|
+
#loading fixtures
|
6
|
+
fpath = Pathname.new(File.dirname(__FILE__)) + "fixtures"
|
7
|
+
fpath.entries.each do |e|
|
8
|
+
ent = fpath + e
|
9
|
+
load ent if ent.file?
|
10
|
+
end
|
11
|
+
|
12
|
+
RSpec.configure do |c|
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bzproxies
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Bombazook
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-10-10 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: bundler
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ~>
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "1.0"
|
24
|
+
type: :runtime
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: rspec
|
28
|
+
prerelease: false
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ~>
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: "2.6"
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id002
|
37
|
+
description: collection of reusable proxies and accessorsetc.
|
38
|
+
email: bombazook@gmail.com
|
39
|
+
executables: []
|
40
|
+
|
41
|
+
extensions: []
|
42
|
+
|
43
|
+
extra_rdoc_files: []
|
44
|
+
|
45
|
+
files:
|
46
|
+
- .gitignore
|
47
|
+
- .rspec
|
48
|
+
- Gemfile
|
49
|
+
- README.md
|
50
|
+
- Rakefile
|
51
|
+
- bzproxies.gemspec
|
52
|
+
- lib/bzproxies.rb
|
53
|
+
- lib/bzproxies/base.rb
|
54
|
+
- lib/bzproxies/basic_object.rb
|
55
|
+
- lib/bzproxies/core_ext.rb
|
56
|
+
- lib/bzproxies/owner.rb
|
57
|
+
- lib/bzproxies/stub.rb
|
58
|
+
- lib/bzproxies/version.rb
|
59
|
+
- spec/base_spec.rb
|
60
|
+
- spec/basic_object_spec.rb
|
61
|
+
- spec/fixtures/base.rb
|
62
|
+
- spec/fixtures/owner.rb
|
63
|
+
- spec/owner_spec.rb
|
64
|
+
- spec/spec_helper.rb
|
65
|
+
homepage: http://github.com/bombazook/bzproxies
|
66
|
+
licenses: []
|
67
|
+
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options: []
|
70
|
+
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: "0"
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: "0"
|
85
|
+
requirements: []
|
86
|
+
|
87
|
+
rubyforge_project: bzproxies
|
88
|
+
rubygems_version: 1.8.10
|
89
|
+
signing_key:
|
90
|
+
specification_version: 3
|
91
|
+
summary: collection of reusable proxies and accessors
|
92
|
+
test_files: []
|
93
|
+
|