Module: UseCaseValidations::ClassMethods

Defined in:
lib/usecasing_validations.rb

Instance Method Summary collapse

Instance Method Details

#_validatorsObject



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

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

#_validators=(value) ⇒ Object



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

def _validators=(value)
  @_validators = value
end

#clear_errors!Object



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

def clear_errors!
  @clear_errors = true
end

#clear_errors?Boolean

Returns:

  • (Boolean)


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

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

#inherited(base) ⇒ Object

Copy validators on inheritance.



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

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

#validate(*args, &block) ⇒ Object



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

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

#validates_with(*args, &block) ⇒ Object



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

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



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

def validators
  _validators.values.flatten.uniq
end