Sha256: 128d5c374207b5a8dfbc5eb903cda311ebd484fd9a25ae2957cbfe687b570f72

Contents?: true

Size: 1017 Bytes

Versions: 4

Compression:

Stored size: 1017 Bytes

Contents

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 - @@allow_instance_methods) }
    class << self
      silently { undef_methods(*instance_methods - @@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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
safemode-1.0.2 lib/safemode/blankslate.rb
safemode-1.0.1 lib/safemode/blankslate.rb
safemode-1.0.0 lib/safemode/blankslate.rb
safemode-0.0.2 lib/safemode/blankslate.rb