-
-
Save havenwood/0c2a3f70f68151f7ab022e2430487432 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
class DoNotDump < Module | |
class << self | |
alias [] new | |
end | |
attr_reader :ignored_ivars | |
def initialize *ignored_ivars | |
@ignored_ivars = ignored_ivars.freeze | |
super() do | |
define_method :marshal_load do |pairs| | |
pairs.each { |ivar, value| instance_variable_set ivar, value } | |
end | |
define_method :marshal_dump do | |
instance_variables.each_with_object [] do |ivar, pairs| | |
pairs << [ivar, instance_variable_get(ivar)] unless ignored_ivars.include? ivar | |
end | |
end | |
end | |
end | |
alias to_a ignored_ivars | |
def == other | |
other.instance_of?(self.class) && other.ignored_ivars.sort == @ignored_ivars.sort | |
end | |
def inspect | |
"#{self.class.name}#{@ignored_ivars.inspect}" | |
end | |
alias to_s inspect | |
def marshal_dump | |
@ignored_ivars | |
end | |
def marshal_load data | |
@ignored_ivars = data | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment