Module: Shrine::Plugins::Validation::AttacherMethods

Defined in:
lib/shrine/plugins/validation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#errorsObject (readonly)

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


24
25
26
# File 'lib/shrine/plugins/validation.rb', line 24

def errors
  @errors
end

Instance Method Details

#attach(io, validate: nil, **options) ⇒ Object

Performs validations after attaching file.


40
41
42
43
44
# 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) ⇒ Object

Performs validations after attaching cached file.


33
34
35
36
37
# 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

#initialize(**options) ⇒ Object

Initializes validation errors to an empty array.


27
28
29
30
# File 'lib/shrine/plugins/validation.rb', line 27

def initialize(**options)
  super
  @errors = []
end

#validate(**options) ⇒ Object

Runs the validation defined by `Attacher.validate`.


47
48
49
50
# File 'lib/shrine/plugins/validation.rb', line 47

def validate(**options)
  errors.clear
  _validate(**options) if attached?
end