Class: NotNaughty::ClassMethods::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/not_naughty/class_methods.rb

Overview

With this you get syntactical sugar for all descendants of Validation see validates for examples.

Instance Method Summary collapse

Constructor Details

#initialize(klass, *params) ⇒ Builder

:nodoc:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/not_naughty/class_methods.rb', line 35

def initialize(klass, *params) # :nodoc:
  @_vd_obj            = klass
  @_vd_obj_opts       = params.extract_options!
  @_vd_obj_attrs      = params

  methods = ClassMethods.instance_methods(false).grep(/^validates_.+_of$/)

  if @_vd_obj_attrs.empty?
    for method in methods
      eval <<-EOS
        def self.#{ method[/^validates_(.+)$/, 1] }(*params)
          params << @_vd_obj_opts.merge(params.extract_options!)

          begin
            @_vd_obj.__send__(:#{ method }, *params)
          rescue Exception
            [email protected]_if { |s| /^\\(eval\\):/ =~ s }
            raise
          end
        end
      EOS
    end
  else
    for method in methods
      eval <<-EOS
        def self.#{ method[/^validates_(.+)_of$/, 1] }(*args)
          params = @_vd_obj_attrs.dup
          params << @_vd_obj_opts.merge(args.extract_options!)

          begin
            @_vd_obj.__send__(:#{ method }, *params)
          rescue Exception
            [email protected]_if { |s| /^\\(eval\\):/ =~ s }
            raise
          end
        end
      EOS
    end
  end
end