-
-
Save anonymous/f22b65539c958f5173d33d769d7d9fec to your computer and use it in GitHub Desktop.
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 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