class Saviour::Validator::AttachmentValidator
Public Class Methods
new(model, column, validations)
click to toggle source
# File lib/saviour/validator.rb, line 5 def initialize(model, column, validations) @model = model @column = column @validations = validations @file = model.send(column) end
Public Instance Methods
validate!()
click to toggle source
# File lib/saviour/validator.rb, line 12 def validate! @validations.each do |data| type = data[:type] method_or_block = data[:method_or_block] case type when :memory run_validation(method_or_block) when :file run_validation_as_file(method_or_block) end end ensure @source_as_file.close! if @source_as_file end
Private Instance Methods
filename()
click to toggle source
# File lib/saviour/validator.rb, line 40 def filename @filename ||= @file.filename_to_be_assigned end
run_validation(method_or_block)
click to toggle source
# File lib/saviour/validator.rb, line 63 def run_validation(method_or_block) opts = { attached_as: @column } if method_or_block.respond_to?(:call) if method_or_block.arity == 2 @model.instance_exec(source_as_memory, filename, &method_or_block) else @model.instance_exec(source_as_memory, filename, opts, &method_or_block) end else if @model.method(method_or_block).arity == 2 @model.send(method_or_block, source_as_memory, filename) else @model.send(method_or_block, source_as_memory, filename, opts) end end end
run_validation_as_file(method_or_block)
click to toggle source
# File lib/saviour/validator.rb, line 81 def run_validation_as_file(method_or_block) opts = { attached_as: @column } if method_or_block.respond_to?(:call) if method_or_block.arity == 2 @model.instance_exec(source_as_file, filename, &method_or_block) else @model.instance_exec(source_as_file, filename, opts, &method_or_block) end else if @model.method(method_or_block).arity == 2 @model.send(method_or_block, source_as_file, filename) else @model.send(method_or_block, source_as_file, filename, opts) end end end
source_as_file()
click to toggle source
# File lib/saviour/validator.rb, line 48 def source_as_file @source_as_file ||= begin f = Tempfile.new("") f.binmode if source_type == :file FileUtils.cp(@file.source.path, f.path) else ::File.binwrite(f.path, source_as_memory) end f end end
source_as_memory()
click to toggle source
# File lib/saviour/validator.rb, line 44 def source_as_memory @source_as_memory ||= @file.source_data end
source_type()
click to toggle source
# File lib/saviour/validator.rb, line 32 def source_type if @file.source.respond_to?(:path) :file else :stream end end