Last active
April 25, 2016 05:10
-
-
Save henrik/24147d0ee263063bef341797e993173e to your computer and use it in GitHub Desktop.
Sharing attributes in Ruby with a single `prepend` instead of e.g. `include` + `merge`. Proof of concept. Too implicit for my taste, I think.
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 SharedAttributes | |
def attributes | |
super.merge(shared: true) | |
end | |
end | |
class FooAttributes | |
prepend SharedAttributes | |
def attributes | |
{ foo: true } | |
end | |
def speak | |
p attributes | |
end | |
end | |
class BarAttributes | |
prepend SharedAttributes | |
def attributes | |
{ bar: true } | |
end | |
def speak | |
p attributes | |
end | |
end | |
FooAttributes.new.speak # { foo: true, shared: true } | |
BarAttributes.new.speak # { bar: true, shared: true } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment