Created
April 19, 2013 16:54
-
-
Save reddyonrails/5421632 to your computer and use it in GitHub Desktop.
Class Attribute Mixin to list out attributes and its values as hash when included in Class
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
module ClassAttributeMixin | |
def self.included(base) | |
class << base | |
def attr_accessor(*splash) | |
@attributes ||= [] | |
@attributes.concat splash | |
super | |
end | |
def attributes | |
@attributes | |
end | |
end | |
end | |
def attributes | |
self.class.attributes | |
end | |
def to_hash | |
hash = {}; attributes.each { |k| hash[k] = self.send k } | |
return hash | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment