module MixedNumberRails::MixedNumberAttribute::ClassMethods

Public Instance Methods

mixed_number_attribute(attribute) click to toggle source
# File lib/mixed_number_rails/mixed_number_attribute.rb, line 6
def mixed_number_attribute(attribute)
        serialize attribute, MixedNumber

        validate -> {
                if read_attribute(attribute).is_a?(NullMixedNumber)
                        errors.add(attribute, "is not a valid mixed number")
                end
        }

        define_method(attribute.to_s + '=') do |value|
                if value == nil
                        write_attribute(attribute, value)
                else
                        write_attribute(attribute, MixedNumber(value))
                end
        end
end