Module: Feature::BasicInitialize::InstanceMethods

Defined in:
lib/feature/basic_initialize.rb

Instance Method Summary collapse

Instance Method Details

#initialize(attrs = {}) ⇒ Object

See source for details.


28
29
30
31
32
33
34
35
36
# File 'lib/feature/basic_initialize.rb', line 28

def initialize(attrs = {})
  # Validate `attrs` with a meaningful exception. Otherwise confusion is possible at `MyKlass.new(obj)`.
  [attrs, Hash].tap {|v, klass| v.is_a?(klass) or raise ArgumentError, "#{klass} expected, #{v.class} given: #{v.inspect}"}

  # Handle defaults.
  defaults = self.class.respond_to?(:defaults) ? self.class.defaults : {}

  defaults.merge(attrs).each {|k, v| public_send("#{k}=", v)}
end