Module: Class2::StrictConstructor

Defined in:
lib/class2.rb

Overview

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

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



248
249
250
251
252
253
254
255
256
257
258
# File 'lib/class2.rb', line 248

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