Created
December 30, 2015 00:20
-
-
Save romaimperator/9860eed69ea98fab35ea to your computer and use it in GitHub Desktop.
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 VirtualDate | |
extend ActiveSupport::Concern | |
class_methods do | |
def virtual_date(name) | |
attr_reader name | |
# Incoming forms pass dates as a hash like {1 => 2014, 2 => 4, 3 => 30} | |
# This converts that to a date class instance | |
define_method("#{name}=") do |new_value| | |
if new_value.is_a?(Hash) | |
new_value = Date.new(*(new_value.sort.map(&:last))) | |
end | |
self.instance_variable_set("@#{name}", new_value) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment