Module: Erector::Needs

Included in:
Widget
Defined in:
lib/erector/needs.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
# File 'lib/erector/needs.rb', line 3

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#initialize(assigns = {}) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# 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)
      instance_variable_set("@#{name}", value)
      assigned << name
    end
  end

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

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