Created
August 1, 2012 01:46
-
-
Save verdi327/3222591 to your computer and use it in GitHub Desktop.
Class Extension Mixin
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 CheckedAttributes | |
def self.included(base) | |
base.extend ClassMethods | |
end | |
module ClassMethods | |
def attr_checked(attribute, &validation) | |
define_method "#{attribute}=" do |value| | |
raise 'Invalid attribute' unless validation.call(value) | |
instance_variable_set("@#{attribute}", value) | |
end | |
define_method attribute do | |
instance_variable_get "@#{attribute}" | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment