Skip to content

Instantly share code, notes, and snippets.

/.rb Secret

Created August 21, 2017 22:01
Show Gist options
  • Save anonymous/f22b65539c958f5173d33d769d7d9fec to your computer and use it in GitHub Desktop.
Save anonymous/f22b65539c958f5173d33d769d7d9fec to your computer and use it in GitHub Desktop.
module Model
module ClassMethods
TYPE_MAP = {}
def attribute(attribute_name, type_restriction)
TYPE_MAP[self] ||= {attribute_name => type_restriction}
end
end
def self.included(mod)
mod.extend ClassMethods
end
def initialize(attributes)
@attributes = attributes
@attributes.each do |k,v|
ClassMethods::TYPE_MAP[self.class][k] == v.class or raise(TypeError, 'blah')
instance_variable_set :"@#{k}", v
end
end
end
class SomeThing
include Model
attribute :foo, String
def start
puts "@foo is a string"
end
end
SomeThing.new(foo: "hi").start # ok
SomeThing.new(foo: 1).start # error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment