module Shrine::Plugins::Validation::AttacherMethods

Attributes

errors[R]

Returns an array of validation errors created on file assignment in the ‘Attacher.validate` block.

Public Class Methods

new(**options) click to toggle source

Initializes validation errors to an empty array.

Calls superclass method
# File lib/shrine/plugins/validation.rb, line 27
def initialize(**options)
  super
  @errors = []
end

Public Instance Methods

attach(io, validate: nil, **options) click to toggle source

Performs validations after attaching file.

Calls superclass method
# File lib/shrine/plugins/validation.rb, line 40
def attach(io, validate: nil, **options)
  result = super(io, **options)
  validation(validate)
  result
end
attach_cached(value, validate: nil, **options) click to toggle source

Performs validations after attaching cached file.

Calls superclass method
# File lib/shrine/plugins/validation.rb, line 33
def attach_cached(value, validate: nil, **options)
  result = super(value, validate: false, **options)
  validation(validate)
  result
end
validate(**options) click to toggle source

Runs the validation defined by ‘Attacher.validate`.

# File lib/shrine/plugins/validation.rb, line 47
def validate(**options)
  errors.clear
  _validate(**options) if attached?
end

Private Instance Methods

_validate(**options) click to toggle source

Calls validate_block, passing it accepted parameters.

# File lib/shrine/plugins/validation.rb, line 64
def _validate(**options)
  if method(:validate_block).arity.zero?
    validate_block
  else
    validate_block(**options)
  end
end
validate_block(**options) click to toggle source

Overridden by the ‘Attacher.validate` block.

# File lib/shrine/plugins/validation.rb, line 73
def validate_block(**options)
end
validation(argument) click to toggle source

Calls validation appropriately based on the :validate value.

# File lib/shrine/plugins/validation.rb, line 55
def validation(argument)
  case argument
  when Hash  then validate(**argument)
  when false then errors.clear # skip validation
  else            validate
  end
end