Method: Eye::Dsl::Validation::ClassMethods#validate

Defined in:
lib/eye/dsl/validation.rb

#validate(options = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/eye/dsl/validation.rb', line 53

def validate(options = {})
  options.each do |param, value|
    param = param.to_sym
    types = validates[param]
    unless types
      if param != :type
        raise Error, "#{self.name} unknown param :#{param} value #{value.inspect}"
      end
    end

    if self.variants[param]
      if value && !value.is_a?(Proc)
        if value.is_a?(Array)
          if (value - self.variants[param]).present?
            raise Error, "#{value.inspect} should be within #{self.variants[param].inspect}"
          end
        elsif !self.variants[param].include?(value)
          raise Error, "#{value.inspect} should be within #{self.variants[param].inspect}"
        end
      end
    end

    next if types.blank?

    types = Array(types)
    good = types.any?{|type| value.is_a?(type) }
    raise Error, "#{self.name} bad param :#{param} value #{value.inspect}, type #{types.inspect}" unless good
  end

  should_bes.each do |param|
    raise Error, "#{self.name} for param :#{param} value should be" unless options[param.to_sym] || defaults[param.to_sym]
  end
end