Created
November 4, 2018 22:56
-
-
Save dannysmith/d9841020b4d1a9ea55c8a39adc5d7ad7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class ValueObject | |
NotImplementedInSubclassError = Class.new(StandardError) | |
def initialize | |
raise NotImplementedInSubclassError, 'Value Objects must implement a constructor' | |
end | |
# This is not ideal, since it will fail if the object contains any instance variables that are not exposed via getter methods. | |
def ==(other) | |
return false unless self.class == other.class | |
self.instance_variables.map do |iv| | |
method_name = iv.to_s[1..-1].to_sym | |
return false if self.send(method_name) != other.send(method_name) | |
end | |
true | |
end | |
alias :eql? :== | |
def hash | |
self.instance_variables.map {|iv| self.instance_variable_get(iv)}.hash | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment