forked from github/gem-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lazy_dir.rb
39 lines (32 loc) · 939 Bytes
/
lazy_dir.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
35
36
37
38
39
class LazyDir < Array
OrigDir = Dir
def initialize(method, args, block = nil)
@method, @args, @block = method, args, block
end
# this method is meant to be called lazily after the $SAFE has reverted to 0
def to_a
raise SecurityError unless %w([] glob).include? @method
files = OrigDir.send(@method, *@args, &@block)
# only return files within the current directory
cur_dir = File.expand_path('.') + File::SEPARATOR
files.reject do |f|
File.expand_path(f) !~ %r{^#{cur_dir}}
end
end
alias_method :to_ary, :to_a
def to_yaml(opts = {})
to_a.to_yaml(opts)
end
class << self
# these methods are meant to be called with tainted data in a $SAFE >= 3
%w(glob []).each do |method_name|
define_method method_name do |*a|
LazyDir.new method_name, a
end
end
def method_missing m, *a, &b
OrigDir.send m, *a, &b
end
end
end
LazyDir.freeze