Class: Console1984::Shield::MethodInvocationShell
- Inherits:
-
Object
- Object
- Console1984::Shield::MethodInvocationShell
- Includes:
- Freezeable
- Defined in:
- lib/console1984/shield/method_invocation_shell.rb
Overview
Prevents invoking a configurable set of methods
Instance Attribute Summary collapse
-
#class_name ⇒ Object
readonly
Returns the value of attribute class_name.
-
#methods ⇒ Object
readonly
Returns the value of attribute methods.
-
#only_for_user_commands ⇒ Object
readonly
Returns the value of attribute only_for_user_commands.
Class Method Summary collapse
Instance Method Summary collapse
- #build_protection_module ⇒ Object
-
#initialize(invocation, only_for_user_commands:) ⇒ MethodInvocationShell
constructor
A new instance of MethodInvocationShell.
- #prevent_methods_invocation ⇒ Object
- #protected_method_invocation_source_for(method) ⇒ Object
- #protected_method_invocations_source ⇒ Object
Methods included from Freezeable
Constructor Details
#initialize(invocation, only_for_user_commands:) ⇒ MethodInvocationShell
Returns a new instance of MethodInvocationShell.
14 15 16 17 18 |
# File 'lib/console1984/shield/method_invocation_shell.rb', line 14 def initialize(invocation, only_for_user_commands:) @class_name, methods = invocation.to_a @methods = Array(methods) @only_for_user_commands = only_for_user_commands end |
Instance Attribute Details
#class_name ⇒ Object (readonly)
Returns the value of attribute class_name.
12 13 14 |
# File 'lib/console1984/shield/method_invocation_shell.rb', line 12 def class_name @class_name end |
#methods ⇒ Object (readonly)
Returns the value of attribute methods.
12 13 14 |
# File 'lib/console1984/shield/method_invocation_shell.rb', line 12 def methods @methods end |
#only_for_user_commands ⇒ Object (readonly)
Returns the value of attribute only_for_user_commands.
12 13 14 |
# File 'lib/console1984/shield/method_invocation_shell.rb', line 12 def only_for_user_commands @only_for_user_commands end |
Class Method Details
.install_for(config) ⇒ Object
6 7 8 9 |
# File 'lib/console1984/shield/method_invocation_shell.rb', line 6 def install_for(config) Array(config[:user]).each { |invocation| self.new(invocation, only_for_user_commands: true).prevent_methods_invocation } Array(config[:system]).each { |invocation| self.new(invocation, only_for_user_commands: false).prevent_methods_invocation } end |
Instance Method Details
#build_protection_module ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/console1984/shield/method_invocation_shell.rb', line 24 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 ⇒ Object
20 21 22 |
# File 'lib/console1984/shield/method_invocation_shell.rb', line 20 def prevent_methods_invocation class_name.constantize.prepend build_protection_module end |
#protected_method_invocation_source_for(method) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/console1984/shield/method_invocation_shell.rb', line 37 def protected_method_invocation_source_for(method) <<~RUBY def #{method}(*args) if (!#{only_for_user_commands} || Console1984.command_executor.executing_user_command?) && caller.find do |line| line_from_irb = line =~ /^[^\\/]/ break if !(line =~ /console1984\\/lib/ || line_from_irb) line_from_irb end raise Console1984::Errors::ForbiddenCommand else super end end RUBY end |
#protected_method_invocations_source ⇒ Object
33 34 35 |
# File 'lib/console1984/shield/method_invocation_shell.rb', line 33 def protected_method_invocations_source methods.collect { |method| protected_method_invocation_source_for(method) }.join("\n") end |