Module: Optout::Validator

Defined in:
lib/optout.rb

Overview

:nodoc: all

Defined Under Namespace

Classes: Array, Base, Boolean, Class, Dir, File, Multiple, Regexp, Required

Class Method Summary collapse

Class Method Details

.for(setting) ⇒ Object



492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
# File 'lib/optout.rb', line 492

def self.for(setting)
  if setting.respond_to?(:validate!)
    setting
  else
    # Load validator based on the setting's name or the name of its class
    # Note that on 1.9 calling class.name on anonymous classes (i.e., Class.new.new) returns nil
    validator = setting.class.name.to_s  
    if validator == "Class"
      name = setting.name.to_s.split("::", 2)
      validator = name[1] if name[1] && name[0] == "Optout"
    end

    # Support 1.8 and 1.9, avoid String/Symbol and const_defined? differences
    if validator.empty? || !constants.include?(validator) && !constants.include?(validator.to_sym)
      raise ArgumentError, "don't know how to validate with #{setting}"
    end

    const_get(validator).new(setting)
  end
end