module Ardm::Ar::Validations

Extend ActiveRecord to support DataMapper validations

Public Class Methods

extract_options(fields, *keep) click to toggle source

Extract and convert options from DM style to AR style

# File lib/ardm/ar/validations.rb, line 13
def self.extract_options(fields, *keep)
  keep += [:on, :message, :if, :unless]

  if Hash === fields.last
    ar = fields.pop.dup
    w = Array(ar.delete(:when)).first
    if w && [:create, :update].include?(w)
      ar[:on]    = w
    end
    ar[:maximum] = ar.delete(:max) if ar[:max]
    ar[:minimum] = ar.delete(:min) if ar[:min]
    ar[:in]      = ar.delete(:set) if ar[:set]

    removed = ar.slice!(*keep)
    unless removed.empty?
      $stderr.puts "WARNING validation options not handled: #{removed.inspect} in:"
      $stderr.puts caller[0..1]
    end
  else
    ar = {}
  end

  [fields, ar]
end