class Safemode::Scope

Public Class Methods

new(delegate = nil, delegate_methods = [], instance_vars: {}, locals: {}, &block) click to toggle source
# File lib/safemode/scope.rb, line 3
def initialize(delegate = nil, delegate_methods = [], instance_vars: {}, locals: {}, &block)
  @delegate = delegate
  @delegate_methods = delegate_methods
  @locals = symbolize_keys(locals) # why can't I just pull them to local scope in the same way like instance_vars?
  instance_vars = symbolize_keys(instance_vars)
  instance_vars.each {|key, obj| eval "@#{key} = instance_vars[:#{key}]" }
  @_safemode_output = ''
  @binding = binding
end

Public Instance Methods

get_binding() click to toggle source
# File lib/safemode/scope.rb, line 13
def get_binding
  @binding
end
method_missing(method, *args, **kwargs, &block) click to toggle source
# File lib/safemode/scope.rb, line 33
def method_missing(method, *args, **kwargs, &block)
  if @locals.has_key?(method)
    @locals[method]
  elsif @delegate_methods.include?(method)
    @delegate.send method, *unjail_args(args), **unjail_kwargs(kwargs), &block
  else
    raise Safemode::SecurityError.new(method, "#<Safemode::ScopeObject>")
  end
end
output() click to toggle source
# File lib/safemode/scope.rb, line 29
def output
  @_safemode_output
end
print(*args) click to toggle source
puts(*args) click to toggle source
# File lib/safemode/scope.rb, line 21
def puts(*args)
  print args.to_s + "\n"
end
to_jail() click to toggle source
# File lib/safemode/scope.rb, line 17
def to_jail
  self
end

Private Instance Methods

symbolize_keys(hash) click to toggle source
# File lib/safemode/scope.rb, line 45
def symbolize_keys(hash)
  hash.inject({}) do |hash, (key, value)|
    hash[key.to_s.intern] = value
    hash
  end
end
unjail(arg) click to toggle source
# File lib/safemode/scope.rb, line 52
def unjail(arg)
  arg.class.name.end_with?('::Jail') ? arg.instance_variable_get(:@source) : arg
end
unjail_args(args) click to toggle source
# File lib/safemode/scope.rb, line 56
def unjail_args(args)
  args.collect { |arg| unjail(arg) }
end
unjail_kwargs(kwargs) click to toggle source
# File lib/safemode/scope.rb, line 60
def unjail_kwargs(kwargs)
  kwargs.map { |key, value| [unjail(key), unjail(value)] }.to_h
end