Module: MyJohnDeereApi::Validators::Base

Included in:
Request::Create::Base, Request::Update::Base, Asset, AssetLocation
Defined in:
lib/my_john_deere_api/validators/base.rb

Overview

This is a mix-in for Create/Update Reqest classes. It assumes that the class in question has a hash of attributes that will be passed to the request.

This module creates the errors hash as a reader. The key of the hash is the attribute name, and the value is an array of error messages for that attribute. Follow this format when defining custom validations in the ‘validate_attributes` method.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



13
14
15
# File 'lib/my_john_deere_api/validators/base.rb', line 13

def errors
  @errors
end

Instance Method Details

#valid?Boolean

Runs validations, adding to the errors hash as needed. Returns true if the errors hash is still empty after all validations have been run.

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
# File 'lib/my_john_deere_api/validators/base.rb', line 28

def valid?
  return @is_valid if defined?(@is_valid)

  @errors = {}
  validate_required
  validate_attributes

  @is_valid = errors.empty?
end

#validate!Object

Raises an error if the record is invalid. Passes the errors hash to the error, in order to build a useful message string.

Raises:



19
20
21
22
# File 'lib/my_john_deere_api/validators/base.rb', line 19

def validate!
  raise(InvalidRecordError, errors) unless valid?
  true
end