Sha256: 8aef3cedaaf2295cc9d8678d1342e9a05ac4c06972fe68346602bddfc7888647

Contents?: true

Size: 1.09 KB

Versions: 4

Compression:

Stored size: 1.09 KB

Contents

module ValidatesFormattingOf
  module ModelAdditions
    def validates_formatting_of(attribute, opts = {})

      # Here is where the input is formatted before it is validated.
      # Not yet implemented.
      # 
      # unless opts[:preformat].nil?
      #   before_validation do
      #     # add formatting changes here
      #   end
      # end

      regex_for_validation = opts[:regex] || validate_with(opts[:using])

      # Add :allow_nil and :allow_blank options. Both are false by default.
      allow_nil = opts[:allow_nil] || false
      allow_blank = opts[:allow_blank] || false

      validates attribute,  :format => {  :with     => regex_for_validation,
                                          :message  => opts[:message] },
                            :allow_nil => allow_nil,
                            :allow_blank => allow_blank
    end

  private

    def validate_with(method)
      # Actually retrieve the regex to check against
      formatting.send(method)
    end

    def formatting
      # Grab the validating methods
      @formatting ||= ValidatingMethods.new
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
validates_formatting_of-0.3.3 lib/validates_formatting_of/model_additions.rb
validates_formatting_of-0.3.2 lib/validates_formatting_of/model_additions.rb
validates_formatting_of-0.3.1 lib/validates_formatting_of/model_additions.rb
validates_formatting_of-0.3.0 lib/validates_formatting_of/model_additions.rb