Last active
August 29, 2015 13:57
-
-
Save NathanZook/9797293 to your computer and use it in GitHub Desktop.
Overcoming an identity crisis
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
class Role < BasicObject | |
# Nope, these role objects are not well-behaved objects __at_all__ | |
alias_method :role_eql?, :eql? | |
def eql?(x) | |
x.eql? @data_object | |
end | |
def hash | |
@data_object.hash | |
end | |
alias_method :role_eq, :== | |
alias_method :role_ne, :!= | |
def ==(x) | |
x == @data_object | |
end | |
alias_method :role_equal?, :equal? | |
def equal?(x) | |
x.equal? @data_object | |
end | |
alias_method :role__id__, :__id__ | |
def __id__ | |
@data_object.__id__ | |
end | |
alias_method :object_id, :__id__ | |
# <=> is not meaningful for roles in general | |
def <=>(x) | |
inverse = @data_object <=> x | |
inverse ? -inverse : inverse | |
end | |
alias_method :role_case_eq, :=== | |
def ===(x) | |
x === @data_object | |
end | |
# =~ is not meaningful for roles in general | |
def =~(x) | |
x =~ @data_object | |
end | |
klass = self | |
define_method(:role_class){ klass } | |
def class | |
@data_object.class | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment