Class: RubyReportable::Sandbox

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

Instance Method Summary collapse

Constructor Details

#initialize(_methods = {}) ⇒ Sandbox

Returns a new instance of Sandbox.



7
8
9
10
11
12
13
# File 'lib/ruby_reportable/sandbox.rb', line 7

def initialize(_methods = {})
  @values = {}

  _methods.map do |key, value|
    define(key, value)
  end
end

Instance Method Details

#[](key) ⇒ Object



20
21
22
# File 'lib/ruby_reportable/sandbox.rb', line 20

def [](key)
  @values[key]
end

#[]=(key, value) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/ruby_reportable/sandbox.rb', line 24

def []=(key, value)
  if value.is_a?(Proc)
    @values[key] = value.call
  else
    @values[key] = value
  end
end

#build(base, block) ⇒ Object



15
16
17
18
# File 'lib/ruby_reportable/sandbox.rb', line 15

def build(base, block)
  @values[base] = instance_eval(&block)
  self
end

#define(key, value) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ruby_reportable/sandbox.rb', line 32

def define(key, value)
  if self.class.respond_to?(:define_singleton_method)
    define_singleton_method(key) do
      if value.is_a?(Proc)
        @values[key] ||= value.call
      else
        @values[key] ||= value
      end
    end
  else
    metaclass.send(:define_method, key, Proc.new do
                     if value.is_a?(Proc)
                       @values[key] ||= value.call
                     else
                       @values[key] ||= value
                     end
                   end)
  end
end

#metaclassObject



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

def metaclass
  class << self; self; end
end