Sha256: 8d0b0a08283c2783f9375e07d1c20a887b7e53899a53825e023df0a0a448ca8f

Contents?: true

Size: 939 Bytes

Versions: 12

Compression:

Stored size: 939 Bytes

Contents

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

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
github-safegem-0.1.2 lib/safegem/lazy_dir.rb
github-safegem-0.1.3 lib/safegem/lazy_dir.rb
github-safegem-0.2.0 lib/safegem/lazy_dir.rb
github-safegem-0.2.10 lib/safegem/lazy_dir.rb
github-safegem-0.2.2 lib/safegem/lazy_dir.rb
github-safegem-0.2.3 lib/safegem/lazy_dir.rb
github-safegem-0.2.4 lib/safegem/lazy_dir.rb
github-safegem-0.2.5 lib/safegem/lazy_dir.rb
github-safegem-0.2.6 lib/safegem/lazy_dir.rb
github-safegem-0.2.7 lib/safegem/lazy_dir.rb
github-safegem-0.2.8 lib/safegem/lazy_dir.rb
github-safegem-0.2.9 lib/safegem/lazy_dir.rb