Created
July 5, 2012 19:33
-
-
Save lexmag/3055931 to your computer and use it in GitHub Desktop.
Virtual attributes #3
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 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