Sha256: 83904f97073764e9a0317bfc97d37e91f205a1c2c13ccd8007e5ff94a6a20ca0

Contents?: true

Size: 1.09 KB

Versions: 14

Compression:

Stored size: 1.09 KB

Contents

module RubyReportable
  class Sandbox
    def metaclass
      class << self; self; end
    end

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

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

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

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

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

    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
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
ruby_reportable-0.5.1 lib/ruby_reportable/sandbox.rb
ruby_reportable-0.5.0 lib/ruby_reportable/sandbox.rb
ruby_reportable-0.4.3 lib/ruby_reportable/sandbox.rb
ruby_reportable-0.4.2 lib/ruby_reportable/sandbox.rb
ruby_reportable-0.4.1 lib/ruby_reportable/sandbox.rb
ruby_reportable-0.4.0 lib/ruby_reportable/sandbox.rb
ruby_reportable-0.3.1 lib/ruby_reportable/sandbox.rb
ruby_reportable-0.3.0 lib/ruby_reportable/sandbox.rb
ruby_reportable-0.2.0 lib/ruby_reportable/sandbox.rb
ruby_reportable-0.1.3.1 lib/ruby_reportable/sandbox.rb
ruby_reportable-0.1.3 lib/ruby_reportable/sandbox.rb
ruby_reportable-0.1.2 lib/ruby_reportable/sandbox.rb
ruby_reportable-0.1.1 lib/ruby_reportable/sandbox.rb
ruby_reportable-0.1.0 lib/ruby_reportable/sandbox.rb