Created
July 5, 2012 19:30
-
-
Save lexmag/3055890 to your computer and use it in GitHub Desktop.
Virtual attributes #2
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 VirtualAttributes | |
extend ActiveSupport::Concern | |
class VirtualAttribute < Struct.new(:klass); end | |
module ClassMethods | |
def attr_virtual(*attrs) | |
options = attrs.extract_options! | |
attrs.each do |attr| | |
virtual_attributes[attr] = VirtualAttribute.new(options[:klass]) | |
class_eval do | |
send("attr_accessor", attr) | |
end | |
end | |
end | |
def virtual_attributes | |
@virtual_attributes ||= {} | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment