Module: Erector::Needs

Included in:
Widget, XMLWidget
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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/erector/needs.rb', line 78

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, Symbol].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? || self.class.ignore_extra_assigns
    raise ArgumentError, "Excess parameter#{excess.size == 1 ? '' : 's'} for #{self.class.name}: #{excess.join(', ')}"
  end
end