module DataMapper::Constraints::Relationship::OneToMany

Attributes

constraint[R]

Public Class Methods

new(*args) click to toggle source

Adds the delete constraint options to a relationship

@param params [*ARGS] Arguments passed to Relationship#initialize

@return [nil]

@api private

Calls superclass method
# File lib/data_mapper/constraints/relationship/one_to_many.rb, line 38
def initialize(*args)
  super
  set_constraint
  assert_valid_constraint
end

Public Instance Methods

enforce_destroy_constraint(resource) click to toggle source

@api private

# File lib/data_mapper/constraints/relationship/one_to_many.rb, line 9
def enforce_destroy_constraint(resource)
  return true unless association = get(resource)

  constraint = self.constraint

  case constraint
  when :protect
    Array(association).empty?
  when :destroy, :destroy!
    association.__send__(constraint)
  when :set_nil
    Array(association).all? do |resource|
      resource.update(inverse => nil)
    end
  when :skip
    true  # do nothing
  end
end

Private Instance Methods

assert_valid_constraint() click to toggle source

Checks that the constraint type is appropriate to the relationship

@param [Fixnum] cardinality

cardinality of relationship

@param [Symbol] name

name of relationship to evaluate constraint of

@param [Hash] options

options hash

@option *args :constraint

one of VALID_CONSTRAINT_VALUES

@raise ArgumentError

if @option :constraint is not one of VALID_CONSTRAINT_VALUES

@return [Undefined]

@api semipublic

# File lib/data_mapper/constraints/relationship/one_to_many.rb, line 66
def assert_valid_constraint
  return unless @constraint

  unless VALID_CONSTRAINT_VALUES.include?(@constraint)
    raise ArgumentError, ":constraint option must be one of #{VALID_CONSTRAINT_VALUES.to_a.join(', ')}"
  end
end
set_constraint() click to toggle source
# File lib/data_mapper/constraints/relationship/one_to_many.rb, line 44
def set_constraint
  @constraint = @options.fetch(:constraint, :protect) || :skip
end