module Erector::Needs

Public Class Methods

included(base) click to toggle source
# File lib/erector/needs.rb, line 3
def self.included(base)
  base.extend ClassMethods
end
new(assigns = {}) click to toggle source
# File lib/erector/needs.rb, line 70
def initialize(assigns = {})
  super

  assigned = assigns.keys

  # set variables with default values
  self.class.needed_defaults.each do |name, value|
    unless assigned.include?(name)
      value = [NilClass, FalseClass, TrueClass, Fixnum, Float].include?(value.class) ? value : value.dup
      instance_variable_set("@#{name}", value)
      assigned << name
    end
  end

  missing = self.class.needed_variables - assigned
  unless missing.empty? || missing == [nil]
    raise ArgumentError, "Missing parameter#{missing.size == 1 ? '' : 's'} for #{self.class.name}: #{missing.join(', ')}"
  end

  excess = assigned - self.class.needed_variables
  unless self.class.needed_variables.empty? || excess.empty?
    raise ArgumentError, "Excess parameter#{excess.size == 1 ? '' : 's'} for #{self.class.name}: #{excess.join(', ')}"
  end
end