Skip to content

Instantly share code, notes, and snippets.

@lexmag
Created July 5, 2012 19:30
Show Gist options
  • Save lexmag/3055890 to your computer and use it in GitHub Desktop.
Save lexmag/3055890 to your computer and use it in GitHub Desktop.
Virtual attributes #2
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