Sha256: fbace034fb89070e6b98c6e7da5240bf0a549047662f155733f666ccdf7df000

Contents?: true

Size: 1002 Bytes

Versions: 2

Compression:

Stored size: 1002 Bytes

Contents

module Egi
  class Sandbox
    def initialize
      @current_env = :default
    end

    def envs
      @envs ||= Hash.new {|hash, key| hash[key] = Env.new(key) }
    end

    def env(name, opts = {}, &block)
      name = name.to_sym
      @current_env = name
      
      to_load = opts[:load]
      envs[name].merge!(envs[to_load]) if envs.has_key?(to_load)
      envs[name].instance_eval(&block) if block_given?
    end

    def eval(str)
      instance_eval(str)
      
      # define method_missing to access items
      envs.each_value do |env|
        def env.method_missing(name, *args)
          super if args.size > 0
          items.has_key?(name) ? items[name] : nil
        end
      end

      envs
    end

    def method_missing(name, *args, &block)
      begin
        envs[@current_env].send(name, *args, &block)
      rescue => e
        puts "#{name}(#{args}) #{block_given? ? "with block" : ""} is called at #{@current_env} env"
        raise e
      end        
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
egi-0.0.4 lib/egi/sandbox.rb
egi-0.0.3 lib/egi/sandbox.rb