module Class2::StrictConstructor

By default unknown arguments are ignored. <code>include<code>ing this will cause an ArgumentError to be raised if an attribute is unknown.

Public Class Methods

included(klass) click to toggle source
# File lib/class2.rb, line 276
def self.included(klass)
  klass.class_eval do
    def initialize(attributes = nil)
      return unless __initialize(attributes)
      attributes.each do |name, _|
        next if self.class.__attributes.include?(name.respond_to?(:to_sym) ? name.to_sym : name)
        raise ArgumentError, "unknown attribute: #{name}"
      end
    end
  end
end
new(attributes = nil) click to toggle source
# File lib/class2.rb, line 278
def initialize(attributes = nil)
  return unless __initialize(attributes)
  attributes.each do |name, _|
    next if self.class.__attributes.include?(name.respond_to?(:to_sym) ? name.to_sym : name)
    raise ArgumentError, "unknown attribute: #{name}"
  end
end