Class: Cequel::Record::HasManyAssociation

Inherits:
Object
  • Object
show all
Defined in:
lib/cequel/record/has_many_association.rb

Overview

Represents a child association declared by has_many.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner_class, name, options = {}) ⇒ HasManyAssociation

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of HasManyAssociation.

Parameters:

  • owner_class (Class)

    Record class that declares this association

  • name (Symbol)

    name of the association

  • options (Options) (defaults to: {})

    options for the association

Options Hash (options):

  • :class_name (Symbol)

    name of the child class

  • :dependent (Boolean)

    propagation behavior for destroy

Since:

  • 1.0.0


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cequel/record/has_many_association.rb', line 31

def initialize(owner_class, name, options = {})
  options.assert_valid_keys(:class_name, :dependent)

  @owner_class, @name = owner_class, name
  @association_class_name =
    options.fetch(:class_name, name.to_s.classify)
  case options[:dependent]
  when :destroy, :delete, nil
    @dependent = options[:dependent]
  else
    fail ArgumentError,
         "Invalid :dependent option #{options[:dependent].inspect}. " \
         "Valid values are :destroy, :delete"
  end
end

Instance Attribute Details

#association_class_nameSymbol (readonly)

Returns name of the child class that this association contains.

Returns:

  • (Symbol)

    name of the child class that this association contains

Since:

  • 1.0.0


17
18
19
# File 'lib/cequel/record/has_many_association.rb', line 17

def association_class_name
  @association_class_name
end

#dependentBoolean (readonly)

Returns behavior for propagating destruction from parent to children.

Returns:

  • (Boolean)

    behavior for propagating destruction from parent to children

Since:

  • 1.0.0


20
21
22
# File 'lib/cequel/record/has_many_association.rb', line 20

def dependent
  @dependent
end

#nameSymbol (readonly)

Returns name of this association.

Returns:

  • (Symbol)

    name of this association

Since:

  • 1.0.0


15
16
17
# File 'lib/cequel/record/has_many_association.rb', line 15

def name
  @name
end

#owner_classClass (readonly)

Returns Record class that declares this association.

Returns:

  • (Class)

    Record class that declares this association

Since:

  • 1.0.0


13
14
15
# File 'lib/cequel/record/has_many_association.rb', line 13

def owner_class
  @owner_class
end

Instance Method Details

#association_classClass

Returns class of child association.

Returns:

  • (Class)

    class of child association

Since:

  • 1.0.0


50
51
52
# File 'lib/cequel/record/has_many_association.rb', line 50

def association_class
  @association_class ||= association_class_name.constantize
end

#instance_variable_nameObject

Since:

  • 1.0.0


55
56
57
# File 'lib/cequel/record/has_many_association.rb', line 55

def instance_variable_name
  @instance_variable_name ||= :"@#{name}"
end