Module: PoolParty::Callbacks::ClassMethods

Defined in:
lib/modules/callback.rb

Instance Method Summary collapse

Instance Method Details

#after(m, *args, &block) ⇒ Object



51
52
53
# File 'lib/modules/callback.rb', line 51

def after(m, *args, &block)
  callback(:after, m, *args, &block)
end

#before(m, *args, &block) ⇒ Object



48
49
50
# File 'lib/modules/callback.rb', line 48

def before(m, *args, &block)
  callback(:before, m, *args, &block)
end

#callback(type, m, *args, &block) ⇒ Object



13
14
15
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
# File 'lib/modules/callback.rb', line 13

def callback(type, m, *args, &block)
  arr = []                
  args.each do |arg|
    arr << case arg.class.to_s
    when "Hash"
      arg.collect do |meth, klass|
        case klass.class.to_s
        when "String"
          define_callback_class(klass)
          "self.#{klass.to_s.downcase}.#{meth}(self)"
        else
          "#{klass}.send :#{meth}, self"
        end              
      end
    when "Symbol"
      "self.send :#{arg}, self"
    end
  end

  string = ""
  if block_given?
    num = store_proc(block.to_proc)
    arr << <<-EOM
      self.class.get_proc(#{num}).bind(self).call
    EOM
  end

  string = create_eval_for_mod_with_string_and_type!(m, type) do
    arr.join("\n")
  end        

  mMode = Module.new {eval string}

  define_callback_module(mMode)
end

#callbacksObject



82
83
84
# File 'lib/modules/callback.rb', line 82

def callbacks
  @callbacks ||= []
end

#classesObject



85
86
87
# File 'lib/modules/callback.rb', line 85

def classes
  @classes ||= []
end

#create_eval_for_mod_with_string_and_type!(meth, type = nil, &block) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/modules/callback.rb', line 55

def create_eval_for_mod_with_string_and_type!(meth, type=nil, &block)
  str = ""
  case type
  when :before          
    str << <<-EOD
      def #{meth}(*args)
        #{yield}
        super
      end
    EOD
  when :after
    str << <<-EOD
      def #{meth}(*args)
        super
        #{yield}
      end
    EOD
  else
    str << <<-EOD
      def #{meth}(*args)
        #{yield}
      end
    EOD
  end
  str
end

#define_callback_class(cla) ⇒ Object



10
11
12
# File 'lib/modules/callback.rb', line 10

def define_callback_class(cla)
  classes << cla
end

#define_callback_module(mod) ⇒ Object



7
8
9
# File 'lib/modules/callback.rb', line 7

def define_callback_module(mod)
  callbacks << mod
end