Module: Ardm::Ar::Validations

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::Validations
Included in:
Base
Defined in:
lib/ardm/ar/validations.rb

Overview

Extend ActiveRecord to support DataMapper validations

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Class Method Details

.extract_options(fields, *keep) ⇒ Object

Extract and convert options from DM style to AR style



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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