Skip to content

Instantly share code, notes, and snippets.

@dannysmith
Created November 4, 2018 22:56
Show Gist options
  • Save dannysmith/d9841020b4d1a9ea55c8a39adc5d7ad7 to your computer and use it in GitHub Desktop.
Save dannysmith/d9841020b4d1a9ea55c8a39adc5d7ad7 to your computer and use it in GitHub Desktop.
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