Skip to content

Instantly share code, notes, and snippets.

@tubbo
Last active December 22, 2015 11:39
Show Gist options
  • Save tubbo/6467218 to your computer and use it in GitHub Desktop.
Save tubbo/6467218 to your computer and use it in GitHub Desktop.
A means of enforcing attributes on a given class.
module Requirable
def self.included(base)
required_attributes.each do |key,val|
raise NoMethodError, "'#{key}' is not defined on #{self.inspect}"
end
end
cattr_accessor :required_attributes
def self.attr_required(*attributes)
self.required_attributes = attributes
end
end
class Model
include Requirable
attr_required :name, :description
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment