Skip to content

Instantly share code, notes, and snippets.

@verdi327
Created August 1, 2012 01:46
Show Gist options
  • Save verdi327/3222591 to your computer and use it in GitHub Desktop.
Save verdi327/3222591 to your computer and use it in GitHub Desktop.
Class Extension Mixin
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