Module: AIPP::Patcher

Included in:
AIP
Defined in:
lib/aipp/patcher.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object


4
5
6
7
# File 'lib/aipp/patcher.rb', line 4

def self.included(klass)
  klass.extend(ClassMethods)
  klass.class_variable_set(:@@patches, {})
end

Instance Method Details

#attach_patchesObject


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/aipp/patcher.rb', line 19

def attach_patches
  parser = self
  self.class.patches[self.class]&.each do |(klass, attribute, block)|
    klass.instance_eval do
      alias_method :"original_#{attribute}=", :"#{attribute}="
      define_method(:"#{attribute}=") do |value|
        catch :abort do
          value = block.call(parser, self, value)
          verbose_info("PATCH: #{self.inspect}", color: :magenta)
        end
        send(:"original_#{attribute}=", value)
      end
    end
  end
  self
end

#detach_patchesObject


36
37
38
39
40
41
42
43
44
# File 'lib/aipp/patcher.rb', line 36

def detach_patches
  self.class.patches[self.class]&.each do |(klass, attribute, _)|
    klass.instance_eval do
      alias_method :"#{attribute}=", :"original_#{attribute}="
      remove_method :"original_#{attribute}="
    end
  end
  self
end