Module: UseCaseValidations::ClassMethods

Defined in:
lib/usecasing_validations.rb

Instance Method Summary collapse

Instance Method Details

#_validatorsObject



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

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

#_validators=(value) ⇒ Object



114
115
116
# File 'lib/usecasing_validations.rb', line 114

def _validators=(value)
  @_validators = value
end

#clear_errors!Object



102
103
104
# File 'lib/usecasing_validations.rb', line 102

def clear_errors!
  @clear_errors = true
end

#clear_errors?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/usecasing_validations.rb', line 106

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

#inherited(base) ⇒ Object

Copy validators on inheritance.



144
145
146
147
148
# File 'lib/usecasing_validations.rb', line 144

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

#validate(*args, &block) ⇒ Object



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

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

#validates_with(*args, &block) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/usecasing_validations.rb', line 122

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



139
140
141
# File 'lib/usecasing_validations.rb', line 139

def validators
  _validators.values.flatten.uniq
end