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

+ (Object) for(setting)



464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# File 'lib/optout.rb', line 464

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