Class: Egi::Sandbox

Inherits:
Object
  • Object
show all
Defined in:
lib/egi/sandbox.rb

Instance Method Summary collapse

Constructor Details

#initializeSandbox

Returns a new instance of Sandbox.



3
4
5
# File 'lib/egi/sandbox.rb', line 3

def initialize
  @current_env = :default
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



34
35
36
37
38
39
40
41
# File 'lib/egi/sandbox.rb', line 34

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

Instance Method Details

#env(name, opts = {}, &block) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/egi/sandbox.rb', line 11

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

#envsObject



7
8
9
# File 'lib/egi/sandbox.rb', line 7

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

#eval(str) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/egi/sandbox.rb', line 20

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