class Console1984::Shield::MethodInvocationShell

Prevents invoking a configurable set of methods

Attributes

class_name[R]
methods[R]
only_for_user_commands[R]

Public Class Methods

install_for(invocations) click to toggle source
# File lib/console1984/shield/method_invocation_shell.rb, line 6
def install_for(invocations)
  Array(invocations).each { |invocation| self.new(invocation).prevent_methods_invocation }
end
new(invocation) click to toggle source
# File lib/console1984/shield/method_invocation_shell.rb, line 13
def initialize(invocation)
  @class_name, methods = invocation.to_a
  @methods = Array(methods)
end

Public Instance Methods

build_protection_module() click to toggle source
# File lib/console1984/shield/method_invocation_shell.rb, line 22
  def build_protection_module
    source = protected_method_invocations_source
    Module.new do
      class_eval <<~RUBY, __FILE__, __LINE__ + 1
        #{source}
      RUBY
    end
  end
prevent_methods_invocation() click to toggle source
# File lib/console1984/shield/method_invocation_shell.rb, line 18
def prevent_methods_invocation
  class_name.to_s.constantize.prepend build_protection_module
end
protected_method_invocation_source_for(method) click to toggle source
# File lib/console1984/shield/method_invocation_shell.rb, line 35
  def protected_method_invocation_source_for(method)
    <<~RUBY
      def #{method}(*args)
        if Console1984.command_executor.from_irb?(caller)
          raise Console1984::Errors::ForbiddenCommandAttempted
        else
          super
        end
      end
    RUBY
  end
protected_method_invocations_source() click to toggle source
# File lib/console1984/shield/method_invocation_shell.rb, line 31
def protected_method_invocations_source
  methods.collect { |method| protected_method_invocation_source_for(method) }.join("\n")
end