Module: UseCaseValidations::ClassMethods

Defined in:
lib/usecasing_validations.rb

Instance Method Summary collapse

Instance Method Details

#_validatorsObject



81
82
83
# File 'lib/usecasing_validations.rb', line 81

def _validators
  @_validators ||= Hash.new { |h,k| h[k] = [] }
end

#_validators=(value) ⇒ Object



85
86
87
# File 'lib/usecasing_validations.rb', line 85

def _validators=(value)
  @_validators = value
end

#clear_errors!Object



73
74
75
# File 'lib/usecasing_validations.rb', line 73

def clear_errors!
  @clear_errors = true
end

#clear_errors?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/usecasing_validations.rb', line 77

def clear_errors?
  defined?(@clear_errors) ? @clear_errors : false
end

#inherited(base) ⇒ Object

Copy validators on inheritance.



115
116
117
118
119
# File 'lib/usecasing_validations.rb', line 115

def inherited(base)
  dup = _validators.dup
  base._validators = dup.each { |k, v| dup[k] = v.dup }
  super
end

#validate(*args, &block) ⇒ Object



89
90
91
# File 'lib/usecasing_validations.rb', line 89

def validate(*args, &block)
  _validators[nil] << CustomValidator.new(args, &block)
end

#validates_with(*args, &block) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/usecasing_validations.rb', line 93

def validates_with(*args, &block)
  options = Helpers._extract_options!(args)
  options[:class] = self

  args.each do |klass|
    validator = klass.new(options, &block)

    if validator.respond_to?(:attributes) && !validator.attributes.empty?
      validator.attributes.each do |attribute|
        _validators[attribute.to_sym] << validator
      end
    else
      _validators[nil] << validator
    end
  end
end

#validatorsObject



110
111
112
# File 'lib/usecasing_validations.rb', line 110

def validators
  _validators.values.flatten.uniq
end