Class: Safemode::Scope

Inherits:
Blankslate show all
Defined in:
lib/safemode/scope.rb

Instance Method Summary collapse

Methods inherited from Blankslate

allow, allowed?, allowed_methods, inherited, init_allowed_methods, method_added

Constructor Details

#initialize(delegate = nil, delegate_methods = []) ⇒ Scope

Returns a new instance of Scope.



3
4
5
6
7
# File 'lib/safemode/scope.rb', line 3

def initialize(delegate = nil, delegate_methods = [])
  @delegate = delegate        
  @delegate_methods = delegate_methods
  @locals = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/safemode/scope.rb', line 33

def method_missing(method, *args, &block)
  if @locals.has_key?(method)
    @locals[method]
  elsif @delegate_methods.include?(method)
    @delegate.send method, *unjail_args(args), &block
  else
    raise Safemode::SecurityError.new(method, "#<Safemode::ScopeObject>")
  end
end

Instance Method Details

#bind(instance_vars = {}, locals = {}, &block) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/safemode/scope.rb', line 9

def bind(instance_vars = {}, locals = {}, &block)
  @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
end

#outputObject



29
30
31
# File 'lib/safemode/scope.rb', line 29

def output
  @_safemode_output
end


25
26
27
# File 'lib/safemode/scope.rb', line 25

def print(*args)      
  @_safemode_output += args.to_s
end

#puts(*args) ⇒ Object



21
22
23
# File 'lib/safemode/scope.rb', line 21

def puts(*args)
  print args.to_s + "\n"
end

#to_jailObject



17
18
19
# File 'lib/safemode/scope.rb', line 17

def to_jail
  self
end