blankslate 2.1.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README +31 -0
  2. data/lib/blankslate.rb +109 -0
  3. metadata +58 -0
data/README ADDED
@@ -0,0 +1,31 @@
1
+ BlankSlate
2
+ ===
3
+
4
+ BlankSlate provides an abstract base class with no predefined
5
+ methods (except for <tt>\_\_send__</tt> and <tt>\_\_id__</tt>).
6
+ BlankSlate is useful as a base class when writing classes that
7
+ depend upon <tt>method_missing</tt> (e.g. dynamic proxies).
8
+
9
+ Copyright 2004, 2006 by Jim Weirich (jim@weirichhouse.org).
10
+ All rights reserved.
11
+
12
+
13
+ Extracted from Builder because, for no conceivable reason,
14
+ blankslate isn't in its own gem. Gemified by David Masover,
15
+ moved to gemcutter by Jack Danger Canty (gemcutter@6brand.com
16
+ if you'd like to own this gem).
17
+
18
+ Explanation on extraction from David Masover:
19
+
20
+ So, Builder seems to have the most complete implementation of
21
+ BlankSlate, short of Ruby 1.9's BasicObject. The problem is,
22
+ this is part of Builder, and still inside the Builder gem.
23
+
24
+ It's especially frustrating, because the Builder source
25
+ (lib/builder/blankslate.rb) seems to acknowledge that there
26
+ should be a separate gem. But the only reference I can find
27
+ refers to onestepback.org's gem repository, which isn't working.
28
+
29
+ So I built my own. I'll try to keep it up to date with Builder.
30
+ The first three parts of the version number are
31
+ the Builder version; the last part is my revision.
data/lib/blankslate.rb ADDED
@@ -0,0 +1,109 @@
1
+ #!/usr/bin/env ruby
2
+ #--
3
+ # Copyright 2004, 2006 by Jim Weirich (jim@weirichhouse.org).
4
+ # All rights reserved.
5
+
6
+ # Permission is granted for use, copying, modification, distribution,
7
+ # and distribution of modified versions of this work as long as the
8
+ # above copyright notice is included.
9
+ #++
10
+
11
+ ######################################################################
12
+ # BlankSlate provides an abstract base class with no predefined
13
+ # methods (except for <tt>\_\_send__</tt> and <tt>\_\_id__</tt>).
14
+ # BlankSlate is useful as a base class when writing classes that
15
+ # depend upon <tt>method_missing</tt> (e.g. dynamic proxies).
16
+ #
17
+ class BlankSlate
18
+ class << self
19
+
20
+ # Hide the method named +name+ in the BlankSlate class. Don't
21
+ # hide +instance_eval+ or any method beginning with "__".
22
+ def hide(name)
23
+ if instance_methods.include?(name.to_s) and
24
+ name !~ /^(__|instance_eval)/
25
+ @hidden_methods ||= {}
26
+ @hidden_methods[name.to_sym] = instance_method(name)
27
+ undef_method name
28
+ end
29
+ end
30
+
31
+ def find_hidden_method(name)
32
+ @hidden_methods ||= {}
33
+ @hidden_methods[name] || superclass.find_hidden_method(name)
34
+ end
35
+
36
+ # Redefine a previously hidden method so that it may be called on a blank
37
+ # slate object.
38
+ def reveal(name)
39
+ hidden_method = find_hidden_method(name)
40
+ fail "Don't know how to reveal method '#{name}'" unless hidden_method
41
+ define_method(name, hidden_method)
42
+ end
43
+ end
44
+
45
+ instance_methods.each { |m| hide(m) }
46
+ end
47
+
48
+ ######################################################################
49
+ # Since Ruby is very dynamic, methods added to the ancestors of
50
+ # BlankSlate <em>after BlankSlate is defined</em> will show up in the
51
+ # list of available BlankSlate methods. We handle this by defining a
52
+ # hook in the Object and Kernel classes that will hide any method
53
+ # defined after BlankSlate has been loaded.
54
+ #
55
+ module Kernel
56
+ class << self
57
+ alias_method :blank_slate_method_added, :method_added
58
+
59
+ # Detect method additions to Kernel and remove them in the
60
+ # BlankSlate class.
61
+ def method_added(name)
62
+ result = blank_slate_method_added(name)
63
+ return result if self != Kernel
64
+ BlankSlate.hide(name)
65
+ result
66
+ end
67
+ end
68
+ end
69
+
70
+ ######################################################################
71
+ # Same as above, except in Object.
72
+ #
73
+ class Object
74
+ class << self
75
+ alias_method :blank_slate_method_added, :method_added
76
+
77
+ # Detect method additions to Object and remove them in the
78
+ # BlankSlate class.
79
+ def method_added(name)
80
+ result = blank_slate_method_added(name)
81
+ return result if self != Object
82
+ BlankSlate.hide(name)
83
+ result
84
+ end
85
+
86
+ def find_hidden_method(name)
87
+ nil
88
+ end
89
+ end
90
+ end
91
+
92
+ ######################################################################
93
+ # Also, modules included into Object need to be scanned and have their
94
+ # instance methods removed from blank slate. In theory, modules
95
+ # included into Kernel would have to be removed as well, but a
96
+ # "feature" of Ruby prevents late includes into modules from being
97
+ # exposed in the first place.
98
+ #
99
+ class Module
100
+ alias blankslate_original_append_features append_features
101
+ def append_features(mod)
102
+ result = blankslate_original_append_features(mod)
103
+ return result if mod != Object
104
+ instance_methods.each do |name|
105
+ BlankSlate.hide(name)
106
+ end
107
+ result
108
+ end
109
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: blankslate
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.1.2.2
5
+ platform: ruby
6
+ authors:
7
+ - Jim Weirich
8
+ - David Masover
9
+ - Jack Danger Canty
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+
14
+ date: 2009-01-03 00:00:00 -08:00
15
+ default_executable:
16
+ dependencies: []
17
+
18
+ description:
19
+ email: dave@3mix.com
20
+ executables: []
21
+
22
+ extensions: []
23
+
24
+ extra_rdoc_files: []
25
+
26
+ files:
27
+ - README
28
+ - lib/blankslate.rb
29
+ has_rdoc: true
30
+ homepage:
31
+ licenses: []
32
+
33
+ post_install_message:
34
+ rdoc_options: []
35
+
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: "0"
43
+ version:
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ requirements: []
51
+
52
+ rubyforge_project:
53
+ rubygems_version: 1.3.5
54
+ signing_key:
55
+ specification_version: 3
56
+ summary: BlankSlate extracted from Builder.
57
+ test_files: []
58
+