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


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# 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