Class: ValidationSets::ValidationSet

Inherits:
Object
  • Object
show all
Defined in:
lib/validation_sets/validation_set.rb

Overview

A ValidationSet instance is used to redirect the original validation methods (validate, validate_on_create, and validate_on_update) to the callback chain for the set.

Instance Method Summary collapse

Constructor Details

#initialize(model, set) ⇒ ValidationSet

Returns a new instance of ValidationSet.



5
6
7
8
# File 'lib/validation_sets/validation_set.rb', line 5

def initialize(model, set)
  @model = model
  @set = set
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *attributes, &block) ⇒ Object

Forwards all other methods (ie. validates_presence_of) to the model class.



26
27
28
# File 'lib/validation_sets/validation_set.rb', line 26

def method_missing(method, *attributes, &block)
  @model.send(method, *attributes, &block)
end

Instance Method Details

#validate(*params, &block) ⇒ Object

Adds a validation method or proc that always runs



11
12
13
# File 'lib/validation_sets/validation_set.rb', line 11

def validate(*params, &block)
  send(validation_set_method(:save, @set), *params, &block)
end

#validate_on_create(*params, &block) ⇒ Object

Adds a validation method or proc that runs on create



16
17
18
# File 'lib/validation_sets/validation_set.rb', line 16

def validate_on_create(*params, &block)
  send(validation_set_method(:create, @set), *params, &block)
end

#validate_on_update(*params, &block) ⇒ Object

Adds a validation method or proc that runs on update



21
22
23
# File 'lib/validation_sets/validation_set.rb', line 21

def validate_on_update(*params, &block)
  send(validation_set_method(:update, @set), *params, &block)
end