Module: DataMapper::Validations::ValidatesWithBlock
- Included in:
- ClassMethods
- Defined in:
- lib/dm-validations/validators/block_validator.rb
Overview
Instance Method Summary collapse
-
#validates_with_block(*fields, &block) ⇒ Object
Validate using the given block.
Instance Method Details
#validates_with_block(*fields, &block) ⇒ Object
Validate using the given block. The block given needs to return:
- result::<Boolean>, Error Message::<String>
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/dm-validations/validators/block_validator.rb', line 41 def validates_with_block(*fields, &block) @__validates_with_block_count ||= 0 @__validates_with_block_count += 1 # create method and pass it to MethodValidator unless block_given? raise ArgumentError, 'You need to pass a block to validates_with_block method' end method_name = "__validates_with_block_#{@__validates_with_block_count}".to_sym define_method(method_name, &block) = fields.last.is_a?(Hash) ? fields.last.pop.dup : {} [:method] = method_name fields = [method_name] if fields.empty? validators.add(MethodValidator, *fields + []) end |