Skip to content

Instantly share code, notes, and snippets.

@reddyonrails
Created April 19, 2013 16:54
Show Gist options
  • Save reddyonrails/5421632 to your computer and use it in GitHub Desktop.
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
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