Sha256: 44a6632f522f5f1ead6ffd1a81e72aa7e823df9456d7677f7be6c6ed86d1c0b8

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

module AIPP
  module Patcher

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

    module ClassMethods
      def patches
        class_variable_get(:@@patches)
      end

      def patch(klass, attribute, &block)
        (patches[self] ||= []) << [klass, attribute, block]
      end
    end

    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)
              debug("PATCH: #{self.inspect}", color: :magenta)
            end
            send(:"original_#{attribute}=", value)
          end
        end
      end
      self
    end

    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

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
aipp-0.2.2 lib/aipp/patcher.rb