Skip to content

Instantly share code, notes, and snippets.

@emanon-was
Created June 16, 2016 06:18
Show Gist options
  • Select an option

  • Save emanon-was/3fd2b7e162f65e78d80cefece9c72115 to your computer and use it in GitHub Desktop.

Select an option

Save emanon-was/3fd2b7e162f65e78d80cefece9c72115 to your computer and use it in GitHub Desktop.
module SandboxHelper
require 'set'
class Sandbox
def initialize(application)
@base = application
@virt = application.clone
end
def run(&block)
@virt.instance_eval &block
end
def locals
base_vars = Set.new(@base.instance_variables)
hash = @virt
.instance_variables
.select {|var| !(base_vars.include? var)}
.map {|var| {trim(var) => dict(@virt,var)}}
.inject({}) {|ret,h| ret.merge(h)}
return Hashie::Mash.new(hash)
end
def method_missing(name, *args)
@virt.send(name,*args)
end
private
def dict(instance,var)
return instance.instance_variable_get(var)
end
def trim(var)
return var.to_s[1..-1].to_sym
end
def diff
@base.instance_variables.select do |var|
(dict(@base,var) == dict(@virt,var)) ? false : true
end
end
end
def sandbox(&block)
@sandbox ||= Sandbox.new(self)
@sandbox.run &block if block
return @sandbox
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment