Module: Primer::Watcher::Macros

Defined in:
lib/primer/watcher/macros.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.alias_name(method_name) ⇒ Object



11
12
13
# File 'lib/primer/watcher/macros.rb', line 11

def self.alias_name(method_name)
  method_name.to_s.gsub(/[^a-z0-9_]$/i, '') + '_before_primer_patch'
end

.extended(klass) ⇒ Object



5
6
7
8
9
# File 'lib/primer/watcher/macros.rb', line 5

def self.extended(klass)
  if defined?(ActiveRecord) and klass < ActiveRecord::Base
    klass.extend(ActiveRecordMacros)
  end
end

Instance Method Details

#patch_for_primer!Object



26
27
28
29
30
31
32
# File 'lib/primer/watcher/macros.rb', line 26

def patch_for_primer!
  return if @primer_patched
  @primer_patched = true
  primer_watched_calls.each do |method_name|
    patch_method_for_primer(method_name)
  end
end

#patch_method_for_primer(method_name) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/primer/watcher/macros.rb', line 34

def patch_method_for_primer(method_name)
  alias_name = Macros.alias_name(method_name)
  return unless method = instance_method(method_name) rescue nil
  class_eval <<-RUBY
    alias :#{alias_name} :#{method_name}
    def #{method_name}(*args, &block)
      result = #{alias_name}(*args, &block)
      Primer::Watcher.log(self, :#{method_name}, args, block, result)
      result
    end
  RUBY
end

#primer_watched_callsObject



15
16
17
18
19
# File 'lib/primer/watcher/macros.rb', line 15

def primer_watched_calls
  @primer_watched_calls ||= (Macros === superclass) ?
                            superclass.primer_watched_calls.dup :
                            Set.new
end

#unpatch_for_primer!Object



47
48
49
50
51
52
53
# File 'lib/primer/watcher/macros.rb', line 47

def unpatch_for_primer!
  return unless @primer_patched
  @primer_patched = false
  primer_watched_calls.each do |method_name|
    unpatch_method_for_primer(method_name)
  end
end

#unpatch_method_for_primer(method_name) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/primer/watcher/macros.rb', line 55

def unpatch_method_for_primer(method_name)
  alias_name = Macros.alias_name(method_name)
  return unless method = instance_method(alias_name) rescue nil
  class_eval <<-RUBY
    alias :#{method_name} :#{alias_name}
    undef_method :#{alias_name}
  RUBY
end

#watch_calls_to(*methods) ⇒ Object



21
22
23
24
# File 'lib/primer/watcher/macros.rb', line 21

def watch_calls_to(*methods)
  method_names = methods.map { |m| m.to_s }
  primer_watched_calls.merge(method_names)
end