Class: Reaction::EachValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/reaction/each_validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ EachValidator

Returns a new instance of EachValidator.



5
6
7
# File 'lib/reaction/each_validator.rb', line 5

def initialize(options = {})
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/reaction/each_validator.rb', line 3

def options
  @options
end

Instance Method Details

#cleanupObject

Cleanup is provided in case you need to create files that require cleanup after the action has been performed. For example, Paid creates Tempfiles with some types and uses the cleanup phase to ensure these files get closed up properly.



40
41
# File 'lib/reaction/each_validator.rb', line 40

def cleanup
end

#validate_each(record, attribute, value) ⇒ Object

If you need to validate a parameter outside of the basic type then validators are your best bet. They are generally very similar to Rails’ EachValidator. You provide a validate_each method that takes in a record, attribute, and value and then if the attribute isn’t valid adds an error to record.errors. You can also access the data provided for the validator via the options method.

Example validate_each method for a RequiredValidator which would be used like:

param :cat, required: true
param :dog, required: false

def validate_each(record, attribute, value)
  if options && value.nil?
    record.errors.add(attribute, 'is required.')
  end
end

Raises:

  • (NotImplementedError)


30
31
32
# File 'lib/reaction/each_validator.rb', line 30

def validate_each(record, attribute, value)
  raise NotImplementedError.new('Subclasses must implement a validate_each(record, attribute, value) method')
end