/
blankslate.rb
34 lines (27 loc) · 1.02 KB
/
blankslate.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
module Safemode
class Blankslate
@@allow_instance_methods = ['class', 'inspect', 'methods', 'respond_to?', 'to_s', 'instance_variable_get']
@@allow_class_methods = ['methods', 'new', 'name', 'inspect', '<', 'ancestors', '=='] # < needed in Rails Object#subclasses_of
silently { undef_methods(*instance_methods.map(&:to_s) - @@allow_instance_methods) }
class << self
silently { undef_methods(*instance_methods.map(&:to_s) - @@allow_class_methods) }
def method_added(name) end # ActiveSupport needs this
def inherited(subclass)
subclass.init_allowed_methods(@allowed_methods)
end
def init_allowed_methods(allowed_methods)
@allowed_methods = allowed_methods
end
def allowed_methods
@allowed_methods ||= []
end
def allow(*names)
@allowed_methods = allowed_methods + names.map{|name| name.to_s}
@allowed_methods.uniq!
end
def allowed?(name)
allowed_methods.include? name.to_s
end
end
end
end