acts_as_wrapped_class 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +4 -0
- data/README.txt +15 -15
- data/Rakefile +3 -0
- data/lib/acts_as_wrapped_class.rb +17 -6
- data/lib/wrapper_base.rb +3 -1
- data/test/test_acts_as_wrapped_class.rb +13 -1
- metadata +3 -3
data/History.txt
CHANGED
data/README.txt
CHANGED
@@ -12,22 +12,22 @@ ActsAsWrappedClass is designed to automatically generate a wrapper for an object
|
|
12
12
|
|
13
13
|
== SYNOPSIS:
|
14
14
|
|
15
|
-
class Something
|
16
|
-
|
17
|
-
|
15
|
+
class Something
|
16
|
+
acts_as_wrapped_class :methods => [:safe_method]
|
17
|
+
# SomethingWrapper is now defined
|
18
18
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
end
|
26
|
-
|
27
|
-
s = Something.new
|
28
|
-
wrapper = s.to_wrapper
|
29
|
-
wrapper.safe_method # returns a new SomethingWrapper
|
30
|
-
wrapper.unsafe_method # raises an exception
|
19
|
+
def safe_method # allowed to access this method through SomethingWrapper
|
20
|
+
Something.new
|
21
|
+
end
|
22
|
+
|
23
|
+
def unsafe_method # not allowed to access this method through SomethingWrapper
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
s = Something.new
|
28
|
+
wrapper = s.to_wrapper
|
29
|
+
wrapper.safe_method # returns a new SomethingWrapper
|
30
|
+
wrapper.unsafe_method # raises an exception
|
31
31
|
|
32
32
|
== REQUIREMENTS:
|
33
33
|
|
data/Rakefile
CHANGED
@@ -12,6 +12,9 @@ Hoe.new('acts_as_wrapped_class', ActsAsWrappedClass::VERSION) do |p|
|
|
12
12
|
p.description = p.paragraphs_of('README.txt', 2..5).join("\n\n")
|
13
13
|
p.url = p.paragraphs_of('README.txt', 0).first.split(/\n/)[1..-1]
|
14
14
|
p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
|
15
|
+
p.rubyforge_name = "wrapped-class"
|
16
|
+
p.remote_rdoc_dir = '' # Release to root
|
17
|
+
p.rsync_args << ' --exclude=statsvn/'
|
15
18
|
end
|
16
19
|
|
17
20
|
# vim: syntax=Ruby
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require "erb"
|
2
2
|
|
3
3
|
module ActsAsWrappedClass
|
4
|
-
VERSION = "1.0.
|
4
|
+
VERSION = "1.0.1"
|
5
5
|
WRAPPED_CLASSES = []
|
6
6
|
|
7
7
|
module InstanceMethods
|
@@ -61,10 +61,11 @@ module ActsAsWrappedClass
|
|
61
61
|
# * options[:constants] contains a list of constant names (symbols) to allow access to
|
62
62
|
# * options[:except_constants] contains a list of constant names (symbols) to not allow access to
|
63
63
|
def acts_as_wrapped_class(options = {})
|
64
|
+
|
64
65
|
raise "Can't specify methods to allow and to deny." if options[:methods] && options[:except_methods]
|
65
66
|
raise "Can't specify constants to allow and to deny." if options[:constants] && options[:except_constants]
|
66
|
-
options[:methods] ||= :all
|
67
|
-
options[:constants] ||= :all
|
67
|
+
options[:methods] ||= :all unless options[:except_methods]
|
68
|
+
options[:constants] ||= :all unless options[:except_constants]
|
68
69
|
|
69
70
|
WRAPPED_CLASSES << self
|
70
71
|
|
@@ -76,7 +77,7 @@ module ActsAsWrappedClass
|
|
76
77
|
if options[:constants] == :all
|
77
78
|
options.delete(:constants)
|
78
79
|
options[:except_constants] = []
|
79
|
-
end
|
80
|
+
end
|
80
81
|
|
81
82
|
meths = options[:methods] || options[:except_methods]
|
82
83
|
consts = options[:constants] || options[:except_constants]
|
@@ -129,11 +130,21 @@ module ActsAsWrappedClass
|
|
129
130
|
|
130
131
|
method_defs = ERB.new(method_defs_erb).result(binding)
|
131
132
|
|
133
|
+
allowed_methods = <<-EOF
|
134
|
+
def self.allowed_methods
|
135
|
+
#{ options[:except_methods] ?
|
136
|
+
("#{self.name}.public_instance_methods - [" + (meths + WrapperBase::ALLOWED_METHODS + ["to_wrapper"]).collect{|m| "\"#{m}\"" }.join(", ") + "]") :
|
137
|
+
("[" + meths.collect{|m| "\"#{m}\""}.join(", ") + "]")
|
138
|
+
}
|
139
|
+
end
|
140
|
+
EOF
|
141
|
+
|
132
142
|
wrapper_class_code = <<-EOF
|
133
143
|
class #{self.name}Wrapper < WrapperBase
|
134
|
-
#{method_defs}
|
144
|
+
#{method_defs}
|
145
|
+
#{allowed_methods}
|
135
146
|
end
|
136
|
-
EOF
|
147
|
+
EOF
|
137
148
|
|
138
149
|
eval wrapper_class_code, TOPLEVEL_BINDING
|
139
150
|
end
|
data/lib/wrapper_base.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
class WrapperBase
|
2
|
-
|
2
|
+
ALLOWED_METHODS = ["__id__","__send__", "is_a?", "kind_of?", "hash", "class", "inspect"]
|
3
|
+
|
4
|
+
eval((public_instance_methods - ALLOWED_METHODS).collect{|meth| "undef "+meth}.join("; "))
|
3
5
|
|
4
6
|
# Create a wrapper, passing in an object to wrap
|
5
7
|
def initialize(wrapped_object)
|
@@ -28,7 +28,7 @@ end
|
|
28
28
|
class OtherClass
|
29
29
|
SAMPLE_CLASS = SampleClass.new
|
30
30
|
|
31
|
-
acts_as_wrapped_class :
|
31
|
+
acts_as_wrapped_class :except_methods => [:unsafe_method], :constants => :all
|
32
32
|
|
33
33
|
attr_reader :value
|
34
34
|
|
@@ -48,6 +48,10 @@ class OtherClass
|
|
48
48
|
6.6
|
49
49
|
end
|
50
50
|
|
51
|
+
def unsafe_method
|
52
|
+
6.666
|
53
|
+
end
|
54
|
+
|
51
55
|
def hash
|
52
56
|
@value
|
53
57
|
end
|
@@ -117,6 +121,14 @@ class ActsAsWrappedCodeTest < Test::Unit::TestCase
|
|
117
121
|
assert !NotWrappedClass.wrapped_class?
|
118
122
|
end
|
119
123
|
|
124
|
+
def test_allowed_methods
|
125
|
+
assert SampleClassWrapper.allowed_methods.is_a?(Array)
|
126
|
+
assert_equal ["get_other_class", "other_method"], SampleClassWrapper.allowed_methods
|
127
|
+
assert !OtherClassWrapper.allowed_methods.include?("unsafe_method")
|
128
|
+
assert OtherClassWrapper.allowed_methods.include?("another_method")
|
129
|
+
assert OtherClassWrapper.allowed_methods.include?("get_hash")
|
130
|
+
end
|
131
|
+
|
120
132
|
def assert_contents_same(array1, array2)
|
121
133
|
assert_equal array1.length, array2.length, "#{array1.inspect} != #{array2.inspect}"
|
122
134
|
array1.each { |a| assert array2.include?(a), "#{array2.inspect} does not contain #{a.inspect}" }
|
metadata
CHANGED
@@ -3,14 +3,14 @@ rubygems_version: 0.9.4
|
|
3
3
|
specification_version: 1
|
4
4
|
name: acts_as_wrapped_class
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 1.0.
|
7
|
-
date: 2007-10-
|
6
|
+
version: 1.0.1
|
7
|
+
date: 2007-10-25 00:00:00 -07:00
|
8
8
|
summary: automatically generate wrapper classes which restrict access to methods and constants in the wrapped class
|
9
9
|
require_paths:
|
10
10
|
- lib
|
11
11
|
email: ds@elctech.com
|
12
12
|
homepage: " by David Stevenson "
|
13
|
-
rubyforge_project:
|
13
|
+
rubyforge_project: wrapped-class
|
14
14
|
description: "== FEATURES/PROBLEMS: * Wrappers do not dispatch const_missing yet, so constants are not accessible yet. == SYNOPSIS: class Something acts_as_wrapped_class :methods => [:safe_method] # SomethingWrapper is now defined def safe_method # allowed to access this method through SomethingWrapper Something.new end def unsafe_method # not allowed to access this method through SomethingWrapper end end"
|
15
15
|
autorequire:
|
16
16
|
default_executable:
|