Sha256: f4642fa11e00b44763776a267019882fb3bcc243714a77b96a8e3c2ea87a64a6

Contents?: true

Size: 1.15 KB

Versions: 3

Compression:

Stored size: 1.15 KB

Contents

module Safemode
  class Jail < Blankslate
    def initialize(source = nil)
      @source = source
    end

    def to_jail
      self
    end

    def to_s
      @source.to_s
    end

    def method_missing(method, *args, &block)
      if @source.is_a?(Class)
        unless self.class.allowed_class_method?(method)
          raise Safemode::NoMethodError.new(".#{method}", self.class.name, @source.name)
        end
      else
        unless self.class.allowed_instance_method?(method)
          raise Safemode::NoMethodError.new("##{method}", self.class.name, @source.class.name)
        end
      end

      # As every call to an object in the eval'ed string will be jailed by the
      # parser we don't need to "proactively" jail arrays and hashes. Likewise we
      # don't need to jail objects returned from a jail. Doing so would provide
      # "double" protection, but it also would break using a return value in an if
      # statement, passing them to a Rails helper etc.
      @source.send(method, *args, &block)
    end

    def respond_to_missing?(method_name, include_private = false)
      self.class.allowed_instance_method?(method_name)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
safemode-1.3.8 lib/safemode/jail.rb
safemode-1.3.7 lib/safemode/jail.rb
safemode-1.3.6 lib/safemode/jail.rb