Module: Pebbles::SuddenlyDeath

Defined in:
lib/pebbles/suddenly_death.rb,
lib/pebbles/suddenly_death/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/pebbles/suddenly_death.rb', line 16

def self.included(base)
  base.class_eval do 
    defined_instance_methods =
      base.public_instance_methods(true) - Object.public_instance_methods(true)

    defined_instance_methods.each do |method|
      method_with_death = (method.to_s + "_with_death").to_sym
      method_without_death = (method.to_s + "_without_death").to_sym

      define_method(method_with_death) do |*args|
        __send__ method_without_death, *args
        raise Pebbles::SuddenlyDeathError if rand(100) == 0
      end

      alias_method method_without_death, method
      alias_method method, method_with_death
    end

    singleton_class = (class << base; self end)
    defined_class_methods =
      self.public_methods(true) - Object.public_methods(true)
    
    defined_class_methods.each do |method|
      method_with_death = (method.to_s + "_with_death").to_sym
      method_without_death = (method.to_s + "_without_death").to_sym
      singleton_class.class_eval do
        define_method(method_with_death) do |*args|
          __send__ method_without_death, *args
          raise Pebbles::SuddenlyDeathError if rand(100) == 0
        end

        alias_method method_without_death, method
        alias_method method, method_with_death
      end
    end
  end
end