Skip to content

Instantly share code, notes, and snippets.

@mgreenly
Created December 17, 2021 00:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgreenly/e19a06216b01afba409365176dceca59 to your computer and use it in GitHub Desktop.
Save mgreenly/e19a06216b01afba409365176dceca59 to your computer and use it in GitHub Desktop.
class Record
class MissingField < Error; end
class << self
def build(hash)
new(*members.map{|m| hash[m.to_s] })
end
end
def initialize(*values)
self.class.members.zip(values).each do |member, value|
raise MissingField, "Unable to initialize #{self.class}, missing field: '#{member}'." if value.nil?
(class << self; self end).send(:define_method, member) do
value
end
end
end
end
class CreatureRecord < Record
class << self
def members
%i[name perception]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment