Skip to content

Instantly share code, notes, and snippets.

@lexmag
Created July 5, 2012 19:33
Show Gist options
  • Save lexmag/3055931 to your computer and use it in GitHub Desktop.
Save lexmag/3055931 to your computer and use it in GitHub Desktop.
Virtual attributes #3
module VirtualAttributes
extend ActiveSupport::Concern
class VirtualAttribute
attr_reader :name, :type
def initilaize(name, type = :string)
@name = name
@type = type
end
def klass
case type
when :integer then Fixnum
when :float then Float
when :decimal then BigDecimal
when :datetime, :timestamp, :time then Time
when :date then Date
when :text, :string, :binary then String
when :boolean then Object
end
end
end
module ClassMethods
def attr_virtual(*attrs)
options = attrs.extract_options!
attrs.each do |attr|
virtual_attributes[attr] = VirtualAttribute.new(attr, options[:type])
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