Module: DataMapper::Constraints::DeleteConstraint::ClassMethods

Included in:
ClassMethods
Defined in:
lib/dm-constraints/delete_constraint.rb

Constant Summary collapse

CONSTRAINT_OPTIONS =
[ :protect, :destroy, :destroy!, :set_nil, :skip ].to_set.freeze

Instance Method Summary collapse

Instance Method Details

#check_delete_constraint_type(cardinality, name, *args) ⇒ nil

Checks that the constraint type is appropriate to the relationship

Parameters:

  • cardinality (Fixnum)

    cardinality of relationship

  • name (Symbol)

    name of relationship to evaluate constraint of

  • options (Hash)

    options hash

Returns:

  • (nil)

Raises:

  • ArgumentError



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/dm-constraints/delete_constraint.rb', line 25

def check_delete_constraint_type(cardinality, name, *args)
  options = extract_options(args)

  return unless options.key?(:constraint)

  constraint = options[:constraint]

  unless CONSTRAINT_OPTIONS.include?(constraint)
    raise ArgumentError, ":constraint option must be one of #{CONSTRAINT_OPTIONS.to_a.join(', ')}"
  end

  # XXX: is any constraint valid with a :through relationship?
  if constraint == :set_nil && options.key?(:through)
    raise ArgumentError, 'Constraint type :set_nil is not valid for relationships using :through'
  end
end