Last active
December 19, 2015 15:49
-
-
Save jschairb/5978739 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 AttributeAccessor < Module | |
| def initialize(*names) | |
| @_names = names | |
| end | |
| def included(model) | |
| super | |
| @_names.each { |n| attr_accessor n } | |
| end | |
| end | |
| class Book | |
| ATTRIBUTES = [:author, :title] | |
| include AttributeAccessor.new(*ATTRIBUTES) | |
| def title | |
| "#{super} + super works!" | |
| end | |
| end | |
| class Book | |
| include AttributeAccessor.new(:isbn) | |
| def isbn | |
| "#{super} + whoops" | |
| end | |
| end | |
| book = Book.new | |
| book.title = "I'm not sure of the point" | |
| book.author = "jschairb" | |
| puts book.title | |
| puts book.author | |
| puts book.isbn | |
| puts book.class.ancestors |
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
| I'm not sure of the point + super works! | |
| jschairb | |
| + whoops | |
| Book | |
| #<AttributeAccessor:0x007fa3f9883930> | |
| #<AttributeAccessor:0x007fa3f9883b10> | |
| Object | |
| Kernel | |
| BasicObject |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment