Class: Saviour::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/saviour/validator.rb

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Validator

Returns a new instance of Validator.

Raises:



3
4
5
6
7
# File 'lib/saviour/validator.rb', line 3

def initialize(model)
  raise(ConfigurationError, "Please provide an object compatible with Saviour.") unless model.class.respond_to?(:attached_files)

  @model = model
end

Instance Method Details

#validate!Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/saviour/validator.rb', line 9

def validate!
  validations.each do |column, declared_validations|
    raise(ConfigurationError, "There is no attachment defined as '#{column}'") unless attached_files.include?(column)
    if @model.send(column).changed?
      declared_validations.each do |data|
        type = data[:type]
        method_or_block = data[:method_or_block]

        case type
          when :memory
            run_validation(column, method_or_block)
          when :file
            run_validation_as_file(column, method_or_block)
        end
      end
    end
  end
end