Created
August 26, 2022 16:39
-
-
Save nsmmrs/70579852a5eee8d0a095fa0fffcc32b5 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
module ValueObject | |
def ==(other) | |
case other | |
when ValueObject | |
self.object_value == other.object_value | |
else | |
false | |
end | |
end | |
def eql?(other) | |
( self.class == other.class ) && ( self == other ) | |
end | |
def hash | |
object_value.hash | |
end | |
def to_hash | |
object_presentation.deep_stringify_keys | |
end | |
def object_presentation | |
to_json_object(presentation_properties) | |
end | |
def presentation_properties | |
value_properties | |
end | |
def object_value | |
to_json_object(value_properties) | |
end | |
def value_properties | |
self.instance_values.keys.sort | |
end | |
private | |
def to_json_object(property_names) | |
property_names.map(&:to_sym).map do |property_name| | |
[ property_name, self.send(property_name) ] | |
end.to_h | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment