Class: WhyValidationsSuckIn96::ValidationBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/whyvalidationssuckin96/validation_builder.rb

Overview

ValidationBuilder

Class Method Summary

Instance Method Summary

Constructor Details

- (ValidationBuilder) initialize(klass_or_mod, definition_block)

A new instance of ValidationBuilder

Parameters:

  • (Module, Class) klass_or_mod — The Class or Module to add validations to
  • (Proc) definition_block — The block to evaluate to define validations


8
9
10
11
# File 'lib/whyvalidationssuckin96/validation_builder.rb', line 8

def initialize(klass_or_mod, definition_block)
  @klass_or_mod = klass_or_mod
  @definition_block = definition_block
end

Class Method Details

+ (Object) register_macro(macro_name, validation_class)

Registers a macro to be used in setup_validations blocks

Parameters:

  • (Symbol) macro_name — The name to register this macro with
  • (Class) validation_class — The class implementing the validation


20
21
22
23
24
25
26
27
# File 'lib/whyvalidationssuckin96/validation_builder.rb', line 20

def self.register_macro(macro_name, validation_class)
  define_method(macro_name) do |*args|
    attrs, options = extract_options(args)
    attrs.each do |attr|
      add_validation(validation_class, options.merge(:attribute => attr))
    end
  end
end

Instance Method Details

- (Object) create_validations!



13
14
15
# File 'lib/whyvalidationssuckin96/validation_builder.rb', line 13

def create_validations!
  instance_eval(&@definition_block)
end