Module: SeeingIsBelieving::Safe

Defined in:
lib/seeing_is_believing/safe.rb

Constant Summary collapse

Queue =
build ::Queue, :<<, :shift, :clear
Stream =
build ::IO, :sync=, :<<, :flush, :close
Symbol =
build ::Symbol, :==, class: [:define_method]
String =
build ::String, :to_s
Fixnum =
build ::Fixnum, :to_s
Array =
build ::Array, :pack, :map, :size, :join
Hash =
build ::Hash, :[], :[]=, class: [:new]
Marshal =
build ::Marshal, class: [:dump]
Exception =
build ::Exception, :message, :backtrace, :class, class: [:define_method]
Thread =
build ::Thread, :join, class: [:current]

Class Method Summary collapse

Class Method Details

.build(klass, *method_names) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/seeing_is_believing/safe.rb', line 3

def self.build(klass, *method_names)
  options = {}
  options = method_names.pop if method_names.last.kind_of? ::Hash

  Class.new do
    class << self
      alias [] new
    end

    define_method :initialize do |instance|
      @_instance = instance
    end

    methods = method_names.map { |name| [name, klass.instance_method(name)] }
    methods.each do |name, method|
      define_method(name) do |*args, &block|
        method.bind(@_instance).call(*args, &block)
      end
    end

    singleton_methods = options.fetch(:class, []).map { |name| [name, klass.method(name)] }
    singleton_methods.each do |name, method|
      define_singleton_method name do |*args, &block|
        method.call(*args, &block)
      end
    end
  end
end