Created
June 16, 2016 06:18
-
-
Save emanon-was/3fd2b7e162f65e78d80cefece9c72115 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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