module Dry::Core::ClassAttributes
def defines(*args, type: Object)
- Example: with dry-types -
Example: with inheritance and type checking -
def defines(*args, type: Object) mod = Module.new do args.each do |name| define_method(name) do |value = Undefined| ivar = "@#{name}" if value == Undefined if instance_variable_defined?(ivar) instance_variable_get(ivar) else nil end else raise InvalidClassAttributeValue.new(name, value) unless type === value instance_variable_set(ivar, value) end end end define_method(:inherited) do |klass| args.each { |name| klass.public_send(name, send(name)) } super(klass) end end extend(mod) end