Sha256: c82d2cf4d5e4e489d2a258fcf5710f7a73884abfacf63e5c1cf0e44699e8e480

Contents?: true

Size: 1.74 KB

Versions: 1

Compression:

Stored size: 1.74 KB

Contents

module Safemode
  class Blankslate
    @@allow_instance_methods = ['class', 'methods', 'respond_to?', 'respond_to_missing?', 'to_s', 'instance_variable_get']
    @@allow_class_methods    = ['methods', 'new', 'name', '<', '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_instance_methods, @allowed_class_methods)
      end

      def init_allowed_methods(allowed_instance_methods, allowed_class_methods)
        @allowed_instance_methods = allowed_instance_methods
        @allowed_class_methods = allowed_class_methods
      end

      def allowed_instance_methods
        @allowed_instance_methods ||= []
      end
      alias_method :allowed_methods, :allowed_instance_methods

      def allowed_class_methods
        @allowed_class_methods ||= []
      end

      def allow_instance_method(*names)
        @allowed_instance_methods = allowed_instance_methods + names.map{|name| name.to_s}
        @allowed_instance_methods.uniq!
      end
      alias_method :allow, :allow_instance_method

      def allow_class_method(*names)
        @allowed_class_methods = allowed_class_methods + names.map{|name| name.to_s}
        @allowed_class_methods.uniq!
      end

      def allowed_instance_method?(name)
        allowed_instance_methods.include? name.to_s
      end
      alias_method :allowed?, :allowed_instance_method?

      def allowed_class_method?(name)
        allowed_class_methods.include? name.to_s
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
safemode-1.3.1 lib/safemode/blankslate.rb